sub arguments to shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sub arguments to shell script
# 1  
Old 09-12-2012
sub arguments to shell script

Hi,

I have a shell script, when run it i get a prompt to enter arguments say 1 for doing my next task otherwise q for quit.
What I am trying to do is run the shell script with the argument passed in however it does not seem to work.
This is what I did
./test.sh 1
Instead it printed the line to enter the command

test.sh

....some functions
...

Print "1) all
Print "q) quit

Print
Print-n "Command:

read reply
case $reply in
1*) while fun
do
:
done
return 0 ;;

q*) return 1;
esac

...
So entering 1 directly should allow me to go to the function. I tested the shell by entering no args and enter 1 at the command prompt or q to quit it works not directly


Any help is appreciated.
# 2  
Old 09-12-2012

The argument is stored in the positional parameter $1; it is not placed on the standard input.

You need something like:
Code:
if [ -n "$1" ]
then
   reply=$1
else
   printf "%s\n" "1) all" "q) quit"
   read reply
fi

# 3  
Old 09-12-2012
Only do the read reply if no arguments are passed:

Code:
if [ $# -eq 0 ]
then
    Print "1) all
    Print "q) quit
 
    Print
    Print-n "Command:
 
    read reply
else
    reply=$1
fi

Note: $# is the count of the number of arguments passed (0 for none, 1 for one, etc).

edit: @cfajohnson - beat me to it.
# 4  
Old 09-12-2012
Quote:
Code:
[...]
Print "1) all
Print "q) quit

Print
Print-n "Command:

read reply
case $reply in
[...]

Try changing the script this way (I guess you're using ksh):

Code:
#!/bin/ksh
reply="$1"                                           ### first line to add
[...]
[[ -n "$reply" ]] || {                             ### second line to add
Print "1) all
Print "q) quit

Print
Print -n "Command:

read reply
}                                                    ### third line to add
case $reply in
[...]

--
Bye
# 5  
Old 09-12-2012
Hello All,
Thank you for replying.
Unfortunately it is a vendor script that I cannot tweak. In order to understand why the script did not work I broke it down to my test script.

I will still try out your recommendations so I get more understanding as I am totally new to perl scripting.

Here goes my next question.
I have a framework that uses jsch (jcroft) to which I have been able to pass my grep commands etc to get some details from the log files and then displaying it on the ui.
So I was hoping to use the same framework by passing the shell script along with the arguments #1-23 and capturing some sla related messages tied to the different number inputs as args directly in the business logic without prompting the user from the ui for any input. Is there a way I can do that ? Don't know even I have the user provide me the input arg if it would work? Have to think more on those lines.
Thank you.
# 6  
Old 09-12-2012
Quote:
Originally Posted by aqua9
Hello All,
Thank you for replying.
Unfortunately it is a vendor script that I cannot tweak. In order to understand why the script did not work I broke it down to my test script.

Then pipe your argument to the script:
Code:
echo 1 | test.sh

Quote:

I will still try out your recommendations so I get more understanding as I am totally new to perl scripting.

Perl??? Who said anything about perl?

This is a shell script.
# 7  
Old 09-12-2012
Sorry typo there.

---------- Post updated at 09:47 PM ---------- Previous update was at 09:43 PM ----------

I tried echo 1 | test.sh ,noticed it kept going into an infinite loop of trying to display the next lines of code and eventually stopped at the Command line :
however echo q | test.sh did quit the script as expected...

---------- Post updated at 10:28 PM ---------- Previous update was at 09:47 PM ----------

-added some more detail on the function main_screen i have

$all_queues populates values from anoter folder /queue/queue_1 , /queues/queue_2 etc.

upon running the script i get the above values with number 1 -23 for each queue based on which i made my selection of 1 or 2 at the Command prompt and got the next output

--test.sh
Code:
main_screen()
{
typeset count
typeset space
typeset device
typeset list
typeset tmp
 
clear
 
while :
do
print "View queue space ..."
print
 
count=0
for device in $all_devices
do
eval set -- \$device_$device
 
for space in $*
do
eval "tmp=\"\$pages_$space\""; 
printf "%2d - %16s : %-20s %6d pages\n" \
$((count+=1)) $device $space $tmp
list="$list $device:$space"
done
done
print
print "1)all - affect all queues"
print "q)uit - Return to main menu"
print
print -n "Command: "
 
read reply
 
case $reply in
a* ) while operate_all
do
: 
done 
 
return 0 ;;
q* ) return 1 ;;
esac
 
set -- $list
 
reply=$(eval print \${$reply} 2>/dev/null)
 
if [[ -n $reply ]]
then
device=${reply%%:*}
space=${reply##*:}
 
while operate_space $device $space 
do
:
done
 
return 0
else
clear
print "\nInvalid selection - please re-enter\n"
fi
done
}


Last edited by Franklin52; 09-13-2012 at 05:26 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

The shell script doesn't get arguments, but parse them

Hello, I have a simple shell script, which starts from crontab like this: 00 03 * * 2-6 /export/applications/dte/sh/fwmarg.sh > /export/applications/dte/data/cron_log/fwmarg.cronlog.`date +\%m.\%d` 2>&1 The script doesn't get any argument. But inside it I see the line ... (10 Replies)
Discussion started by: digioleg54
10 Replies

2. Shell Programming and Scripting

Pass Arguments to Command from Shell Script

Hi all, I am working on a project, in which I have to connect to Bluetooth low energy device. I am able to connect and do data transfer from command line. But I want to do from script Here is my script #!/bin/bash #sudo hcitool -i hci0 lescan sleep 1 sudo hcitool -i hci0 lecc --random... (8 Replies)
Discussion started by: nithin@embdes
8 Replies

3. Shell Programming and Scripting

How can multiple arguments be passed to shell script?

My requirement is that I want to pass similar argument to a shell script and process it in the script. Something like below: myScript.sh -c COMPONENT1 -c COMPONENT2 -a APPNote: -c option can be specified multiple times and -a is optional parameter I know this can be achieved using... (2 Replies)
Discussion started by: rajdeep_paul
2 Replies

4. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

5. Shell Programming and Scripting

Using arguments in Shell script

Hello, I have to make a shell script doing that : the program tests if there is an argument, if there is it checks whether this is a directory or not, If it is it opens it. for any .c file in the directory it prints 2 lines in the screen : the dependence line of the .o and compiler commend... (1 Reply)
Discussion started by: dekl
1 Replies

6. Shell Programming and Scripting

How to make shell script arguments optional?

Here is my script: #!/bin/ksh usage () { echo " Usage: $0 <opt1> <opt2> <opt3> <opt4>" } if ; then usage exit; fi prog -a $1 -b $2 -c $3 -d $4 2>&1 | tee -a ~/$1.log I want argument 4 to be optional, so if there's no argument for opt4, that it doesn't... (8 Replies)
Discussion started by: guitarscn
8 Replies

7. Shell Programming and Scripting

no of arguments to function in shell script

Hi, I have a function in shell script fun1{ echo "No.of arguments are..."} this function will be called in same script by passing arguments fun 1 2 3 I want to check the no. of arguments passed to fun1 function in the same functionbefore validation. can any one suggest me. (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

8. Shell Programming and Scripting

Need help to pass arguments to shell script

Hi, I have a shell script called ftp.sh which is running continously in background. I tried passing arguments to this script but it did not worked out. Below is ftp.sh script. Please help me case $param in start) sleep_func "300" echo "!ksh $scr_ddir/ftp.sh... (1 Reply)
Discussion started by: bhargav20
1 Replies

9. Shell Programming and Scripting

Is there a limit to the no. of arguments to a shell script ?

What is the maximum no. of arguments that could be passed to a shell script ? Is there any restriction ? I've a requirement where I need to pass a list of names to a unix script and I guess the number of such names is not a fixed one. It can run into hundreds. Is this feasible ? (4 Replies)
Discussion started by: hidnana
4 Replies

10. Shell Programming and Scripting

Shell script with arguments

Hi All, I need some help/ideas in coming up with a shell script. Basically, the script should install 1 or 2 or 3 packages based on the input arguments. For example, if I type in pkgscript.sh a1 a2 a3, it should install all the 3 scripts and pkgscript.sh a1 should install only a1. If a... (3 Replies)
Discussion started by: sankar6254
3 Replies
Login or Register to Ask a Question