我正在编写一个脚本以通过 cmd“sudo/etc/bgenomics”(这是由我们的管理员设置)成为用户(让我们称之为基因组学)并以该用户身份运行一些 bash 代码,即运行一个 cmd,捕获退出代码并采取适当的措施。
问题是 here 文档中的 bash 代码被打印到屏幕上,这会让人分心,而且看起来真的很不雅观。
这是一个例子:
#!/bin/bash
name='George'
sudo /etc/bgenomics <<Q
/bin/bash
if (( 2 == 2 )); then
echo "my name is $name"
grep zzz /etc # will return nothing and $? = 1
echo \$? # this should be 1 after the above cmd
fi
Q
if 语句只是为了显示它在打印时有多烦人。
现在所有以下内容都打印到屏幕上:
Script started, file is /var/tmp/genomicstraces/c060644.20140617143003.11536
Script done, file is /var/tmp/genomicstraces/c060644.20140617143003.11536
brainiac-login-02$brainiac-login-02$/bin/bash
bash-3.2$ if (( 2 == 2 )); then
> echo "my name is George"
> grep zzz /etc # will return nothing and 0 = 1
> echo $? # this should be 1 after the above cmd
> fi
my name is George
1
我唯一想看的部分是“我叫乔治”和“1”。可以吗?
最佳答案
是否有另一个进程调用这个脚本?除非使用“-x”调用 bash,否则通常不会出现输出。如果您无法在调用过程中禁用 echo,请尝试修改脚本的第一行:
#!/bin/bash +x
您可能还想在 sudo 命令后删除对/bin/bash 的调用,除非您真的希望在您的 shell 中启动另一个 shell。
关于linux - Bash here 文档 - 禁止将代码打印到屏幕上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24270974/