è¿ç¯æç« ä¸»è¦ä»ç»äºshellèæ¬ä¸åæ¶éå®åçæ¹æ³å®ä¾,æ¬æç´æ¥ç»åºä»£ç å®ä¾,éè¦çæåå¯ä»¥åèä¸
å¨ä¸ä¸ç¯å
³äºshellç¼ç¨çä¾åä¸ï¼æ讲å°æshellèæ¬è¿è¡æ©å±ä¹åéå®åå°ä¸ä¸ªæ件ä¸ï¼ä»¥ä¾¿è¿è¡æ¥çåè°è¯http://www.jb51.net/article/62435.htmãä½æ¯ï¼è¥æ¯æå¦ä¸ç§æ
åµï¼åªæ¯å¨æäºå°æ¹è¿è¡éå®åï¼èå
¶ä»å°æ¹ä¸è¿è¡éå®åãé£ä¹æ们就æ¥ä¿®æ¹ä¸ä¸ä¸ä¸ç¯ä¸çä¾åæ¥è¿è¡è¿ç§éæ±ç满足ï¼
å¤å¶ä»£ç 代ç å¦ä¸:
#!/bin/bash
function setlogfile
{
if ! [ -z "$1" ]; then
echo "logfilename is not empty!" >> kthh
exec 3>&1
exec 4>&2
exec 2>> $1
exec 1>> $1
fi
}
num1=$1
logfile=$2
execlogfile=$3
setlogfile ${execlogfile}
set -x
if [ $num1 -eq 0 ]; then
echo "num1 is 0">> ${logfile}
elif [ $num1 -ge 0 ]; then
echo "num1 is grate 0">> ${logfile}
else
echo "num1 is less 0">> ${logfile}
fi
exec 2>&4
exec 1>&3
if [ $num1 -eq 0 ]; then
echo "num1 is 0 again">> ${logfile}
fi
å¨å½æ°setlogfileä¸æ·»å äºexec 3>&1ï¼exec 4>&2è¿ä¸¤å¥ï¼èå
¶ä¸ï¼3ã1ã4ã2é½æ¯è¡¨ç¤ºæ件æ述符ï¼1æ¯æ åè¾åºï¼ä¹å³æ¯STDOUT,2æ¯æ åé误è¾åºï¼å³æ¯STDERRï¼1å2æ¬æ¥æ¯è¾åºå¨æ¾ç¤ºå¨ä¸çï¼è3ã4æ¯è¡¨ç¤ºä¸¤ä¸ªæ件æ述符ï¼å¯ç解ææ¯ä¸¤ä¸ªä¿å1å2ç临æ¶åéï¼ 3>&1表示ææ件æ述符1çèµå¼ç»æ件æ述符3,ä¹å¯ä»¥ç解为æ¯ææ件æ述符1ï¼å½åæ åè¾åºï¼çè§åä¿åå°äºæ件æ述符3ä¸ï¼è¿æ¶åæ1çè¾åºéå®åå°æ件ä¸ï¼å¨ä¸é¢çshellç¨åºä¸ï¼æ们å使ç¨äº1>&3è¿å°±è¡¨ç¤ºæ¯æä¿åå¨æ件æ述符3ä¸çåæ åè¾åºçè§åéæ°èµå¼ç»æ件æ述符1,åæ¥æ åè¾åºçè§åæ¯è¾åºå°æ¾ç¤ºå¨ä¸ï¼èä¸æ¯è¾åºå°ä¸ä¸ªæ件ä¸ãè¿å°±æ¯æ´ä¸ªéå®åååæ¶éå®åè¿åçè¿ç¨ã2å4ä¹æ¯ä¸æ ·çéçã
é²è¯å°è¯´ï¼å
çè¿è¡ç»æï¼
å¤å¶ä»£ç 代ç å¦ä¸:
[root@UFO shellprogram]# ./testexecutelog.sh 0 msglog execlog
+ exec
+ '[' 0 -eq 0 ']'
+ echo 'num1 is 0 again'
[root@UFO shellprogram]# cat execlog
+ '[' 0 -eq 0 ']'
+ echo 'num1 is 0'
+ exec
çå°ç»æäºåï¼ï¼ï¼ 没çå°ä¹ï¼ï¼ï¼ é£å°±ä»ç»çäºï¼^_^