I wrote a shell script(beginner), which works fine but it includes a number of parameters.
I assign the value to them as show below.
url=$2
name=$3
ipadd=$5
netmask=$6
vlanid=$4
vlname=$7
Is there is any better approach, I can use ?
Thanks.
I wrote a shell script(beginner), which works fine but it includes a number of parameters.
I assign the value to them as show below.
url=$2
name=$3
ipadd=$5
netmask=$6
vlanid=$4
vlname=$7
Is there is any better approach, I can use ?
Thanks.
You can use read
instead of multiple assignments:
f=$'\6' # or any other control character
IFS=$f read -d'' -r _ url name vlanid ipadd netmask vlname _ < <(printf "%s$f" "$@")
_
will ignore $1
and anything after $8
.
The only way I would see really doing a better job would be to change to a --flag=value setup, if only to not make the order of arguments as important.
./myscript.sh --url=http://www.example.com --ip=10.42.56.23 --netmask=24
This would then require parsing each argument for the --flag part, then if it is found splitting the variable at the = and setting the value of your real internal value. Worth it for something you are shipping out to users, but maybe not so much for something you are using for yourself.