Bash Scripting

Based on previous materials by Dr. Robert Kline


Overview of Bash Script


Setup

1
2
3
4
wget --no-check-certificate https://cs.wcupa.edu/lngo/assets/src/bash_basics.zip
unzip bash_basics.zip
cd bash_basics
ls

Executing a bash script

1
bash SOME-SCRIPT.sh
1
echo "hello world"
1
bash hello.sh

Self-executing bash script

1
chmod +x SOME-SCRIPT.sh          

or

1
chmod 700 SOME-SCRIPT.sh       
1
2
chmod +x hello.sh
./hello.sh

The Bash Language


Interactive Execution


Variables and Values


String operations

1
2
more scalars.sh
./scalars.sh

echo and printf

1
2
printf "num=%05d\n" 27
echo AFTER
1
2
printf -v num "%05d" 27
echo $num
1
2
echo   $'\t'foo
printf "\tfoo\n"
1
echo -e "\033[01;31m HELLO \033[0m THERE"

Other types and declarations

1
2
more scalar-declares.sh
./scalar-declares.sh

Command-line arguments

1
2
3
4
$ more args.sh
$ ./args.sh 
$ ./args.sh  a     b    c
$ ./args.sh "a     b"   c

if-else statements

1
2
3
4
5
6
7
8
if ...
then
  some statements
elif ...
  some statements
else
  some statements
fi

The “…” sections represent boolean “tests”. The chained elif and the else parts are optional. The “then” syntax is often written on the same line as the if portion like this: if ...; then


Program exit status

1
2
3
4
more pingtest.sh
./pingtest.sh 
./pingtest.sh 8.8.8.8
./pingtest.sh 2.2.2.2

The && and || operators


Boolean expressions in test statements

1
2
3
if [ BOOLEAN-EXPRESSION ]; then
  statements ...
fi
1
2
more falsetest.sh
./falsetest.sh
1
[ "$host" ] || { echo usage: $(basename $0) "<host or ip>"; exit 1; }

Unary file information operators

1
2
3
if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

Binary test operators

1
2
more test-values.sh
./test-values.sh

Subtle syntax issues

1
2
more errors.sh
./errors.sh

String patterns and the case statement

1
2
3
4
5
6
7
8
case "$file" in
  *.txt)  # treat "$file" like a text file
          ;;
  *.gif)  # treat it like a GIF file
          ;;
  *) # catch-all
     ;;
esac 

Loops

Bash has both for and while loops. However, the type of control for these is typically not numerical. The most common looping structure in Bash is the for/in structure like this:

1
2
3
4
for x in ...
do
  statements involving $x
done

The “…” is a list of things generated in a number of ways. The x is the loop variable which iterates through each item in the list. For example, try running this program in the current directory:

1
2
more fileinfo.sh
./fileinfo.sh

In this case the things iterated are the files in the current directory. Loops One can use numerical-like looping with the double-parentheses like those in for numerical comparison:

1
2
3
for ((i=1; i<=10; ++i)); do
  echo $i
done 

Reading lines in Bash

1
2
3
while read line; do
  echo "$line"
done
1
2
more process-lines-1.sh
more process-lines-2.sh

Command-line options

1
unzip -q -o FILE.zip -d /usr/local
1
unzip -qo FILE.zip -d /usr/local
1
2
more getopts-test.sh
./getopts-test.sh
1
./getopts-test.sh -q -o FILE.zip -d /usr/local

yields the output:

1
2
3
4
5
6
q 2
o 3
? 3
FILE.zip
d 3 /usr/local
? 3
1
2
./optflags.sh
./optflags.sh -abc foo -d bar foobar barfoo

Built-in string processing operations

1
2
more string-processing.sh
./string-processing.sh

Functions

1
2
more functions.sh
./functions.sh

System command string processing

1
2
more string-operations.sh
./string-operations.sh