argument Pass in shell ?

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
Code:
./eNothing.sh -e [I]value1[/I] -S [I]value2[/I] -c [I]value3[/I]
How i could get each value ? I could not just do as following, Because the user might pass parameter incorrect order.

Code:
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
 
You can try for example something like this
Code:
#!/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
 
getopts() is pretty much the standard way to do this.
Example:

Code:
#!/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(3)'s a lot.
 
anemos said:
getopts() is pretty much the standard way to do this.
Example:

Code:
#!/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(3)'s a lot.
Hi anemos
I use FreeBSD 8.2 amd64 stable and tried to use getopts, like under
Code:
#!/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:
Code:
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
 
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
Code:
set -- -e aa -S bb -c nya
echo $@
 
poh-poh said:
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
Code:
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
Code:
set -- $abc_flags

Example on internet is too difficult
Thanks you very much.
 
Back
Top