bashscript july2015 day1 - university of...
TRANSCRIPT
![Page 1: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/1.jpg)
Bash Script
CIRC Summer School 2015 Baowei Liu
![Page 2: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/2.jpg)
Command Lines VS. Bash Script
Unix/Linux commands in a text file A series of commands executed in batch
mode
![Page 3: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/3.jpg)
Review of Linux Users
Shell
Kernel
Hardware
![Page 4: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/4.jpg)
Linux and Shells
https://en.wikipedia.org/wiki/
![Page 5: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/5.jpg)
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
![Page 6: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/6.jpg)
File manipulation
Wrappers
Examples Using Bash Script – Pipeline things
![Page 7: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/7.jpg)
Shell 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.
Slow run speed comparing with other programming languages
Not easy for some tasks like floating point calculations or math functions
Not friendly to use: error messages/white space.
![Page 8: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/8.jpg)
Linux Commands
ls: list directory contents cd: change directory man: manual echo
![Page 9: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/9.jpg)
Linux Command echo
Display a line of text Example: echo hello world “…” or ‘…’
![Page 10: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/10.jpg)
To Write a Bash Script
An editor: vi emacs, nano,…. Specify interpreter as bash: #!/bin/bash Some Linux commands Comments: # (single line) Set executable permission
![Page 11: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/11.jpg)
File permissions and First Script
Three scopes and permissions Bash script has to have execute permission
to allow the operating system to run it. Check permissions: ls –l Add execute permission: chmod +x First script
![Page 12: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/12.jpg)
Bash Variables Create a variable: name=value No data type No need to declare but can be declared with
“declare command” No space allowed before and after = Use $ to refer to the value: $name
![Page 13: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/13.jpg)
Environment Variables
env $SHELL $PATH $LD_LIBRARY_PATH $RANDOM $USER
![Page 14: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/14.jpg)
Variable Value
Assign value: a=2 Pass value: b=$a Display value: echo $a Multiple Variables Strong quoting & weak quoting
![Page 15: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/15.jpg)
Assign Variable Value
Parameter expansion ${} Command Substitution: $(), or ` Arithmetic expansion: $(( … ))
![Page 16: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/16.jpg)
Arithmetic Expression
Arithmetic operators: + - * / Integer only Arithmetic Expansion ((…)) Floating point calculation: other tools like
bc, or awk
![Page 17: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/17.jpg)
Basic calculator: bc
An arbitrary precision calculator language Simple usage: echo $a+$b | bc Can use math library: echo “s(0.4)” | bc –l
![Page 18: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/18.jpg)
Conditional Expression and if
If condition then …. else …. fi
![Page 19: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/19.jpg)
Conditional Expression
Integers (Numeric Comparison): (( )) operators ==, !=,>,<,>=,<= You can use standard C-language operators
inside (( )) white spaces are not necessary
![Page 20: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/20.jpg)
Conditional Expression: Strings
Compare strings: [[ “$a” = “$b” ]] operators = or ==, !=, >, < (careful!) White spaces around [[ ]] and operators are
necessary!! if [[ $a=$b ]]; then echo "$a=$b" else echo "$a!=$b" fi
-n (not null), -z(null)
![Page 21: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/21.jpg)
Compound Operators
&&, ||
if [[ … ]] && [[ … ]] then …. fi
![Page 22: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/22.jpg)
Alternate Ways: Arithmetic Expansion
Old but more portable way [ ] or test
![Page 23: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/23.jpg)
Compare Floating Point Numbers
Use Basic Calculator: bc compare_results=`echo “$a>$b” | bc` double quotation are important!! Operators: ==, !=, >, >=, <, <= Convert to integer (Return 1 for True and 0
for False) Always check the command before using it!
![Page 24: bashScript July2015 day1 - University of Rochesterbliu/Teaching/bashScript_July2015_day1.pptx.pdfShell Script VS. Other Script Language Easy to program: the commands and syntax are](https://reader035.vdocuments.site/reader035/viewer/2022070718/5ede3f80ad6a402d6669913e/html5/thumbnails/24.jpg)
Shell Expansions Review
Parameter Expansion: $variable, ${variable}
Arithmetic Expansion: $(( expression )) Command Substitution: $() or ``