【问题标题】:logging unix "cp" (copy) command response记录 unix“cp”(复制)命令响应
【发布时间】:2011-07-11 12:32:46
【问题描述】:

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

例如:

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

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

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

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

确实有任何帮助。

谢谢

【问题讨论】:

    标签: unix logging command cp


    【解决方案1】:

    尝试在 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
    

    【讨论】:

      猜你喜欢
      • 2014-11-18
      • 2012-09-09
      • 1970-01-01
      • 1970-01-01
      • 2015-04-22
      • 1970-01-01
      • 2016-08-11
      • 1970-01-01
      • 2022-01-06
      相关资源
      最近更新 更多