Besides the points bellow, what would best practices for scripting be ?
1) set the PATH
2) unset the current environment (set -u ?)
3) (re)set the IFS to default value - space (IFS="" <- is this correct ?)
4) check the return code for each action inside the script (cd, rsync, cp, mv, etc)
5) use quotes around variables (avoid white spaces - ex: "$var")
6) always indent (for, while, until, case, etc) loops
Thx for taking the time to answer.
da1,
script kiddie trying to learn the correct way of scripting
I don't know why ou'd really need to do #2, and I'm not totally sure what a portable way to do it would be. Normally, you need PATH, LD_LIBRARY_PATH, but scores of environment variables are needed by different commands.
#3 unset IFS
#4 you dont have to check the return code per se; you can set up an execution chain, and if anyone breaks, there's been an error:
cd /var/www && rsync . /new/dir && cp /something /new/dir
#5 yes. Use "$@" when referring to the entire argument list and "$var" in almost every case. (Sometimes you need to use single-quotes, sometimes you want to let it expand with spaces and you want no quotes).
#6 uh, whatever. syntax and indentation aren't standard. Two common ways:
if [ "go" = "$1" ] ; then
while [ -f /var/lock/status ]; do
run_command
done
fi
And
if [ "go" = "$1" ]
then
while [ -f /var/lock/status ]
do
run_command
done
fi