My favorite shell is The Z Shell. It has many features that can't be found in Bash, for example hash tables:
#!/bin/zsh
typeset -A myArray
myArray[someKey]=firstValue
myArray[otherKey]=secondValue
echo $myArray[someKey] , $myArray[otherKey]
Using hash tables is very easy in Zsh. In Bash you can only emulate hashes.
Another unique Zsh feature is zparseopts. Look at following example:
#!/bin/zsh
zmodload zparseopts
zparseopts -D -E -A Args -- a b -aopt -bopt
if (( ${+Args[-a]} )); then
echo "option -a given";
fi
if (( ${+Args[-b]} )); then
echo "option -b given";
fi
if (( ${+Args[--aopt]} )); then
echo "long option --aopt given";
fi
if (( ${+Args[--bopt]} )); then
echo "long option --bopt given";
fi
if [ "$#@" -gt 0 ]; then
echo Non option arguments are: "$@"
fi
Example outputs of this script:
# ./parseex -a -b
option -a given
option -b given
# ./parseex -a x y --bopt
option -a given
long option --bopt given
Non option arguments are: x y
# ./parseex -a x y --bopt -a
option -a given
long option --bopt given
Non option arguments are: x y
# ./parseex -ab x y --bopt z --aopt z
option -a given
option -b given
long option --aopt given
long option --bopt given
Non option arguments are: x y z z
Notice how extremely easy it is to parse command line options given in not straightforward form! Zparseopts will parse "-ab" as "-a -b" and "-a text -b text2" as "-a -b text text2"! Scripts written using zparseopts behave like normal console applications, because advanced command line options allow comfortable script invocation.
No comments:
Post a Comment