Presentation is loading. Please wait.

Presentation is loading. Please wait.

Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu.

Similar presentations


Presentation on theme: "Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu."— Presentation transcript:

1 Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu

2 About the Class  Lectures along with example demonstrations. Project session for hands- on experience. Have a project?  Trying examples and doing projects on Bluehive  Lectures along with example demonstrations. Project session for hands- on experience. Have a project?  Trying examples and doing projects on Bluehive

3 Bash Script  Unix/Linux commands in a text file – batch mode  An executable program to be run by the shell (Bash)  Unix/Linux commands in a text file – batch mode  An executable program to be run by the shell (Bash)

4 Model for Modern OS Users Shell Kernel Hardware

5 Linux and Shells https://en.wikipedia.org/wiki/

6  Easy to edit: long command lines  Record & reusable: many command lines or options  Flexible and Efficient: Run with different arguments.  Easy to use with job scheduler  Easy to edit: long command lines  Record & reusable: many command lines or options  Flexible and Efficient: Run with different arguments.  Easy to use with job scheduler Bash Script VS. Command Lines

7 Bash Script VS. Other Script Language  Easy to program: the commands and syntax are exactly the same as those directly entered at the command line. Quick start.  Only need a Linux system to run  Slow run speed comparing with other programming languages  Not easy for some tasks like floating point calculations or math functions  Easy to program: the commands and syntax are exactly the same as those directly entered at the command line. Quick start.  Only need a Linux system to run  Slow run speed comparing with other programming languages  Not easy for some tasks like floating point calculations or math functions

8  Manipulate files: handle large number of files, change individual parameters in data files, etc.  Wrap up programs: pipeline different tools with different options/flags  Manipulate files: handle large number of files, change individual parameters in data files, etc.  Wrap up programs: pipeline different tools with different options/flags Scenarios Using Bash Script

9 To Write a Bash Script  An editor: vi emacs, nano,….  Some Linux commands  Set executable permission  Specify interpreter as bash: #!/bin/bash  Comments: # (single line)  An editor: vi emacs, nano,….  Some Linux commands  Set executable permission  Specify interpreter as bash: #!/bin/bash  Comments: # (single line)

10 Some Linux Commands  ls: list directory contents  cd: change directory  man: manual  echo  ls: list directory contents  cd: change directory  man: manual  echo

11 Linux Command echo  Display a line of text  Example: echo hello world  “…” or ‘…’  Display a line of text  Example: echo hello world  “…” or ‘…’

12 File permissions  Three scopes and permissions  Bash script has to have execute permission to allow the operating system to run it.  Check permissions: ls –l  Change permissions: chmod +x filename  Three scopes and permissions  Bash script has to have execute permission to allow the operating system to run it.  Check permissions: ls –l  Change permissions: chmod +x filename

13 Day 1 Examples  ssh to bluehive:  Create a directory for this class: mkdir folderName  cp /public/bliu17/Teaching/Bash/shareFiles.sh. ./shareFiles.sh Day1  ssh to bluehive:  Create a directory for this class: mkdir folderName  cp /public/bliu17/Teaching/Bash/shareFiles.sh. ./shareFiles.sh Day1

14 Bash Variables  Create a variable: name=value  No data type  No need to declare but can be declared with “declare command”  No white space allowed before and after =  Use $ to refer to the value: $name  Name cannot start with a digit.  Create a variable: name=value  No data type  No need to declare but can be declared with “declare command”  No white space allowed before and after =  Use $ to refer to the value: $name  Name cannot start with a digit.

15 Environment Variables  env  $SHELL  $PATH  $LD_LIBRARY_PATH  $RANDOM  $USER  $PWD  env  $SHELL  $PATH  $LD_LIBRARY_PATH  $RANDOM  $USER  $PWD

16 Variable Value  Assign value: a=2  Pass value: b=$a  Display value: echo $a  Strong quoting & weak quoting  Assign value: a=2  Pass value: b=$a  Display value: echo $a  Strong quoting & weak quoting

17 Assign Variable Value  Parameter expansion ${}  Command Substitution: $(), or `  Arithmetic expansion: $(( … ))  Parameter expansion ${}  Command Substitution: $(), or `  Arithmetic expansion: $(( … ))

18 Parameter and Parameter Expansion  Parameter – an entity store value ( e.g. variable)  ${parameter}  Indirection ${!parameter}  Parameter – an entity store value ( e.g. variable)  ${parameter}  Indirection ${!parameter}

19 Arithmetic Expression  Arithmetic operators: + - * /  Integer only  Arithmetic Expansion ((…))  White spaces are not important  Arithmetic operators: + - * /  Integer only  Arithmetic Expansion ((…))  White spaces are not important

20 Basic calculator: bc  An arbitrary precision calculator language  Simple usage: echo $a+$b | bc  Can use math library: echo “s(0.4)” | bc -l  bc –l <<< “s(0.4)”  An arbitrary precision calculator language  Simple usage: echo $a+$b | bc  Can use math library: echo “s(0.4)” | bc -l  bc –l <<< “s(0.4)”

21 Command Substitution  $() is nestable echo $(echo $(ls)) echo “$(echo “$(ls)”)”  cannot be $(( command ))  `` cannot be directly nesting echo `echo \`ls\``  $() is nestable echo $(echo $(ls)) echo “$(echo “$(ls)”)”  cannot be $(( command ))  `` cannot be directly nesting echo `echo \`ls\``

22 Shell Expansions Review  Parameter Expansion: $variable, ${variable}  Arithmetic Expansion: $(( expression ))  Command Substitution: $() or ``  Parameter Expansion: $variable, ${variable}  Arithmetic Expansion: $(( expression ))  Command Substitution: $() or ``

23 Loop Constructs: for loop  Basic Syntax for arg in [list] do ….. done  [list]: 1. Command Substitution: `ls` 2. Arithmetic Expansion 3. Brace Expansion (string or integer): {1..5}  Basic Syntax for arg in [list] do ….. done  [list]: 1. Command Substitution: `ls` 2. Arithmetic Expansion 3. Brace Expansion (string or integer): {1..5}

24 for loop –Arithmetic Expansion  Basic Syntax for (( expr1; expr2; expr3 )) do … done  Examples:  White space are not important for Arithmetic Expansion  Basic Syntax for (( expr1; expr2; expr3 )) do … done  Examples:  White space are not important for Arithmetic Expansion

25 Project  Copy the Project folder to your own directory:./shareFiles.sh Project  Write a Bash script to count how many files there are in files/  Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo  Copy the Project folder to your own directory:./shareFiles.sh Project  Write a Bash script to count how many files there are in files/  Key tech: editor, chmod, command substitution, for loop, arithmetic expansion, echo


Download ppt "Bash Scripting CIRC Summer School 2016 Baowei Liu CIRC Summer School 2016 Baowei Liu."

Similar presentations


Ads by Google