meallhour
meallhour

Reputation: 15599

How to check various values of same parameter

I have a custom command which looks as below:

bashcustomCommand cfgAlert -i 1

However, the value of i can be anything from 1 to 4 I want to modify the custom command so that it accounts for all the values of i from 1 to 4 in a single command

As of now, I have written it like as below:

bashcustomCommand cfgAlert -i 1
bashcustomCommand cfgAlert -i 2
bashcustomCommand cfgAlert -i 3
bashcustomCommand cfgAlert -i 4

I want to write a single command which accounts for all the 4 values

Upvotes: 0

Views: 43

Answers (2)

Cyrus
Cyrus

Reputation: 88776

echo {1..4} | xargs -n 1 echo bashcustomCommand cfgAlert -i

Output:

bashcustomCommand cfgAlert -i 1
bashcustomCommand cfgAlert -i 2
bashcustomCommand cfgAlert -i 3
bashcustomCommand cfgAlert -i 4

Remove second echo if everything looks fine.

Upvotes: 5

Paul Hodges
Paul Hodges

Reputation: 15388

I assume you are assigning the value to $i for this example.

if [[ a == "$i" ]]
then for this in {1..4}
     do  "$0" "$1" -i "$this"
     done
     exit
fi

If it's 1, 2, 3, or 4, it will do what it always does. If it's a, it will cycle through those in subcalls and then exit.

I'd add error checking and such, but this will keep you from having to type the loop on the command line.

Upvotes: 0

Related Questions