My bash notes to make scripts in GNU/Linux

Here are some Bourne Shell notes that I use to make scripts.

Arrays

Create

$ my_array=()

Add/Push element

$ element=banana
$ my_array+=($element)

Get elements number

$ echo "${#my_array[@]}"

Print the first element

$ echo "${my_array[0]}"

Print all elements of an array

x=0
while [ "$x" -lt "${#my_array[@]}" ]
do
	echo "${my_array[$x]}"
	x=$(( $x + 1 ))
done
for i in "${my_array[@]}"
do
	echo "$i"
done

Arithmetic Operations

Examples

  • Addition 1 :
$ a=3
$ let a=a+1
$ echo $a
4
  • Addition 2 :
$ a=3
$ a=$((a+1))
$ echo $a
4

Case… Esac

Structure

case $1 in
	*.mp3)    echo "$1 is a mp3 file";;
	*.mkv)  echo "$1 is a mkv file";;
	*)
        echo "$1 is an unknown"
        echo "Format.";;
esac

Check last command result

if [ $? -eq 0 ] ; then

Comparison

  • Comparison :
    • String, Text : =, !=, -n (not null), -z (null)
    • Numeric : -eq (equal), -ne (not equal), -lt (less than), -gt (greater than), -ge (greater or equal)
    • Logical operators : && (AND), || (OR), ! (NOT)
    • File : -f (if exist), -d (directory), -s (size file is not null), -x (if file is executable)

For… Do… Done

Structure

  • Example n°1 :
for x in 1 2 3
do
	echo $x
done
  • Example n°2 :
for s in www-0{1..8}
do
	echo $s
done

Manage spaces in a for loop

By default if you put, a command result inside a variable, and do a for loop on it, the spaces, tabulations and newlines will be considered as new lines.

Let's see a very useless but concrete example.

  • We put inside a text variable, a text with spaces :
$ text="Body screaming fucking bloody mess"
  • If we do a for loop we can see that our sentence is completely messed up with new lines :
for i in $text; do
	echo "$i"
done
Body
screaming
fucking
bloody
mess

The reason is because of the IFS (Internal Field Separator) default value is space, tab and newline.

So, to avoid that, we need do redefine IFS variable, to tell that only new lines should be considered as... new lines...

$ IFS=$'\n'
for i in $text; do
	echo "$i"
done
Body screaming fucking bloody mess

If… Then… Else… Fi

Structure

if [ -s $1 ]
then
	echo "$1 exist"
else
	echo "$1 doesn't exist"
fi

Examples

If variable is null

if [ ! -n "$var" ]; then

Double if condition

  • Example n°1 :
if [ -n "$answer" ] && [ "$answer" != "N" ]; then
	echo "OK"
fi
  • Example n°2 :
if [ $(ssh user@192.168.1.200 mount | grep -q crypt_backup; echo $?) -eq "0" ] && [ $(ps -ef | grep -q [r]sync; echo $?) -ne "0" ] ; then
        echo "OK"
fi

Text manipulation

Substitution

  • % to replace suffix. Here we replace the file extension :
$ FILE="The_Dolphins_-_Demo_-_Anemia.ogg"
$ echo "${FILE%.ogg}.mp3"
The_Dolphins_-_Demo_-_Anemia.mp3
  • # to replace prefix :
$ FILE="The_Dolphins_-_Demo_-_Anemia.ogg"
$ echo "${FILE#The_Dolphins_-_Demo_-_}"
Anemia.ogg

expr command

Example n°1

$ line="secure-policy rule: 1"
$ expr match "$line" '.*: \([0-9]\+\)'
1

Example n°2

$ line="  name: STD01_to_ZYXEL_SSH"
$ expr match "$line" '.*: \([a-zA-Z0-9_]\+\).*'
STD01_to_ZYXEL_SSH

Read a file line by line

while IFS= read -r line
do
	echo "$line"
done < /tmp/a_file.txt

Stop script on error

Source : https://wizardzines.com/

  • Stops the script on errors
$ set -e
  • Stops the script on unset variables
$ set -u
  • Stops the script on failing pipeline
$ set -o pipefail
  • Stops on all of these
$ set -euo pipefail

While… Do… Done

Structure

while [ $a -gt 0 ]
do
	echo "a=$a"
	let a=$a-1
done
Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :