Bash Shell Scripting is powerful way of automating shell commands. But its syntax is archaic so it’s hard to remember everything. In this blog post, I try to keep track of all the stuff that I learned. It will get updated occasionally.
1. The Start
#!/usr/bin/env bash
set -e
set -x
Beginn every script with those 3 lines. The first make sure it runs on as many devices as possible (see SO), the second lines aborts the whole script if one command fails, the third line prints the command that gets executed.
2. Default Parameters
port=${1:-8888}
echo $port
This takes the first positional paramters as port or 8888 as fallback.
3. Run a command N times
for run in {1..10}
do
command
done
for run in {1..10}; do command; done
4. Run a command indefinitely
while true; do command; done
5. Check if file exists
FILE=data.csv
if test -f "$FILE"; then
echo "$FILE exist"
fi
More material: 1