2011-07-11 63 views
0

我正在處理某個文件,所以結果可以是任何一種方式。日誌記錄unix「cp」(複製)命令響應

例如:

>cp -R bin/*.ksh ../backup/

>cp bin/file.sh ../backup/bin/

當我執行上述命令,其得到複製。如果複製成功,系統沒有響應。如果沒有,則在終端本身cp: file.sh: No such file or directory中輸出錯誤或響應。

現在,我想記錄錯誤消息,或者如果成功,我想將自定義消息記錄到文件中。我能怎麼做?

確實有幫助。

感謝

回答

2

嘗試在shell腳本寫的:很多

#these three lines are to check if script is already running. 
#got this from some site don't remember :(

ME=`basename "$0"`; 
LCK="./${ME}.LCK"; 
exec 8>$LCK; 

LOGFILE=~/mycp.log 

if flock -n -x 8; then 

    # 2>&1 will redirect any error or other output to $LOGFILE 

    cp -R bin/*.ksh ../backup/ >> $LOGFILE 2>&1 

    # $? is shell variable that contains outcome of last ran command 
    # cp will return 0 if there was no error 
    if [$? -eq 0]; then 
     echo 'copied succesfully' >> $LOGFILE 
    fi 
fi 
+0

謝謝:)),這是我想要的東西 –