简体   繁体   English

在expect脚本内执行shell命令时无法读取此类变量

[英]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} user@xx.xx.xx.xx:/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)

You appear to have combined two scripts there. 您似乎在那里合并了两个脚本。 One shell script and one expect script. 一个shell脚本和一个expect脚本。

This line is the shebang line for a shell script 这行是shell脚本的shebang行

#!/usr/bin/bash

This line is the shebang line for an expect script 该行是期望脚本的shebang行

#!/usr/bin/expect -f

These lines are shell code 这些行是shell代码

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 这些行是Expect / tcl代码

# connect via scp and transfer ztools
spawn scp /home/user/releases/${ztools} user@xx.xx.xx.xx:/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. 另外,这些shell行根本无法满足您的要求,也不是实现此目标的正确方法。

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. 我不相信管道的ls -t位会为您做任何事情,因为我根本不相信find会从标准输入中读取数据。

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? ztools*.tar.gz文件名是否以对您有用的方式排序,或者您需要按文件的修改时间进行排序?

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 如果文件名不按字母顺序排序(但可以使用sort ),那么您可能想要

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. 如果文件名也不能使用sort ,并且确实需要修改时间,那么如果保证文件名不包含任何空格/换行符/ glob元字符/等。 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 . 如果您不确定将要处理的文件名(通常,即使您相信可以,也不要打赌),并且仍然需要修改时间,然后您不得不在此答案中使用更多类似的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM