0

My script is called by a program that generates argument randomly such as

input=12 output=14 destinationroute=10.0.0.0

and then calls my script with the generated arguments:

./getroute.sh input=12 output=14 destinationroute=10.0.0.0

Inside the script is like:

 #!/bin/bash
 input=$1
 output=$2
 destinationroute=$3
 ...

The program always calls arguments in random order (ex. input=12 output=14 or output=14 input=12), and I can't change the program.

Is there any way to recognize the correct parameters and put them in their proper place.

RobS
  • 3,807
  • 27
  • 34
badal16
  • 499
  • 1
  • 5
  • 18

2 Answers2

5

Don't rely on order if they aren't in order. Just iterate over the arguments, look at which patterns they match, and assign to a variable appropriately:

for arg; do # default for a for loop is to iterate over "$@"
  case $arg in
    'input='*) input=${arg#*=} ;;
    'output='*) output=${arg#*=} ;;
    'destinationroute='*) destinationroute=${arg#*=} ;;
  esac
done

If, for some reason, you really wanted to update $1, $2, and $3, though, you can do that by putting the following code after the above loop:

set -- "$input" "$output" "$destinationroute"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

you need to call your function differently; For exmple:

./getroute.sh -i 12 -o 14 -d 10.0.0.0

Then inside your script use getopt to read the variables.

Edit:

My scripting knowledge is not strong; therefore, there should be a better way to do it.

As you don't have access to the Program, you can add some lines inside your script to get the inputs; for example:

input=`echo $* | grep -E -o "input=[0-9]{2}" | awk -F"=" {'print$2'}`

You can do the same thing for other variables.

Aman
  • 1,157
  • 7
  • 13
  • The problem is my automation software puts equal to like input=12, output=14, so i was thinking if we can somehow read those input= and try to parse only values – badal16 Jan 26 '15 at 21:31
  • So to clarify: your **Software** gives you a string like (input=12 output=.....) (lets call it **Str1**), and then calls your **Script** like `(./script $str1)`, and you don't have any control over the **Software** (to change the sequence of **Str1**. Did I understand it correctly? – Aman Jan 26 '15 at 21:38
  • Yes i dont have any control over automation software, i need to do it through script that my scripts correctly reads arguments – badal16 Jan 26 '15 at 21:40
  • 1
    `grep | awk` is always silly: `awk -F= '/input=[[:digit:]]+/ {print $2}'` does the work of both tools in this case, as awk's capabilities are a proper superset of grep's. But in this case it's also silly because the shell can do everything needed built-in, without any fork/exec overhead from calling external commands. – Charles Duffy Jan 26 '15 at 23:02
  • 1
    Another thing to note: `./yourprog input="foo bar" output=baz` is distinct from `./yourprog input=foo bar output=baz`, but concatenating the arguments together with `$*` throws away the information distinguishing them. – Charles Duffy Jan 27 '15 at 03:58