Linux 如果从脚本运行,VerScript安装脚本将失败

Linux 如果从脚本运行,VerScript安装脚本将失败,linux,bash,Linux,Bash,我是dokerising veracryp,需要在非交互模式下运行安装脚本。要运行脚本,我需要将脚本询问的答案传递给它(如果在交互模式下运行)。问题是,如果我通过命令管道传递答案,或者从文件中获得错误: echo -e "1\n\nq\ny\ny\n\n" | /opt/tdp/veracrypt-1.23-setup-console-x64 Verifying archive integrity... 100% All good. Uncompressing V

我是dokerising veracryp,需要在非交互模式下运行安装脚本。要运行脚本,我需要将脚本询问的答案传递给它(如果在交互模式下运行)。问题是,如果我通过命令管道传递答案,或者从文件中获得错误:

    echo -e "1\n\nq\ny\ny\n\n" | /opt/tdp/veracrypt-1.23-setup-console-x64 
    Verifying archive integrity...  100%  
    All good. Uncompressing VeraCrypt 1.23 Installer  100% 
    Error: Terminal required
如果我运行相同的脚本而不使用管道,那么脚本工作正常

我把问题缩小到这一行

问题在于,根据脚本是通过管道还是自身运行,代码行的计算方式有所不同,这一行:

为了解决这个问题,我创建了这个虚拟纸条

A=0
tty >/dev/null 2>/dev/null && A=1

echo "A is"
echo $A
echo
如果将此脚本命名为inst.sh并运行两次,无论是否使用管道,都会得到不同的结果

# echo xxx | ./inst.sh
A is
0

# ./inst.sh
A is
1
知道为什么吗?这可能帮助我解决VerScript问题(我无法更改VerScript安装脚本)


非常感谢

我使用(一个根据脚本与其他交互式程序“对话”的程序)实现了这种自动化

如果没有,您需要在docker映像中安装它

您需要创建一个expect脚本,其中包含每个脚本的问题提示和答案

安装vera crypt.exp
文件中:

#!/usr/bin/expect -f

set timeout -1
spawn /opt/tdp/veracrypt-1.23-setup-console-x64
expect "The text you expect to answer 1\r"
send -- "1\r"
expect "The text you expect to answer q\r"
send -- "q\r"

...

expect eof

然后您可以使用以下命令执行它:
expect-f install vera crypt.exp

我已经使用(是一个根据脚本与其他交互程序“对话”的程序)实现了这种自动化

如果没有,您需要在docker映像中安装它

您需要创建一个expect脚本,其中包含每个脚本的问题提示和答案

安装vera crypt.exp
文件中:

#!/usr/bin/expect -f

set timeout -1
spawn /opt/tdp/veracrypt-1.23-setup-console-x64
expect "The text you expect to answer 1\r"
send -- "1\r"
expect "The text you expect to answer q\r"
send -- "q\r"

...

expect eof

然后您可以使用以下命令执行它:
expect-f install vera crypt.exp

Gonzalo Matheu已经成功地回答了这个问题,非常感谢。我只是在这里发布了对我有用的文件稍加修改的版本

#!/usr/bin/expect -f

set timeout -1
spawn /opt/tdp/veracrypt-1.23-setup-console-x64
expect "To select, enter 1 or 2:"
send -- "1\r"
expect "Press Enter to display the license terms..."
send -- "\r"
expect ":"
send -- "q\r"
expect "Do you accept and agree to be bound by the license terms?"
send -- "yes\r"
expect "Press Enter to exit..."
send -- "\r"
expect eof

Gonzalo Matheu成功地回答了这个问题,非常感谢。我只是在这里发布了对我有用的文件稍加修改的版本

#!/usr/bin/expect -f

set timeout -1
spawn /opt/tdp/veracrypt-1.23-setup-console-x64
expect "To select, enter 1 or 2:"
send -- "1\r"
expect "Press Enter to display the license terms..."
send -- "\r"
expect ":"
send -- "q\r"
expect "Do you accept and agree to be bound by the license terms?"
send -- "yes\r"
expect "Press Enter to exit..."
send -- "\r"
expect eof

完整的文件实际上略有不同/usr/bin/expect-f
设置超时-1
生成/opt/tdp/veracrypt-1.23-setup-console-x64
expect”选择,输入1或2:“
发送--“1\r”
按enter键显示许可条款…”
发送--“\r”
发送--“q\r”
send--“是\r”expect“按Enter键退出…”send--“\r”expect eof`完整文件实际上略有不同。
!/usr/bin/expect-f
设置超时-1
生成/opt/tdp/veracrypt-1.23-setup-console-x64
expect”选择,输入1或2:“
发送--”1\r“
预期”按Enter键显示许可条款…”
发送--“\r”
预期”:“
发送--”q\r“
预期”您接受并同意受许可条款约束吗?
发送--”是\r“预期”按Enter键退出…”发送--“\r”预期eof”`