# argument Pass in shell ?



## vaclinux (Oct 18, 2009)

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 [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.


```
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 (Oct 18, 2009)

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 (Oct 18, 2009)

Thanks it is working,


----------



## anemos (Oct 18, 2009)

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(3)'s a lot.


----------



## epopen (Aug 18, 2011)

anemos said:
			
		

> getopts() is pretty much the standard way to do this.
> Example:
> 
> 
> ...


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 (Aug 18, 2011)

*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 (Aug 18, 2011)

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
> 
> ```
> set -- -e aa -S bb -c nya
> ...


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.


----------

