ãã¤ãã使ãã¤ã¤ãç°¡åã«çµäºã³ã¼ããåå¾ãããã£ããã»ã»
command 2>&1 | tee command.log
ã¨ããã£ã¦ãcommand ã®æ¨æºåºåãç»é¢ä¸ã§ã確èªããªããããã°ã«ãåºåããããã¨ããã±ã¼ã¹ã¯å²ã¨ããã¨æãã¾ãã
ãã ãcommand ã®å®è¡ãæåãããã©ãããç¥ããããåç´ã«ãã¤ãã§ã¤ãªãã¦ãã¾ãã¨ãä¸çªæå¾ã®ã³ãã³ã(ãã®å ´å㯠tee)ã®çµäºã³ã¼ãããåããªããã ããã
ã¾ããã§ãç°¡åã«åå¾ã§ãããããã¨æã£ãããæå¤ã¨è©¦è¡é¯èª¤ãã¦ãã¾ã£ãã¨ãã話ã
åæç¥è
ã³ãã³ããã°ã«ã¼ãã³ã°ããããã«ã¯ã丸æ¬å¼§ã()ãã¨ä¸æ¬å¼§ã{}ããå©ç¨ã§ãã¾ãã
- 丸æ¬å¼§ã§ã°ã«ã¼ãã³ã°ãããã®ã¯ããµãã·ã§ã«(åããã»ã¹)ã§å®è¡ããã
- ä¸æ¬å¼§ã§ã°ã«ã¼ãã³ã°ãããã®ã¯ãã«ã¬ã³ãã·ã§ã«(èªããã»ã¹)ã§å®è¡ããã
ã¨ããéããããã¾ããä¾ãã°ã
hoge=0 (hoge=1) echo $hoge # ->ã0ããåºåãããã(åããã»ã¹ã§å¤æ´ãã¦ãèªããã»ã¹ã«ã¯å½±é¿ãä¸ããªã)
hoge=0 { hoge=1; } echo $hoge # ->ã1ããåºåãããã(åä¸ããã»ã¹ã§å®è¡ãã¦ããã®ã§å¤æ´ããã)
ã¨ãããã¨ã«ãªãã¾ãã
試ããæ¹æ³
ã«ã¬ã³ãã·ã§ã«ã§å®è¡ãã¦ãçµäºã³ã¼ããå¤æ°ã«ä¿åããã°ããã ãããããã¨æã£ã¦å¯¾è©±ã·ã§ã«ã§è»½ã試ãã¦ã¿ãã
{ command; status=$?; }; echo $status
åä½ç¢ºèªOKã ã£ãã®ã§ãã¹ã¯ãªãããã¡ã¤ã«ã«ä»¥ä¸ãè¨è¿°ã
{ command; status=$?; } 2>&1 | tee command.log if [ $status -eq 0 ]; then # ä½ãã³ãã³ã fi
å®è¡ããã¨ãtestã³ãã³ã([ $status -eq 0 ]ã®é¨å)ã®å¼æ°ã足ãã¦ãªãããã£ã¦ã¨ã©ã¼ãã
ãsh -x <ã¹ã¯ãªãããã¡ã¤ã«>ãã§å®è¡ããã¨ã確ãã«statuså¤æ°ã空ã«ãªã£ã¦ããã¿ããã*1
å度ã対話ã·ã§ã«ã§ãªãã¤ã¬ã¯ãããã¤ãã追å ãã試ãã¦ã¿ãã
{ command; status=$?; } 2>&1 | tee command.log echo $status
ããï¼ã¡ããã¨åãããããï¼Windowsã®ã³ãã³ãããã³ããã¿ããã«ã対話ã·ã§ã«ã¨å®è¡æã§åããéããã ã£ãï¼ï¼
ã¡ãã£ã¨æ··ä¹±ãã»ã»ã»æ°ã¥ãã¾ããã対話ã·ã§ã«ã¯bashã§å®è¡ãã¦ã¦ãã¹ã¯ãªãããã¡ã¤ã«ã®å®è¡ã¯å¤ããã /bin/sh(Bourne Shell)ã§ããããããã°ãBourne Shellã§ã¯ä¸æ¬å¼§ã{}ãã§å®è¡ãã¦ãããªãã¤ã¬ã¯ããå©ç¨ããã¨ãµãã·ã§ã«ã§å®è¡ããã¦ãã¾ããã ã£ãã*2
bash(ãªã©æè¿ã®ã·ã§ã«)ã§ã¯ããã®åããæ¹åããã¦ããããªãã¤ã¬ã¯ãã使ã£ã¦ãã«ã¬ã³ãã·ã§ã«ã§åä½ããããã«ãªã£ã¦ããããã§ããbashãå©ç¨ã§ããã°ããã£ããã ãã©ãä»åã¯è²ããªç°å¢ã§åä½ããããã£ãã®ã§Bourne Shellã®å©ç¨ãåæã§ããç¡å¿µã
念ã®ãã
{ command 2>&1; status=$?; } | tee command.log echo $status
ã¨æ¨æºã¨ã©ã¼åºåã®ãªãã¤ã¬ã¯ããä¸ã«å
¥ãã¦ã¿ããããã¤ãã¯ãªãã¤ã¬ã¯ã·ã§ã³ã¨åæ§ã«æ±ãããã®ã§ããã¯ãstatusã¯åå¾ã§ããããããªçãã¯ãªãã£ãããã§ãã
ãã¤ãã使ãã¤ã¤ã(å¤å°æéãããã¦)çµäºã³ã¼ããåå¾ããæ¹æ³
- ãã¡ã¤ã«ãã£ã¹ã¯ãªãã¿ãæ´»ç¨ãã (é©å½ã«æ¸ããã®ã§ééã£ã¦ããå¯è½æ§é«ã)
status=`exec 3>&1; { command 2>&1 3>&-; echo $? 1>&3; } | tee command.log 1>&2 3>&-` echo $status
ç¥ã£ã¦ãã人ãè¦ãã°åããã®ã§ãããããããªããã¼ã§ã¡ã³ããã³ã¹æ§ã¯ãä¸è¾ã«ãããã¨ã¯è¨ããªãã
- ãã¡ã¤ã«ã«çµäºã³ã¼ããæ¸ãããã§ãã¾ã*3
status_file=/tmp/status.$$ # ä¸æ¬å¼§ãããªãã¦ä¸¸æ¬å¼§ã§ãµãããã»ã¹ã§åä½ããã¦ãå ¨ç¶OKã (command; echo $? >$status_file) | tee command.log status=`cat $status_file` rm -f $status_file
ãããªè¤éã§ããªã(ãããç°¡åã¨è¨ãã)ãããããããã¡ã¤ã«ãä»ãã®ãå¤å°åé·ããªã¨ããã ãæ±ç¨æ§ãé«ãããªã¢ã¼ãã·ã§ã«ã使ã£ã¦ããå ´åã«ãå¿ç¨ã§ãããã¯ããã¯ã§ãã
ãã¯ããããã使ãã®ãç¾å®è§£ããããã¾ãããããªãããä»ã«(Bourne Shellã§ãã£ã¨ç°¡åã«ã§ãã)ããæ¹æ³ã¯ãªãããã§ããããã
*1:ãã®å ´åãstatuså¤æ°ã«ç¢ºå®ã«å¤ãå ¥ã£ã¦ãããã¨ãåæãªã®ã§ãã¨ã©ã¼ãåé¿ããããã«"$status"ã®ããã«ã¯ã©ã¼ããã¦ãæå³ããªããã¨ããã[ "" -eq 0 ] ã¯ãtrue(0)ã¨ã¿ãªããã¦ãã¾ãã®ã§éå¹æã
*2:ãã®æã§ãããã話ã¯ãwhile xxx do ã done >yyy.log ãªã©ã®ããã«ãå¶å¾¡æ§é ã¨ãªãã¤ã¬ã¯ããçµã¿åãããå ´åã«ãã«ã¼ãå é¨ã§å¤æ´ããå¤æ°ãã«ã¼ãã®å¤ã§ã¯å©ç¨ã§ããªãããªãã¦ç¾è±¡ã¨ãã¦ç¾ãã¾ãã
*3:ãã®æ¹æ³ã¯ä»åã試è¡é¯èª¤ããåããç¥ã£ã¦ãããã©ããã£ã¨ç°¡åãªæ¹æ³ãããã¯ããã¨æã£ããã ããã