Titolo presentazione Piattaforme Software per la Rete sottotitolo BASH Scripting Milano, XX mese 20XX A.A. 2016/17, Alessandro Barenghi
Outline 1) Introduction to BASH 2) Helper commands 3) Control Flow constructs 4) Plumbing and Input/Output 2/27
Introduction to BASH
Shell scripts A bash script is simply a ASCII text file, which is fed to a command interpreter Every script should always begin with the hashbang directive: #!/path/to/interpreter In case of BASH it usually is: #!/bin/bash Any executable can be used as an interpreter In case no hashbang is specified, the default interpreter is used (usually /bin/sh) 4/27
Shell Variables By default, the variables have global visibility in the script Bash variables are untyped Variable assignment: PLUTO= blablabla The common convention is to have ALL_UPPERCASE names (not mandatory) In order to use a variable, you have to use the $ operator: $PLUTO 5/27
Special Variables Command-line parameters: $0, $1, $2, : similar to the C s argv $*: single variable with the concatenated parameters $@: array of parameters as tokenized by bash $#: number of parameters $?: return value of the previous command 6/27
Special Variables Environmental variables: Exported variables (maybe outside your script) Can be accessed by running programs (e.g. through the C getenv function) export and unset commands in BASH 7/27
Helper commands
Sequence of numbers seq command generates a sequence of numbers: Syntax: seq [first] [increment] last -f format -w -s SEP : printf-style notation to specify the format string : add padding zeros to equalize width : use SEP as separator instead the default one (\n) 9/27
Concatenation, splitting : concatenate file passed as input or as parameters to stdout : splits an input file into multiple output ones, with a given discipline cat split -l N : go to next file after N lines and tail print to stdout the first or the last 10 lines of a file (number of lines can be modified by -n N ) head 10/27
Arithmetic operations Performing integer arithmetic operations in BASH are performed through arithmetic expansion: expr can be used with backticks: CINQUE=`expr 4 + 1` let, $(( )), $[ ] as evaluation of a string: let ABC=$A + 3 ABC=$(( $A + 3 )) ABC=$[ $A + 3 ] 11/27
Arithmetic operations What about floating point operations? In BASH you can t because you don t need it. If you really think you need it you are probably wrong. But if you can use any command able to compute them: Tipical commands: Any other scripting language: python bc awk zsh R perl Be aware of the portability! 12/27
Control Flow constructs
Script level control flow exec COMMAND replace the current shell without creating a new process exit the scripts. If NUM is provided, returns the value NUM (often non-zero value mean error) exit [NUM] source FILENAME execute FILENAME in the current context 14/27
The `if` construct Syntax: if <condition>; then <statements>; else <statements>; fi The output of a command can be used as a condition: returned value == 0 (successful execution) TRUE returned value!= 0 (error) FALSE The test command is useful in many situation: returns true if the file exists test -e <filename> test! -e <filename> test <string1> = <string2> returns true if the file does not exist returns true if the strings match The operator [] can be used as synonym of test 15/27
The `for` construct Syntax: for <variable> in <list>; do <statements>; done Loop similar to foreach construct in programming languages By default split over whitespaces, tabs and newlines Can be changed properly setting IFS variable (i.e. Internal Field Separator) List can be obtained by other commands, e.g.: for i in `seq 1 10`; do echo $i; done 16/27
The `for` construct The for construct can be used to iterate over filenames: All files in the current directory: for i in *; rm $i; done Only jpg files: for i in *.jpg; cp $i $RANDOM.jpg; done Only files starting with abc with ext.sh in folder mistero: for i in mistero/abc*.sh; chmod +x $i.sh; done 17/27
The `while` construct Syntax: while <condition>; do <statements>; done <condition> follows the same rules of if construct Example: i=5 while [ $i -gt 0 ] do echo $i let i=$i-1 done 18/27
The `case` construct Similar to switch-case construct in C Syntax: case <variable> in <pattern>) <statements>;; <pattern>) <statements>;;... esac <pattern> can be a regex: * : indicates zero or more characters [] : indicates a character class, e.g. [0-5] 19/27
Functions Syntax for function declaration: <function_name> () { <statements> } All the functions are assumed to have variable arguments, accessible via the usual special variables ($1, $2,, $@,...) Syntax for function invocation: <function_name> [arg1 arg2 arg3...] Warning: the function body is not parsed until the function is invoked, latent syntax errors may hide there! 20/27
Plumbing e Input/Output
BASH Plumbing UNIX Philosophy: keep program small and simple, combine them to obtain complex effects BASH provides the user constructs to: Concatenate the standard output files of a command into standard input of another one Feed any of them with/to a file on the disk Duplicate data flows or merge them 22/27
Redirection Redirection operators with files: a>b : redirect the stdout of a to file b (eventually overwrite) a<b : redirect the file b to the stdin of a a >> b : redirect the stdout of a to file b (append) Redirection can be used to reference file descriptor: cmd 1>&2 : redirect output to stderr Common file descriptors: stdin:0, stdout:1, stderr:2 23/27
BASH Plumbing Pipe operator: : redirect the stdout of cmd1 into the stdin of cmd2 (this is not bidirectional!) cmd1 cmd2 Some useful targets: cmd1 less or cmd1 sort : sorts by line the text passed as input cmd1 uniq : returns unique occurrences of the lines cmd1 > /dev/null cmd1 more to read long text : a special file that discards anything 24/27
The `grep` command The It can be used with stdin or with a filename: grep command prints lines that match a pattern command grep pattern e.g. ls -l grep txt grep pattern filename.txt e.g. grep ERROR log.txt Matching can be inverted with -i -v option option provides a case-insensitive matching 25/27
The `tee` command Name from the idraulic T-splitter Syntax: Example: tee <filename> ls -l tee file.txt less ls -l tee file.txt less stdout stdin file.txt Figure from https://commons.wikimedia.org/wiki/file:tee.svg (GFDL licence) 26/27
Control buffers size stdin, stdout and stderr are buffered You can use the buffer Useful when dealing with interactive application like stdbuf [-i N] [-o N] [-e N] command to control the size of socat or netcat Figure from https://commons.wikimedia.org/wiki/file:tee.svg (GFDL licence) 27/27