我有一個腳本,簡化為以下:
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 解釋器本身讀取的內容。
一個狹窄的(逐個命令)方法是重定向 apt-get 的標準輸入/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
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句