Archive for August, 2009

Decode command line arguments given to a BASH script

August 25, 2009

Try the elegant way, at last!

For instance, imagine that your programme has the following arguments:
./myprog -s /source/directory -d /dest/directory -c deep

Then, the script myprog should contain a piece of code like this:


if [ $# -eq 0 ] ; then
echo "Usage: $0 -s -d -c"
exit 1
fi
while [ $# -gt 1 ] ; do
case $1 in
-s) source_dir=$2 ; shift 2 ;;
-d) dest_dir=$2 ; shift 2 ;;
-c) copy_mode=$2 ; shift 2 ;;
*) shift 1 ;;
esac
done

Of course, this is only a hint. Just tailor the decoding according to your needs.


Follow

Get every new post delivered to your Inbox.