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.

7
  • Are you asking about how to parse arguments instead of just assuming an order? Does your script currently handle the arguments in a specific order somehow? Commented Jan 26, 2015 at 19:44
  • I can't understand what you need, plz give some more details (ie., upload a code, your current problem and your required format) Commented Jan 26, 2015 at 19:44
  • 1
    Is this bash or powershell? Veeeery different tags. Commented Jan 26, 2015 at 19:54
  • Yes, it handles in specific order but automation software gives my script random order arguments, so how can i get specific argument to correct variable Commented Jan 26, 2015 at 20:20
  • 1
    I think you'll find the answer here: stackoverflow.com/questions/16483119/… Commented Jan 26, 2015 at 20:33

2 Answers 2

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"
Sign up to request clarification or add additional context in comments.

Comments

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.

5 Comments

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
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?
Yes i dont have any control over automation software, i need to do it through script that my scripts correctly reads arguments
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.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.