Sponsored Content
Top Forums Shell Programming and Scripting Passing Command Line Args in a Single Variable? Post 302685839 by mrm5102 on Monday 13th of August 2012 12:02:22 PM
Old 08-13-2012
Passing Command Line Args in a Single Variable?

Hello All,

I have a Bash Script and an Expect script that together will SSH to another server and
do some stuff there... From within the Bash Script I process the Command Line Arguments,
which are Required Args and Optional Args.

When I call the Expect script from the Bash Script, I pass it all the required Args as
there own variables: i.e. $ip_address, $username, $password, $option...

Then, as I process any Optional Arguments I want to concatenate them into one Variable. Doing this
will remove a whole bunch of If statements from my script that would, for example, if the optional
argument "--restart" was given then execute the Expect Script with all the Required Args then
add the Optional Arg "--restart" to the end of the line that executes the script:
Code:
/path/to/script/myExpect_Script $ip_address $username $password $option --restart

So I would have to do this sort of thing for each optional argument, which could get very messy
because most of the Optional Args can be used together, which means a bunch of different combinations of Args can be used,
leaving me with a bunch of if statements...


So now what I have is, as I am checking all the Args from the command line, in the Bash Script... If
I find for example the "--restart" option I would say:
Code:
exe_command_args="$exe_command_args --restart"

Then I find the "add" option, I do:
Code:
exe_command_args="$exe_command_args add"

Lastly, I give it this final Arg:
Code:
exe_command_args="$exe_command_args command=\"command[check_alias]=/path/to/exe/check_myCmd -w 1 -c 5\""

Now I only have to have one line that makes a Call to the Expect Script like this...
(fyi when I make the call to execute the Expect script I add one last required option to the end of the line):
Code:
### Call the Expect Script
/path/to/script/myExpect_script $ip_address $username $password $option $exe_command --sshed

Now, while in the Expect Script I did a simple loop to check all the Args passed in and it seems it's treating the
Args within the variable "$exe_command_args" as one command line argument.
Here's the Loop from the Expect Script, and the Output:
Code:
#!/usr/bin/expect -f

puts "IN EXPECT SCRIPT:\n- Checking Command Line Args..."

### Loop through "$argv", which contains all the Arguments passed in as a list...
    set x 0
    foreach cmd_arg $argv {
        set tmp [lindex $argv $x]
        puts "\tARG $x: $tmp"
        incr x
    }
exit 101





________OUTPUT________

IN EXPECT SCRIPT:
- Checking Command Line Args...
    LINE 0: 192.168.5.181
    LINE 1: Matt
    LINE 2: Password
    LINE 3: add
    LINE 4:  command="command[check_alias]=/path/to/exe/check_myCmd -w 1 -c 5" comment="# COMMENT FOR NEW CHECK_COMMAND" --restart
    LINE 5: --sshed

So could there be anything I am doing wrong syntactically in the bash script when passing the
arguments? I was under the assumption that in order for an argument to be considered ONE arg,
if it contained any whitespace, that it needed to be quoted..?

And if I look at the Expect Output it seems to be quoted correctly and everything.


If anyone has anythoughts that would be great!
I really didn't want to have to change anything in the Expect Script of that way it assigns the
arguments to the variables...

***Sorry if I made this a little more confusing/complicated then it needed to be...

Thanks in Advance,
Matt
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing the command line argument in a variable

Hi, I am new to unix. Is their a way to pass the output of the line below to a variable var1. ls -1t | head -1. I am trying something like var1=ls -1t | head -1, but I get error. Situation is: I get file everyday through FTP in my unix box. I have to write a script that picks up first... (1 Reply)
Discussion started by: rkumar28
1 Replies

2. UNIX for Dummies Questions & Answers

required command line args

Hello, How do I make a command line argument required using getopts? Thanks. (6 Replies)
Discussion started by: yoi2hot4ya
6 Replies

3. Programming

Command line args

My program usage takes the form for example; $ theApp 2 "one or more words" i.e. 3 command line arguments; application name, an integer, some text My code includes the following 4 lines: int anInteger; char words; sscanf(argv, "%d", &anInteger); sscanf(argv, "%s", &message); Based... (2 Replies)
Discussion started by: enuenu
2 Replies

4. UNIX for Dummies Questions & Answers

command line args

I am trying to print command line arguments one per second. I have this while do echo "6" shift echo "5" shift echo "4" shift echo "3" shift echo "2" shift echo "1" shift done (2 Replies)
Discussion started by: skooly5
2 Replies

5. UNIX for Dummies Questions & Answers

command line args 2

I have this while loop and at the end I am trying to get it to tell me the last argument I entered. And with it like this all I get is the sentence with no value for $1. Now I tried moving done after the sentence and it printed the value of $1 after every number. I don't want that I just want... (2 Replies)
Discussion started by: skooly5
2 Replies

6. Shell Programming and Scripting

How to retrieve command line args one by on.

Hi, I have to store all the command line arguments into an array. I have the following code. ********************** #! /bin/sh set -A arr_no_updates i=1 while do arr_no_updates=$($i) echo ${arr_no_updates} i=$(($i+1)) done**************** (1 Reply)
Discussion started by: little_wonder
1 Replies

7. Shell Programming and Scripting

command line args in unix

Hi, i have a perl script named test.pl. It is executed as cat *.log|test.pl i need the complete command line args. I tried using basename $0 but im getting test.pl only but not cat *.log... Can anyone help me on this. Thanks in advance (3 Replies)
Discussion started by: niteesh_!7
3 Replies

8. Emergency UNIX and Linux Support

Cut | command line args

Hi, Can you please hint me how to achieve the below? Input: $./script.sh start 1 2 Internally inside the script i want to set a single variable with $2 and $3 value? Output: CMD=$1 ARGS=$2 $3 --VInodh (10 Replies)
Discussion started by: vino_hymi
10 Replies

9. Shell Programming and Scripting

Reading of variable in a single line command

Hi All, Below is a sample command that I can run without any problem in the command line. Command Line dtToday=`date +%Y%m%d`; ls -ltr ./filename_${dtToday}.txt -rw-r--r-- 1 monuser oinstall 0 Jan 18 11:02 ./filename_20130118.txt But once I put that command line in file (list.txt) and... (3 Replies)
Discussion started by: padi
3 Replies

10. Shell Programming and Scripting

Passing the value of variable which is read from command line in called script

Hi, I am calling a Perl script in my shell script. When Perl script is executed it asks for a answer to be entered by user from terminal. How can i pass that value from my shell script ?? I know I can change perl script to default the answer but i dont have access to do that so only option i... (5 Replies)
Discussion started by: varun22486
5 Replies
All times are GMT -4. The time now is 08:30 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy