gpt4 book ai didi

bash - 阻止bash脚本在终端中输出

转载 作者:行者123 更新时间:2023-12-02 07:34:23 26 4
gpt4 key购买 nike

我相信我为if else语句正确设置了所有内容,但是它始终将内容输出到我的shell终端中,就像我自己运行命令一样。无论如何,我可以逃避这个问题,所以我可以运行这些命令,而无需在终端上填充结果文本?

#!/bin/bash
ps cax | grep python > /dev/null
if [ $? -eq 0 ]; then
echo "Process is running." &
echo $!
else
echo "Process is not running... Starting..."
python likebot.py &
echo $!
fi

这是运行我的bash脚本几分钟后的输出结果
[~]# sh check.sh
Process is not running... Starting...
12359
[~]# Your account has been rated. Sleeping on kranze for 1 minute(s). Liked 0 photo(s)...
Your account has been rated. Sleeping on kranze for 2 minute(s). Liked 0 photo(s)...

最佳答案

如果要从shell脚本中重定向输出,请使用exec:

exec 1>/dev/null 2>&1

从现在开始,这将重定向所有内容。如果要输出到日志:
exec 1>/tmp/logfile 2>&1

追加日志:
exec 1>>/tmp/logfile 2>&1

要备份句柄,以便您可以还原它们:
exec 3>&1 4>&2
exec 1>/dev/null 2>&1

# Do some stuff

# Restore descriptors
exec 1>&3 2>&4

# Close the descriptors.
exec 3>&- 4>&-

如果脚本中有特定的部分,您希望保持静音:
#!/bin/bash

echo Hey, check me out, I can make noise!

{
echo Thats not fair, I am being silenced!
mv -v /tmp/a /tmp/b

echo Me too.
} 1>/dev/null 2>&1

关于bash - 阻止bash脚本在终端中输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18334889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com