Patil Prashanth
Patil Prashanth

Reputation: 1941

can't read no such variable while executing shell command inside expect script

I have the code below. The goal is to

  1. get the latest file which matches certain starting characters
  2. to scp that file

 

#!/usr/bin/bash
#!/usr/bin/expect -f
ztools=$(ls -t|find -name '/home/user/releases/ztools*.tar.gz'|head -n1)
ztools=$(echo $ztools | cut -c 3-)

# connect via scp and transfer ztools
spawn scp /home/user/releases/${ztools} [email protected]:/home/user
#######################
expect {
-re ".*es.*o.*" {
exp_send "yes\r"
exp_continue
}
-re ".*sword.*" {
exp_send "Password\r"
}
}
interact

But when I run this I get the below error can any one help me on this

can't read "(ls -t|find -name '/home/zadmin/releases/ztools*.tar.gz'|head -n1)":    no such variable
while executing
"ztools=$(ls -t|find -name '/home/zadmin/releases/ztools*.tar.gz'|head -n1)"
(file "copy_ztools_cust.sh" line 2)

Upvotes: 1

Views: 6036

Answers (1)

Etan Reisner
Etan Reisner

Reputation: 80921

You appear to have combined two scripts there. One shell script and one expect script.

This line is the shebang line for a shell script

#!/usr/bin/bash

This line is the shebang line for an expect script

#!/usr/bin/expect -f

These lines are shell code

ztools=$(ls -t|find -name '/home/user/releases/ztools*.tar.gz'|head -n1)
ztools=$(echo $ztools | cut -c 3-)

These lines are expect/tcl code

# connect via scp and transfer ztools
spawn scp /home/user/releases/${ztools} [email protected]:/home/user
#######################
expect {
    -re ".*es.*o.*" {
        exp_send "yes\r"
        exp_continue
    }
    -re ".*sword.*" {
        exp_send "Password\r"
    }
}
interact

You need to split those lines back into separate bits of code if you expect this to work correctly.

Additionally those shell lines are not at all doing what you want and are not the proper way to go about that.

I don't believe that ls -t bit of the pipeline is doing anything for you as I don't believe find reads from standard input at all.

Do the ztools*.tar.gz filenames sort in a way that is useful to you or do you need to go by modification time of the file?

If the filenames sort alphabetically for you then what you want there is probably

ztools=(/home/zadmin/releases/ztools*.tar.gz)
ztools=${ztools[0]}

If the filenames don't sort alphabetically (but can be sorted using sort) then you probably want

ztools=$(printf '%s\0' /home/zadmin/releases/ztools*.tar.gz | sort -z)
ztools=${ztools[0]}

If the filenames can't be sorted using sort either and the modification times really are necessary then if your filenames are guaranteed to not contain any whitespace/newlines/glob metacharacters/etc. then you can use (but do note that this is not at all safe in the face of filenames that do)

ztools=$(ls -t | head -n1)

If you cannot be sure about the filenames you are going to deal with (and generally you shouldn't bet on it even if you believe you can) and modification times are still necessary then you are forced to use something more like in this answer.

Upvotes: 2

Related Questions