shell脚本中取消重定向的方法实例
在上一篇关于shell编程的例子中,有讲到把shell脚本进行扩展之后重定向到一个文件中,以便进行查看和调试https://www.nhooo.com/article/62435.htm。但是,若是有另一种情况:只是在某些地方进行重定向,而其他地方不进行重定向。那么我们就来修改一下上一篇中的例子来进行这种需求的满足:
#!/bin/bash functionsetlogfile { if![-z"$1"];then echo"logfilenameisnotempty!">>kthh exec3>&1 exec4>&2 exec2>>$1 exec1>>$1 fi }
num1=$1 logfile=$2 execlogfile=$3
setlogfile${execlogfile} set-x if[$num1-eq0];then echo"num1is0">>${logfile} elif[$num1-ge0];then echo"num1isgrate0">>${logfile} else echo"num1isless0">>${logfile} fi exec2>&4 exec1>&3 if[$num1-eq0];then echo"num1is0again">>${logfile} fi