1

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
Sherry
  • 311
  • 2
  • 4
  • 9
  • This is fine; it is direct, it is obvious what it does, and it is no longer than it needs to be. – chepner Oct 12 '16 at 19:55
  • You can put multiple assignments on one line if that is what bothers you. – chepner Oct 12 '16 at 20:04
  • See: http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/14203146#14203146 – agc Oct 13 '16 at 01:08

2 Answers2

0

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.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • If I have to assign one value to two variables – Sherry Oct 12 '16 at 19:40
  • suppose $2 for vlanid and x ? – Sherry Oct 12 '16 at 19:41
  • One line != better. In fact, this is worse, because it doesn't work if any of the positional parameters themselves contain whitespace. ("$@" is not a list of separate quoted words here; the whole thing is just one string fed to `read`.) – chepner Oct 12 '16 at 19:54
  • You're right. I have corrected it now to take care of whitespaces in parameters. – anubhava Oct 12 '16 at 20:03
  • Your update assumes none of the values contains a newline. It might work for the intended values, but it still doesn't actually improve on the original. – chepner Oct 12 '16 at 20:03
  • ok updated further to handle newlines as well but yes it doesn't have the simplicity of individual assignments. – anubhava Oct 12 '16 at 20:43
  • Just wanted to know from professional point of you is it fine ? I was reading few scripts on github didn't find this kind of pattern – Sherry Oct 12 '16 at 20:46
  • Multiple assignments are fine but you must quote them in assignment. – anubhava Oct 12 '16 at 20:48
  • 1
    This is of course broken, by design. This would be better: `i=2; for var in url name vlanid ipadd netmask vlname; do printf -v "$var" '%s' "${!i}"; i=$((i+1)); done`. But at this point, OP's method isn't that bad (clear and readable and shorter). – gniourf_gniourf Oct 13 '16 at 06:55
  • Yes agreed @gniourf_gniourf – anubhava Oct 13 '16 at 07:08
0

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.

ivanivan
  • 2,155
  • 2
  • 10
  • 11