Computing.Net > Forums > Unix > Giving an input value to a script

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Giving an input value to a script

Reply to Message Icon

Name: ch Bushu
Date: May 14, 2009 at 05:44:10 Pacific
OS: Unix
Subcategory: General
Comment:

Hi Experts,
I want to pass one input to a script. I tried the below command:

echo "1" | script.sh arg1 arg2

means script.sh will take two arguments arg1 and arg2. 1 is the input to the script which it has to read from the user. Its like selecting an option from number of scripts.

But when I tried the above mentioned command, it is going to an infinite loop. Its excuting the same command until I stop it by pressing Cntrl+z

Can anyone please help me to solve this?

Thank you



Sponsored Link
Ads by Google

Response Number 1
Name: lankrypt0
Date: May 14, 2009 at 06:42:50 Pacific
Reply:

I am a little confused by your explanation. are you simply looking for a way for a user to select an option in a script? For example? Are you looking for the script to do something like:

What is your gender?
1) Male
2) Female

And then have the user hit either 1 or 2 to select the answer?


0

Response Number 2
Name: lankrypt0
Date: May 14, 2009 at 07:04:40 Pacific
Reply:

let's use the other topic you opened to discuss this, ignore my post above


0

Response Number 3
Name: ch Bushu
Date: May 14, 2009 at 20:38:39 Pacific
Reply:

Yes..exactly...

But the script has to automatically take the option also (I am triggering a cron job for this)


0

Response Number 4
Name: lankrypt0
Date: May 15, 2009 at 05:25:49 Pacific
Reply:

ok, without knowing what exactly you are trying to do, here is what I would suggest:

#!/usr/bin/ksh
getgender () {
echo -n 'What is your gender:\n1) Male\n2) Female\n\nSelection: ';read entry
casestatement
}

casestatement () {
case $entry in
1)
echo "You chose male"
x=1
;;
2)
echo "You chose female"
x=1
;;
*)
echo "The only options are 1 and 2! Try again!"
;;
esac
}

if [[ $1 = "" ]];then
 until [[ $x = 1 ]];do
  getgender
 done
else
 entry=$1
 casestatement
fi

I defined my 2 functions up front, remember, functions are not used unless they are called.

The first function is called getgender and simply asks for the users input, assigning their answer to $entry. Then it runs the second function, casestatement

The second function holds my case statement. This is where the work will be done. You can put as many lines as you would like in place of the echo statements to do any work that is needed. You will notice that there are two x=1 lines in there, those will let us loop the getgender function until a correct entry is entered.
The last lines of the case: *) through the last ;; is what you would place if someone enters an option that is not valid (in this case, anything but 1 or 2).

The last lines is where we determine if $entry will be taken from the command line or by the getgender function.
If the script is run without an argument, getgender is run until a valid entry is selected, otherwise it will keep looping.
If the script is run with an argument, that argument will be set to $entry, then we skip the getgender function and go right to casestatement. No looping will occur if a bad entry is set, it will simply exit out with "The only options are 1 and 2! Try again!"

Hopefully that makes sense.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More






Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: Giving an input value to a script

assigning a variable to a script www.computing.net/answers/unix/assigning-a-variable-to-a-script/5566.html

Prob Assign Row count to a variable www.computing.net/answers/unix/prob-assign-row-count-to-a-variable/7875.html

Determine a script's execution dir? www.computing.net/answers/unix/determine-a-scripts-execution-dir/6717.html