> -6ren">
gpt4 book ai didi

linux - Bash - if 语句中的错误代码不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:54:06 25 4
gpt4 key购买 nike

我正在编写一个 bash 脚本来备份到已安装的驱动器。

备份本身工作正常,但即使在传输过程中出现错误,它也会说成功。

backup() {
echo -e "Backup Started\n" >> $1
echo -e "Mounting backup Drive" >> $1
mount $2 /mnt 2>>$1
if [ $? -eq 0 ] ; then
echo -e "Backup Drive: $2 mounted successfully" >> $1
cd /
echo -e "Sync data to $2\n" >> $1
nice -n 19 rsync -lHa --exclude-from '/opt/ts/bin/exclude.txt' / /mnt/ 2> ${ERROR}
local RETURNCODE=$?
if [ $RETURNCODE -eq 23 ] ; then backup_failed "Backup Warning: Some files did not copy" $1 $2; fi
if [ $RETURNCODE -eq 20 ] ; then backup_failed "Backup Failed: Transfer was terminated prematurely" $1 $2; fi
if [ $RETURNCODE -eq 11 ] ; then backup_failed "Backup Failed: Input/Output Error [URGENT]" $1 $2; fi
if [ $RETURNCODE -ne 0 ] ; then backup_failed "Backup Failed: Error while copying data" $1 $2; fi
echo -e "Backup completed Successfully with code $RETURNCODE `date`\n" >> $1
echo -e >> $1
printf "Time taken: "%dh:%dm:%ds"\n" $(($SECONDS/3600)) $(($SECONDS%3600/60)) $(($SECONDS%60)) >> $1
echo -e "##############################\n" >> $1
mkdir -p /mnt/mnt /mnt/proc /mnt/tmp /mnt/lost+found
cp -a /proc/mounts /proc/filesystems /mnt/proc
umount $2
fi
}

尽管 $RETURNCODE 给出的值为 20,但它不会触发。输出如下。

安装备份驱动器备份盘:/dev/hda4挂载成功同步数据到/dev/hda4

备份成功完成,代码为 20 Sun May 4 11:04:48 EST 2014

耗时:0h:0m:3s

#

如果有人有任何建议,我们将不胜感激:)

最佳答案

您确定函数 backup_failed 的行为符合预期吗?

另请注意,当 RETURNCODE 不为 0 且 backup_failed 不会终止脚本时,backup_failed 将被触发两次:

if [ $RETURNCODE -eq 20 ] ; then backup_failed ...
...
if [ $RETURNCODE -ne 0 ] ; then backup_failed ...

希望这对您有所帮助。

关于linux - Bash - if 语句中的错误代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23451671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com