problem passing arguments to script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem passing arguments to script
# 1  
Old 01-14-2009
problem passing arguments to script

Hi,
I am writing a script, which is invoked from other system using ssh.
I have problems reading the arguments passing to the script. If the argument has a space in it (ex "rev 2.00"), the script considers "rev" as 1 argument and "2.00" as another. Instead i want "rev 2.00" to be considered as a single argument.

Below is my script and sample output.

Code:
#myscript.sh

#! /bin/bash
     script=${0##*/}
     value1=$1
     value2=$2
     value3=$3

     echo "received parameters :"
     echo "script: $script, value1: $value1, value2: $value2 value3: $value3"

Output:
bash-3.00$ ssh root@192.168.10.121 /usr/local/sbin/myscript "Rev_1" "Rev 2" "Rev3"
root@192.168.10.121's password:
received parameters :
script: myscript, value1: Rev_1, value2: Rev value3: 2

desired output:
script: myscript, value1: Rev_1, value2: Rev 2, value3: Rev3
# 2  
Old 01-14-2009
Try putting quotes around it because of the whitespace.

Code:
$ cat myscript.sh
#! /bin/bash
     script=${0##*/}
     value1="$1"
     value2="$2"
     value3="$3"

     echo "received parameters :"
     echo "script: $script, value1: $value1, value2: $value2 value3: $value3"

$ ./myscript.sh "Rev_1" "Rev 2" "Rev3"
received parameters :
script: myscript.sh, value1: Rev_1, value2: Rev 2 value3: Rev3

# 3  
Old 01-14-2009
That did not help.. I am getting same output as before, even after your modification. To add, I am using bash shell, hope it is not shell dependent
# 4  
Old 01-14-2009
Try using the getopts command as described in this thread

This will allow you to specify an argument per switch and you will not have to worry about which field is in which variable.
Padow
# 5  
Old 01-14-2009
Quote:
Originally Posted by cjjoy
bash-3.00$ ssh root@192.168.10.121 /usr/local/sbin/myscript "Rev_1" "Rev 2" "Rev3"

It is very foolish to experiment with scripts using the root account. Execute as a regular user until the script is working properly, and even then only use root if really need root privileges.

Code:
$ ssh 192.168.0.101 "bin/try x 'y z' z"
chris@192.168.0.101's password: 
received parameters :
script: try, value1: x, value2: y z value3: z

Or:

Code:
ssh 192.168.0.101 'bin/try x "y z" z'


Last edited by cfajohnson; 01-14-2009 at 12:41 PM..
# 6  
Old 01-14-2009
Thanks Cris...It's working as desired..Smilie
Thank's for the suggestion for not experimenting things as root..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

C shell script passing arguments problem.

I found something insteresting when I tested passing arguments into my scripts. My scripts is as below. % cat passarg.env #!/bin/csh echo "passarg: argv = $argv argv = $argv" passarg1.env $* % cat passarg1.env #!/bin/csh echo "passarg1: argv = $argv argvp=$argv" set str = "test... (5 Replies)
Discussion started by: bestard
5 Replies

2. Shell Programming and Scripting

Passing arguments to php script

i want to be able to pass arguments to a php script if it is being piped: cat myphpscript.php | php - $1 $2 $3 blah blah This usually works for other script languages...i.e. ruby: cat myrubyscript.rb | ruby - $1 $2 $3 blah blah so my question is, how can i pass arguments to my php... (1 Reply)
Discussion started by: SkySmart
1 Replies

3. Shell Programming and Scripting

Passing arguments while running the script

Hi, I have a requirement for creating a MQ (queue) where the inputs has to be passed as arguments. Running the script as below ./hi.sh "Servername" "QueueManagername" "QueuecreationCommand" cat hi.sh echo "Welcome to $1" runmqsc $2 < $3 But the queue creation command is... (9 Replies)
Discussion started by: Anusha M
9 Replies

4. Shell Programming and Scripting

Passing arguments to a bash script

Hi, I wanted to pass an argument to a bash script. So that the argument is used inside the awk command inside the bash script. I know the noraml way of passing argument to a bash script as below : sh myScript.sh abc Inside the bash script i can use like this myArg1=$1 wc $myArg But... (8 Replies)
Discussion started by: shree11
8 Replies

5. Shell Programming and Scripting

Passing arguments to a perl script

Hi I need to pass comma seperated arguments to a perl script? It is like: Exect.pl -d GUI1,GUI2,GUI3 and I need to store these argsGUI1,GUI2,GUI3 in an array. can anyone suggest how to do that: (1 Reply)
Discussion started by: rkrish
1 Replies

6. Shell Programming and Scripting

passing arguments to external script

Hi! I have a python script that requires arguments and these arguments are file paths. This script works fine when executed like this: /my_python_script "file_path1" "file_path2" (i added quotes as some file names may have weird characters) the issue happens when i launch my python script... (14 Replies)
Discussion started by: gigagigosu
14 Replies

7. Shell Programming and Scripting

Problem passing arguments to a Python script

I have part of the script below and I tried calling the script using ./tsimplex.py --fstmod=chris.cmod --nxz=8x6 --varp=0.25 but am getting the error option --fstmod must not have an argument Any idea on how to fix this would be highly appreciated #! /usr/bin/python import... (0 Replies)
Discussion started by: kristinu
0 Replies

8. UNIX for Advanced & Expert Users

Passing blank arguments to a script

All, I have a cron job script that receives several command line arguments. At some point if there are validation problems and the job cannot be run, it duplicates the entire command line into a temporary text file which is later executed as a script. Unfortunately when I pass the list of received... (7 Replies)
Discussion started by: rm-r
7 Replies

9. Shell Programming and Scripting

Passing arguments to a Perl script

I am playing around with Perl and wrote the script below that is executed from the command line, it will split data up in a file based on a value supplied. When executed you provide two arguments - the file that contains the data to be split and the character you want to split by. It works as... (4 Replies)
Discussion started by: jyoung
4 Replies

10. Shell Programming and Scripting

Passing arguments to a script

I've written a script (bgrep) for a more advanced grep command (& attached a cut down version below). I'm trying allow all grep options to be used, or in any combination. The script works fine if I type say bgrep -i -files product it will return a non-case sensitive list of matches for... (3 Replies)
Discussion started by: Kevin Pryke
3 Replies
Login or Register to Ask a Question