Answer the question
In order to leave comments, you need to log in
How to organize parsing of arguments and switches in bash?
For example, I call my script like this:
MyPlugin header footer -c
Therefore, the order and number can be anything. Footer and header are arguments, and -c is the execution key.
Answer the question
In order to leave comments, you need to log in
Have a look at libshflags
https://debian.pro/files/anlamp/mksite.sh - usage example
https://code.google.com/p/shflags/ the library itself
If the script has named parameters (options), it's time to switch from bash to languages richer in structures and libraries like Perl, Python, Ruby. Easier debugging, support, refinement.
In general, I heaped up such a hell:
for val in [email protected]
do
loopIndex=$[loopIndex+1]
if [ ! ${val:0:1} = "-" ]; then
result=${args[$loopIndex]}
arguments+=$result
else
echo "не соответствие"
fi
done
#!/bin/bash
showopts () {
while getopts ":pq:" optname
do
case "$optname" in
"p")
echo "Option $optname is specified"
;;
"q")
echo "Option $optname has value $OPTARG"
;;
"?")
echo "Unknown option $OPTARG"
;;
":")
echo "No argument value for option $OPTARG"
;;
*)
# Should not occur
echo "Unknown error while processing options"
;;
esac
done
return $OPTIND
}
showargs () {
for p in "[email protected]"
do
echo "[$p]"
done
}
optinfo=$(showopts "[email protected]")
argstart=$?
arginfo=$(showargs "${@:$argstart}")
echo "Arguments are:"
echo "$arginfo"
echo "Options are:"
echo "$optinfo"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question