bash脚本编程,检查错误,记录 [英] Bash scripting, checking for errors, logging
问题描述
下面是一个在bash福向导。不,实际上,我只是在开玩笑,你可能都知道这除了我..
Here's one for the bash-fu wizards. No, actually, I'm just kidding, you'll all probably know this except for me..
我试图创建一个备份的shell脚本。这个想法很简单:找到特定的文件夹的文件,超过7天,焦油/ gzip压缩他们到另一个目录,并删除它们。问题是,我不知道我有没有足够的权限来创建在目标DIR一个tar / gzip文件。有没有(合适的)的方式来检查文件是否已成功创建,如果是这样,删除文件。否则,请跳过这部分也不会破坏用户的数据。我听说他们是不是很喜欢这一点。
I'm trying to create a backup shell script. The idea is fairly simple: find files in a certain folder, older than 7 days, tar/gzip them to another directory, and remove them. The problem is, I'm not sure if I'll have enough permissions to create a tar/gzip file in the target dir. Is there any (proper) way to check if the file has been created successfully, and if so, delete the files. Otherwise, skip that part and don't destroy customers' data. I hear they are not very fond of that.
下面是我到目前为止有:
Here's what I have so far:
01: #!/bin/bash
02:
03: ROOTDIR="/data/www"
04:
05: TAR="${ROOTDIR}/log/svg_out_xml/export_out_ack_$(date +%Y-%m-%d).tar"
06: cd ${ROOTDIR}/exchange/export/out_ack/
07: find . -mtime +7 -type f -print0 | xargs -0 tar -cf "${TAR}"
08: gzip ${TAR}
09: find . -mtime +7 -type f -print0 | xargs -0 rm -f
基本上,我需要检查,如果一切顺利上线7和8罚款,如果是执行9。
Basically, I'd need to check if everything went fine on lines 7 and 8, and if so execute 9.
此外,我想使这些操作的日志文件,所以我知道一切正常(这是一个夜间cron作业)。
Additionally, I'd like to make a log file of these operations so I know everything went fine (this is a nightly cron job).
推荐答案
有关记录,你可以用你的脚本片段在大括号和标准输出重定向到一个日志文件:
For logging, you can wrap sections of your script in curly braces and redirect the stdout to a log file:
{
script_command_1
script_command_2
script_command_3
} >> /path/to/log_file
这篇关于bash脚本编程,检查错误,记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!