# getopts
cat>/tmp/tar.sh<<"EOF"
declare -A myopts
usage() {
echo "Usage: $0 [-zxvf <fname>] [-C <dir>]" 1>&2
exit 1
}
while getopts "xvzf:C:" opt "$@"; do
case "${opt}" in
z|x|v|C|f) myopts["${opt}"]=${OPTARG} ;;
*) usage ;;
esac
done
shift $((OPTIND-1))
for key in ${!myopts[@]};
do echo "$key => ${myopts[$key]}"; done
EOF
bash /tmp/tar.sh -zxvf ~//tmp/tst.tar.gz -C /tmp
-------output----------
C => /tmp
f => /home/neutrino0717//tmp/tst.tar.gz
v =>
x =>
z =>
|
# getopts
cat>/tmp/tar.pl<<"EOF"
use Data::Dumper;
use Getopt::Std;
# read options
getopts('C:zxvf:', \%opts);
print "the options are:\n";
print Dumper (\%opts);
EOF
perl /tmp/tar.pl -zxvf ~//tmp/tst.tar.gz -C /tmp
-------output----------
the options are:
$VAR1 = {
'z' => 1,
'C' => '/tmp',
'v' => 1,
'x' => 1,
'f' => '/home/neutrino0717//tmp/tst.tar.gz'
};
|
# getopt
cat>/tmp/tar.py<<"EOF"
import sys, getopt
argv=sys.argv[1:]
try:
opts = getopt.getopt(argv,"zxvf:C:")
except getopt.GetoptError:
print "Usage: "+sys.argv[0]+" [-zxvf <fname>] [-C <dir>]"
sys.exit(2)
print opts
EOF
python /tmp/tar.py -zxvf ~/tmp/tst.tar.gz -C /tmp
-------output----------
([('-z', ''), ('-x', ''), ('-v', ''), ('-f', '/home/neutrino0717/tmp/tst.tar.gz'), ('-C', '/tmp')], [])
|