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

发布于 2024-11-19 15:28:06 字数 341 浏览 4 评论 0原文

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

例如:

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

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

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

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

确实有任何帮助。

谢谢

I am coping some file,So, the result can be either way.

eg:

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

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

When I execute above commands, its getting copied. No response from the system, if it copied successful. If not, prints the error or response in terminal itself cp: file.sh: No such file or directory.

Now, I want to log the error message, or if it successful I want to log my custom message to a file. How can I do?

Any help indeed.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谁把谁当真 2024-11-26 15:28:06

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

try writing this in a shell script:

#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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文