Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm writing a shell script to be used for various purposes. Various operations depend on input parameters. How can I check which parameter has been passed to the shell script while it runs?

Can someone please explain how this query will work? Say I've 1 shell script abc.sh which takes 1 input parameter and depending on its value the shell script should or should not do something. Say I've passed '1' as an input parameter and scheduled a run. While the shell script runs, how do I test the parameter's value?

share|improve this question

closed as unclear what you're asking by devnull, Chris Down, Anthon, strugee, slm Apr 11 '14 at 8:09

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

    
Parameters are stored in $1, $2, … Just have them tested (haha!) with the usual tools ([ … ]). Maybe you want to edit your question to be a bit more specific otherwise. ;) –  Andreas Wiese Apr 11 '14 at 6:29
    
How do I ask a good question? –  devnull Apr 11 '14 at 6:30
    
Have you tried reading a shell scripting tutorials? They are plentiful - randomly picking from the first search page: linuxconfig.org/bash-scripting-tutorial linuceum.com/Distros/osCmdLineScriptStruct.php –  peterph Apr 11 '14 at 10:06
    
Do you want to "test" the parameter values from inside the running script or from outside, e.g. from another shell/another process? –  Dubu Apr 11 '14 at 11:02

1 Answer 1

_fn() { set -- "$@" $(cat)
    while ${1+:} false ; do 
        echo "$1" && [ "$1" = "arg2" ] && echo "$1"
        $YOUR_CHK
        shift
    done
}

echo "arg2" | _fn "arg1"

OUTPUT

arg1
arg2
arg2

That handles both cmd-line args and stdin. It only runs the while loop to check them while you still have at least one argument saved in your parameter array. It does discard every argument it checks, so part of $YOUR_CHK should be saving information you think valuable in one way or another.

_fn would behave the same if its contents were the body of a script, or as is in the form of a shell function.

_fn handles stdin - in this case "arg2" echoed over the |pipe - in the first line by setting its positional parameter shell $@array to everything passed on the command-line - or "$@" - AND everything cat spits out via $(comand substitution).

while _fn's $1 first parameter is ${set+} we substitute the shell's built-in :true to satisfy the while loop conditional. As soon as $1 is unset that substitution fails to happen, the conditional evaluates to false and the while loop breaks.

For every iteration of the while loop the _fn() echoes its $1 first parameter && if successful (echo is always successful) [ tests ] to see if $1 equals the string "arg2" && if the [ test ] is successful _fn() echoes $1 again.

$YOUR_CHK is a null-op - it is an unset variable that evaluates to nothing before ever the shell executes any code.

For every iteration of the while loop we shift away the $1 first parameter. So on iteration 1 "$@" looks like:

arg1 arg2

But after we shift the first time it looks like:

arg2

And after we shift the last time it looks like:

So this brings us back around again to ${1+:} false - because $1 is now unset the shell will not substitute :true and instead only false is evaluated, which breaks the while loop and ends the _fn().

share|improve this answer
    
Hi Mike,Thanks for reply. but didn't get you. Can you please explain how this query will work. Let me explain my requirement. Say I have 1 shell script abc.sh which take the 1 input parameter and on the basis of parameter shell script will do something. lets say I have passed '1' as input parameter and scheduled for run. shell script is running and I want to check what parameter i have passed to shell script. –  user55551 Apr 11 '14 at 7:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.