如何将输出重定向回显示单个命令

时间:2012-01-04 10:44:59

标签: linux shell command-line

在shell脚本中我有

LOG=/my.log
exec 1>>$LOG
exec 2>&1

重定向shell脚本中的输出。现在问题在于以下

LOG=/etc/security/aixpert/log/aixpert.log
exec 1>>$LOG
exec 2>&1

#some codes

print "I want this on cmd output not in log"

#I want rest of the output redirected to log as usual

我该怎么做?

2 个答案:

答案 0 :(得分:4)

关键是将stdout克隆到控制台到任意fd(我选择3),然后再将其重定向到您的日志。每当您想要将输出发送到控制台时,您只需将fd 1重定向到fd 3,>&3用于该命令

LOG=/etc/security/aixpert/log/aixpert.log

exec 3>&1 >>"$LOG" 2>&1

#some codes

echo "I want this on cmd output not in log" >&3

#I want rest of the output redirected to log as usual

答案 1 :(得分:2)

# save stdout as fd 3
exec 3>&1
exec 1>>$LOG
exec 2>&1

echo foo >&3  # output to old stdout
echo bar      # output to logfile