home |
electronics |
toolbox |
science club |
tuxtalk |
photos |
e-cards |
online-shop
April 2022
Bash script tricks and recipes
There are a number of programming concepts that are important but hard to remember.
This is a cheat-sheet type of document where you can just copy ideas to your
own bash scripts.
Read STDIN line by line by looping over the data from STDIN:
while read l; do
echo "this is one line: $l"
done
Read a file line by line and assigning the value to a variable:
while read l; do
echo "this is one line: $l"
done < file.txt
Assign the output of a command to variables in an array (whitespace is the default field delimiter). You can think of this as a good way of assigning multiple variables at the same time:
arr=( `echo "1 two 3"` )
echo ${arr[0]} # prints 1
echo ${arr[1]} # prints two
echo ${arr[2]} # prints 3
echo ${arr[@]} # prints all elements: 1 two 3
echo ${!arr[@]} # prints all available array indices: 0 1 2
echo ${#arr[@]} # prints array size: 3
Read data from myfile.txt and split out columns using awk. Assing the values
located with awk to two variables (${arr[0]} and ${arr[1]}):
linenumber=0
while read oneline; do
linenumber=`expr $linenumber "+" 1`
arr=(`echo $oneline | awk '{print $2 " " $3}'`)
echo "line $linenumber: second field in file=${arr[0]}, third=${arr[1]}"
done < myfile.txt
Loop over the content of an array:
arr=( `echo "1 two 3"` )
for val in ${arr[@]}; do
echo "value is: $val"
done
Loop 2 to 10:
for i in {2..10}; do
echo "$i"
done
This will print the numbers from 2 to 10
Note: there is no space inside the "{ }".
Test if stdin is from the terminal or a pipe:
if [ -t 0 ]; then
echo "interactive terminal"
else
echo "reading data from a pipe in stdin"
fi
Get date and time in a nicely sortable format
today=`date "+%Y-%m-%d"`
echo "date: $today"
dt=`date "+%Y-%m-%d_%H%M%S"`
echo "date and HourMinSec: $dt"
dtzone=`date "+%Y-%m-%d_%H%M%S %Z"`
echo "date and HourMinSec and timezone: $dtzone"
A complete bash script with help function and and an option parser:
#!/bin/bash
help()
{
cat << EOH
arg-parse-demo.sh -- a bash argument parser
Usage: arg-parse-demo.sh [-h][-v][-a value] file1 [file2]
OPTIONS:
-h this help
-v verbous flag
-a this option takes an argument.
Examples:
arg-parse-demo.sh -h
arg-parse-demo.sh -a hello firstfile secondfile
EOH
exit 0
}
opt_v=0 # we make this numeric
opt_a="" # an empty string
# the arguments we accept are listed below. A colon after the arg means it takes a value:
while getopts "a:hv" o; do
case "${o}" in
a)
opt_a="$OPTARG"
;;
h)
help
;;
v)
opt_v=1
;;
\?) exit 1 # invalid option, getopts itself will print the error
;;
esac
done
shift $((OPTIND-1))
# the $1 is now the first argument after all the options
# $* is all the argumnets after the options
echo "remaining commandline arguments: $*"
if [ $opt_v -gt 0 ]; then
echo "option -v was given"
fi
if [ -n "$opt_a" ]; then
echo "option -a was given with value: $opt_a"
fi
# since this program expect at least one argument after all the options we call
# help unless there is one argument left
if [ -z "$1" ]; then
help;
fi
Loop over files given on the command even if they contain spaces (such as "file with space in filename.txt"):
for f in "$@" ; do
echo "file size of: $f"
du -hs "$f"
done
© 2004-2024 Guido Socher