简体   繁体   English

如何在bash脚本中将参数传递给内联期望?

[英]How to pass arguments to inline expect in a bash script?

I'm creating a shell script to automate the installation of a software which has it's own installer script, but I don't want prompts to the user. 我正在创建一个Shell脚本来自动化具有它自己的安装程序脚本的软件的安装,但是我不希望提示用户。 So I'm using expect to answer to said script. 因此,我正在使用Expect回答上述脚本。 Right now I have the following: 现在,我有以下内容:

expect $package -c 'set timeout -1
set package [lindex $argv 0]
spawn sh install_pipeline
expect "Where should I install the software packages ? *"
send "/usr/local/$package\r"
expect "Where should I install the pipeline calibration files ? *"
send "/usr/local/$package\r"
expect eof'

Where package is a variable containing the name of the module to be installed. 其中package是一个变量,其中包含要安装的模块的名称。 The problem is, this script doesn't work and prints a message saying: "Couldnt read file "name of the package": no such file or directory. 问题是,此脚本不起作用,并显示一条消息:“无法读取文件“软件包名称”:没有这样的文件或目录。

How can I pass my variable to the expect script? 如何将变量传递给期望脚本? I don't want to create a separate script for expect to keep things simple. 我不想创建一个单独的脚本来使事情保持简单。

Your expect command is surrounded with single quotes. 您的expect命令用单引号引起来。 Variables inside can not be expanded by the shell. shell不能扩展其中的变量。

Try this : 尝试这个 :

expect "$package" -c "set timeout -1
set package [lindex $argv 0]
spawn sh install_pipeline
expect \"Where should I install the software packages ? *\"
send \"/usr/local/$package\r\"
expect \"Where should I install the pipeline calibration files ? *\"
send \"/usr/local/$package\r\"
expect eof"

Or if you want to keep your single quotes : 或者,如果您想保留单引号:

expect "$package" -c 'set timeout -1
set package [lindex $argv 0]
spawn sh install_pipeline
expect "Where should I install the software packages ? *"
send "/usr/local/'"$package"'\r"
expect "Where should I install the pipeline calibration files ? *"
send "/usr/local/'"$package"'\r"
expect eof'

Based on Kenavoz answer, I found the solution. 根据Kenavoz的答案,我找到了解决方案。 As he stated, my code was encased by simple quotes, so expect couldn't get the variables from the main script. 正如他所说,我的代码用简单的引号引起来,所以期望无法从主脚本中获取变量。 As such, putting it with only normal quotes solved the problem, without the need to pass the variable as an argument: 这样,只用普通引号将其解决即可,而无需将变量作为参数传递:

expect -c "set timeout -1
spawn sh install_pipeline
expect \"Where should I install the software packages ? *\"
send \"/usr/local/$package\r\"
expect \"Where should I install the pipeline calibration files ? *\"
send \"/usr/local/$package\r\"
expect eof"

You could pass it in through the environment: 您可以将其传递给环境:

env pkg="$package" expect -c '
    set timeout -1
    spawn sh install_pipeline
    expect "Where should I install the software packages ? *"
    send "/usr/local/$env(pkg)\r"
    expect "Where should I install the pipeline calibration files ? *"
    send "/usr/local/$env(pkg)\r"
    expect eof
'

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

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