当脚本在 shell 的标准输入上时,apt-get 之后的 shell 脚本中的命令未正确执行
Command in a shell script following apt-get not correctly executed when script is on shell's stdin
我有一个脚本,简化如下:
echo Step one...
apt-get install -y --reinstall ca-certificates
echo Step two...
对于这个用例,我不能直接 运行 它,它必须通过管道传输到 bash,如下所示:
cat script.sh | bash
问题显示在下面的输出中,其中 apt-get 命令(从第二个 echo 语句开始)之后的任何内容甚至都没有执行,而是显示了 echo 命令而不是执行了
我怎样才能完成这项工作?
# cat uu | bash
Step one...
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 509 not upgraded.
Need to get 0 B/166 kB of archives.
After this operation, 0 B of additional disk space will be used.
Preconfiguring packages ...
echo Step two...
(Reading database ... 131615 files and directories currently installed.)
Preparing to unpack .../ca-certificates_20170717~14.04.2_all.deb ...
Unpacking ca-certificates (20170717~14.04.2) over (20170717~14.04.2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up ca-certificates (20170717~14.04.2) ...
Processing triggers for ca-certificates (20170717~14.04.2) ...
Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
注意 echo Step two...
在 apt-get
仍在执行时如何显示在输出中,而不是 运行 完成后作为命令显示。
这里的问题是 apt-get,或者它在重新配置这个特定包时启动的东西,正在消耗你只希望 bash 解释器本身读取的内容。
一种狭窄的(逐个命令)方法是从 /dev/null
:
重定向 apt-get 的标准输入
#!/usr/bin/env bash
echo Step one...
apt-get install -y --reinstall ca-certificates </dev/null
echo Step two... #^^^^^^^^^^
更通用的方法是将您的代码封装在一个函数中,并仅在脚本末尾调用该函数:
#!/usr/bin/env bash
main() {
echo Step one...
apt-get install -y --reinstall ca-certificates
echo Step two...
}
main
我有一个脚本,简化如下:
echo Step one...
apt-get install -y --reinstall ca-certificates
echo Step two...
对于这个用例,我不能直接 运行 它,它必须通过管道传输到 bash,如下所示:
cat script.sh | bash
问题显示在下面的输出中,其中 apt-get 命令(从第二个 echo 语句开始)之后的任何内容甚至都没有执行,而是显示了 echo 命令而不是执行了
我怎样才能完成这项工作?
# cat uu | bash
Step one...
Reading package lists... Done
Building dependency tree
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 509 not upgraded.
Need to get 0 B/166 kB of archives.
After this operation, 0 B of additional disk space will be used.
Preconfiguring packages ...
echo Step two...
(Reading database ... 131615 files and directories currently installed.)
Preparing to unpack .../ca-certificates_20170717~14.04.2_all.deb ...
Unpacking ca-certificates (20170717~14.04.2) over (20170717~14.04.2) ...
Processing triggers for man-db (2.6.7.1-1ubuntu1) ...
Setting up ca-certificates (20170717~14.04.2) ...
Processing triggers for ca-certificates (20170717~14.04.2) ...
Updating certificates in /etc/ssl/certs... 0 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d....done.
注意 echo Step two...
在 apt-get
仍在执行时如何显示在输出中,而不是 运行 完成后作为命令显示。
这里的问题是 apt-get,或者它在重新配置这个特定包时启动的东西,正在消耗你只希望 bash 解释器本身读取的内容。
一种狭窄的(逐个命令)方法是从 /dev/null
:
#!/usr/bin/env bash
echo Step one...
apt-get install -y --reinstall ca-certificates </dev/null
echo Step two... #^^^^^^^^^^
更通用的方法是将您的代码封装在一个函数中,并仅在脚本末尾调用该函数:
#!/usr/bin/env bash
main() {
echo Step one...
apt-get install -y --reinstall ca-certificates
echo Step two...
}
main