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:
/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:
exe_command_args="$exe_command_args --restart"
Then I find the "add" option, I do:
exe_command_args="$exe_command_args add"
Lastly, I give it this final Arg:
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):
### 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:
#!/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