I'm just getting into bash and sh scripting. I mostly just stub out little convenience or exercise scripts for myself, but I recognize I may be flaunting best practices at times.
My question is specifically related to best practices (or just common sense) when using rm
inside a shell script. My googles have only turned up suggestions to alias rm
to rm -i
or somesuch.
Anyway, enough talk, here's the script (note: the rm
appears at the very end of the script):
#!/bin/sh -e
# Format output of alias print dump into more readable format
if [ -f "TEMP.AWK" ]; then
echo "Somehow 'TEMP.AWK' already exists"
exit 1
fi
cat ${HOME}/.zshrc | grep -e '^alias' | awk -F'=' 'BEGIN {
print "ALIAS | COMMAND";
print "---------------------------------------";
}
{
# replace all multi-spaces with a single space
gsub(/\s+/, " ", $0);
col1Len = 20;
col2Len = 60;
aliasLen = length($1);
cmdLen = length($2);
tab1len = col1Len - aliasLen;
tab2len = col2Len - cmdLen;
printf " " $1 " "
for (i = 0; i < tab1len; i++)
printf "-"
print " " $2
} END {
print "---------------------------------------";
}' > TEMP.AWK
cat TEMP.AWK | grep --color -e '[#].*'
rm TEMP.AWK
Any other feedback would not go unappreciated.
rm
inside a shell, make sure to use it carefully if you need-rf
:) \$\endgroup\$