PDA

View Full Version : [Solved] argument Pass in shell ?


vaclinux
October 18th, 2009, 09:49
Dear guys,
I have a problem with to understand how the arguments passed could be used. and assigned in specific variable, regardless ordering of argument passed by the user
What i meant is
./eNothing.sh -e value1 -S value2 -c value3
How i could get each value ? I could not just do as following, Because the user might pass parameter incorrect order.


eValue1="$1"
eValue2="$3"
eValue3="$5"

I do aware with sed or awk usage, and i think it needs regular expression to search correct value
based on different option (e.g.: -e, -S,-c). And it is the problem for me, I dont know really understand well to create correct pattern of regexp. Could any help on this ?
Or maybe any other suggestion rather used sed ?

Thanks

ale
October 18th, 2009, 10:50
You can try for example something like this#!/bin/sh
args_no=1
while [ $args_no -le $# ]
do
case "$1" in
-e)
e_opt="$2"
shift 2
;;
-S)
S_opt="$2"
shift 2
;;
-c)
c_opt="$2"
shift 2
;;
*)
echo "WTF? $1"
shift
;;
esac
done

echo "e_opt: " $e_opt
echo "S_opt: " $S_opt
echo "c_opt: " $c_opt

vaclinux
October 18th, 2009, 11:17
Thanks it is working,

anemos
October 18th, 2009, 11:47
getopts is pretty much the standard way to do this.
Example:


#!/bin/sh
while getopts e:S:c opt
do
case "$opt" in
e) e_arg=$OPTARG;;
S) S_arg=$OPTARG;;
c) c_arg=$OPTARG;;
\?) echo "Usage [blah blah]"; exit 1;; #Invalid argument
esac
done


The way it works resembles getopt's a lot.

epopen
August 18th, 2011, 03:26
getopts is pretty much the standard way to do this.
Example:


#!/bin/sh
while getopts e:S:c opt
do
case "$opt" in
e) e_arg=$OPTARG;;
S) S_arg=$OPTARG;;
c) c_arg=$OPTARG;;
\?) echo "Usage [blah blah]"; exit 1;; #Invalid argument
esac
done


The way it works resembles getopt's a lot.
Hi anemos
I use FreeBSD 8.2 amd64 stable and tried to use getopts, like under

#!/bin/sh
opt="-e aa -S bb -c nya"
echo $opt
while getopts e:S:c: opt
do
case "$opt" in
e) e_arg=$OPTARG;;
S) S_arg=$OPTARG;;
c) c_arg=$OPTARG;;
\?) echo "Usage [blah blah]"; exit 1;; #Invalid argument
*) c_arg=$OPTARG;;
esac
done

shift $(($OPTIND - 1))
echo $e_arg
echo $S_arg
echo $c_arg

But result:

aa -S bb -c nya
(empty line)
(empty line)
(empty line)

Even "shift $(($OPTIND - 1))" exist or not
getopts can't work correct..
Is problem?
Thanks a lot

poh-poh
August 18th, 2011, 08:55
getopts operates only on positional parameters, those passed as arguments to the script or set via set builtin. Try to replace the first 2 lines with

set -- -e aa -S bb -c nya
echo $@

epopen
August 18th, 2011, 09:47
getopts operates only on positional parameters, those passed as arguments to the script or set via set builtin. Try to replace the first 2 lines with

set -- -e aa -S bb -c nya
echo $@

Thanks you a lot.
I write a shell scripts for periodic execute.
And given argument from /etc/rc.conf (xxx_flags="-a -b")
So I wish to parser argument by getopts...
Now I use your solution instead like under
set -- $abc_flags

Example on internet is too difficult
Thanks you very much.