记录unix“cp”(复制)命令响应

时间:2011-07-11 04:03:37

标签: unix logging command cp

我正在处理一些文件,因此,结果可以是任何一种方式。

例如:

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

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

当我执行上述命令时,它会被复制。如果复制成功,系统无响应。如果没有,则在终端本身cp: file.sh: No such file or directory中打印错误或响应。

现在,我想记录错误消息,或者如果成功,我想将自定义消息记录到文件中。我能怎么做?

确实有任何帮助。

由于

1 个答案:

答案 0 :(得分: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