Voder-Vocoder

The Log of Hal Canary

Navigation: Home | THE LOG | Log Archives | Resume | Contact Info | Public Key | SSL | Math Applets | Site Map | RSS2 | Atom | Backend

« | »

dealing with lots of arguments in bash

#!/bin/sh
#DTPD#
echo first argument is '"'${1}'"'
for argc in `seq 2 $#`; do
        eval arg=\${$argc}
        echo ${argc}th arg is $arg
done

For example:

#!/bin/sh
# prepend-something.sh
#DTPD#
if [ "$#" -lt 2 ] ; then
        echo "useage:  $0 PREFEX FILE[S]"
        exit 1
fi
PREFEX=${1}
for argc in `seq 2 $#`; do
        eval arg=\${$argc}
        mv -v "${arg}" "${PREFEX}${arg}"
done

Update 2009-12-05: Another way to do it would be to use the shift command.

#!/bin/sh
# prepend-something.sh
#DTPD#
if [ "$#" -lt 2 ] ; then
    echo "useage:  $0 PREFEX FILE[S]"
    exit 1
fi
prefix=${1}
shift
for arg in "$@"; do
    mv -v "$arg" "${prefix}${arg}"
done

Hal Canary | Computers & Code | 2006-12-27 12:00:57 UTC
Permanent Link Comments Off | No Comments

Comments are closed.
Email me your comments — h3 at halcanary dot org.

Copyright 1997-2011 by Hal Canary.
mailto: h3 at halcanary dot org
http://halcanary.org