Expect script causing Invalid Switch message

Dan Kaib
Dan Kaib used Ask the Experts™
I have a script that works on a local system.
When I try to execute that script from an expect script using ssh on another system, I get an Invalid Switch message on the following statement:
sort -ru oldfile > newfile

The spawn statement from the expect script is as follows:
spawn ssh "$USER@$IP" "$CMD" "$PARAMS"
This is running from Cygwin.

Is there something in expect or ssh that is preventing the switches on remote command execution to be recognized?

Any suggestions would be greatly appreciated.


Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Could'nt you post a small example script to be sure, that there'sno mixup

for example.

what did you set CMD to
and what did you set PARAMS to?
your problem is very probbaly an escaping issue.

let's assume the command you type on your localhost is
sort -ru oldfile > newfile

if you want to run this command via ssh you had to type
ssh user@host "sort -ru oldfile > newfile"   # assuming newfile should be created on the remote host
then if you use tcl / expect
spawn ssh "user@host" "\"sort -ru oldfile > newfile\""


Hi gelonida,
Thanks for the reply.
Attached are the 3 scripts involed:
Script 1 is run_on_corp.sh - This is run locally from the managers workstation, it executes Script 2 run_ssh.
Script 2 is run_ssh - This is an expect script that issues the SSH command to execute script copy_from_reg.sh on the remote corporate system.
Script 3 is copy_from_reg.sh - This script goes out to different store registers and downloads files via scp from the register to the corporate system.  
This is done from the managers workstation because there are over 100 locations and the corporate system could not process the 100 locations in a timely manner if it ran them individually.  Plus the 100 locations end there day at different time.
Script 3 copy_from_reg.sh has the sort command that is not working in it.


as said in my second post probably an escaping issue.

Very first comment about

lwithout knowing the contents of the variables
$datemp and $dalist

I would write
sort -ru "${datemp}" > "${dalist}"

I do not have time to dive through your entire code (it's not really a small example)
I'd suggest to either simplfy your output or to try to give some more info:

For Example:

In line 80 of run-ssh you have
    spawn ssh "$USER@$IP" "$CMD" "$PARAMS"

could you please add these above the spawn line
puts "USER <$USER>"
puts "PASS <$PASS>"
puts "IP <$IP>"
puts "CMD <$CMD>"
and then tell me its output (DON'T POST OF COURSE  USERNAME AND PASSWORD).
That's just for you for sanity checking

in run_on_corp you have in line 101
 ${sshcmd} ${ftpuser} ${ftppass} ${ftpip} "$ftpcmd" ${params}
This line does not look good. as you might have blank characters in the password.
I would replace it with
 ${sshcmd} "${ftpuser}" "${ftppass}" "${ftpip}" "$ftpcmd" "${params}"

could you then please add following lines above

echo "sshcmd <$sshcmd>"
echo "ftpuser <$ftpuser>"  # i'm not interested in it's exact output it's enough you tell me
                                # it's really the user name and whether any special characters are in the string
echo "ftppass <$ftppass>" # same as for line above
echo "ftpip <$ftpip>" # #
echo "ftpcmd <$ftpcmd>"
echo "params <$params>"

Just check whether all values are passed as expected or whether anything looks different in the two scripts

in copyfromreg I don't understand lines 31 / 32
I'm not sure, but I would have written them

sshcmd="bash -c \"cd ${frdir}"
sshcmd="${sshcmd};cp /dev/null ${livfl}"
sshcmd="${sshcmd};ls --time-style=+%Y%m%d_%H%M%S -l"

instead of
sshcmd="bash -c \"cd ${frdir}"
sshcmd=${sshcmd}";cp /dev/null ${livfl}"
sshcmd=${sshcmd}";ls --time-style=+%Y%m%d_%H%M%S -l"

the difference is the initial double quote in the second and third line.

ame in line 35

I would also add
echo "<$sshcmd>" in line 232 to be sure, this is really what you want to have as ssh command

I can't see recv_ssh . without knowing escaping issues might also be there.


lwithout knowing the contents of the variables

With the sort command escaped in copy_from_reg.sh as follows:
"\"sort -ru ${datemp} > ${dalist}\""

Below is the error message:
/cygdrive/x/netshares/fiscal_files/bin/copy_from_reg.sh: line 254: "sort -ru /cygdrive/x/netshares/fiscal_files/text2pdf/st0044/work05/datetemp.txt > /cygdrive/x/netshares/fiscal_files/text2pdf/st0044/work05/datelist.txt": No such file or directory

With the sort command escaped in copy_from_reg.sh as follows:
 \"sort -ru ${datemp} > ${dalist}\"

Below is the error message:
/cygdrive/x/netshares/fiscal_files/bin/copy_from_reg.sh: line 254: "sort: command not found
Plus it created the following file with the double quote are part of the filename.
-rw-r--r--+ 1 Administrator None     0 2010-07-02 13:54 datelist.txt"

With the sort command escaped in copy_from_reg.sh as follows:
 sort -ru "${datemp}" > "${dalist}"

Below is the error message:
Invalid switch.
run_ssh produces the following output:
USER <xxxxxxx>      (This contained the correct information)
PASS <ppppppp>      (This contained the correct information)
IP <gmfp1>
CMD </cygdrive/x/netshares/fiscal_files/bin/copy_from_reg.sh>
PARAMS <v.0044>

Below is the variable ouput for the spawn command:
spawn ssh xxxxxxx@gmfp1/cygdrive/x/netshares/fiscal_files/bin/copy_from_reg.sh v.0044
With your changes made to run_on_corp the following output results:
sshcmd </home/Administrator/bin/run_ssh>
ftpuser <xxxxxxx>
ftppass <ppppppp!>
ftpip <gmfp1>
ftpcmd </cygdrive/x/netshares/fiscal_files/bin/copy_from_reg.sh>
params <v.0044>

I'm going to reduce the scripts to a bare minimum of commands to make it easier to follow.
I'll add a new comment shortly.


I renamed run_on_corp.sh to scr1.sh
I renamed copy_from_reg.sh to scr3.sh
I eliminated the expect script run_ssh all together for this test and incorporated the ssh command directly in scr1.sh

This should be easier to follow.


Forgot to add scr3.sh on last comment.


I just manually executed the following command locally on the system that contains scr3.sh
$ ssh Administrator@gmfp1 /cygdrive/x/netshares/fiscal_files/bin/scr3.sh v.0044
Administrator@gmfp1's password:
Sorting file /cygdrive/x/netshares/fiscal_files/text2pdf/st0044/work05/datetemp.txt
Invalid switch.

So this is SSHing to itself and getting the same errors escaped or not.


I find this hard to believe, given the error message of Invalid switch but if I change the copy_from_reg.sh script to:
 /usr/bin/sort -ru "${datemp}" > "${dalist}"

It works perfectly.

I would have expected to see command not found or something of that nature as the error message not Invalid switch.

Thanks for all of your help, I do appreciate it.

hi scodhk.
apologies for not having reacted for to your post.
I wasn't online till now.

probably ${datemp}
contained a space character or a '-'


Neither do:
${datemp} =/cygdrive/x/netshares/fiscal_files/text2pdf/st0044/work05/datetemp.txt

Changing from:      sort -ru "${datemp}" > "${dalist}"
                    to:      /usr/bin/sort -ru "${datemp}" > "${dalist}"

Made this work.

The Invalid switch error message is misleading.

Thanks for your help,

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial