I've got another bash-script problem I simply couldn't solve. It's my simplified script showing the problem:
while getopts "r:" opt; do
case $opt in
r)
fold=/dev
dir=${2:-fold}
a=`find $dir -type b | wc -l`
echo "$a"
;;
esac
done
I calling it by:
./sc.sh -r /bin
and it's work, but when I don't give a parameter it doesn't work:
./sc.sh -r
I want /dev to be my default parameter $2 in this script.
This works for me:
Remove the colon (
:
) in the getopts argument. This caused getopt to expect an argument. (see here for more information about getopt)There may be other parameters before, don't hard-code the parameter number ($2).
The getopts help says
So you want: