我将在底部包含整个脚本以供参考。在init脚本结束时调用此脚本。如果我在登录后运行脚本,则运行时没有错误。 30000英尺视图,尝试执行rsync命令并将任何错误捕获到变量中。我正在使用bash 3.5.6pl。
以下是罪魁祸首:
LogMessage+=$(rsync -rpltDzv --delete --progress /mnt/WinPrimary /mnt/WinBackup 2>&1 >/dev/tty)$'\n'
变量中存储的内容是:
/etc/Braums/scripts/Sync1to2.sh: line 47: /dev/tty: No such device or address
我用Google搜索技术来捕获stderr而不是stdout。我希望显示进度而不是捕获所有屏幕内容。 所以init调用脚本“Sync1to2.sh”。哪个有上面的命令。如果我在登录后从命令行运行相同的脚本,它将按预期运行。
这是整个Sync1to2.sh脚本。我不经常做bash脚本,所以要小心,不要伤害自己的笑声。另外一个注意事项,下面是我尝试/ dev / stdout时它有一些诊断。我认为当getty运行时它有什么用?
#!/bin/bash
#
#Variable to set if an error occurs at any point during the process.
ErrorOccured="False"
LogMessage=""
LogMessage+=$0$' was stated at '$(date)$'\n'
if !(mount | grep -cq "/mnt/WinPrimary")
then
LogMessage+=$'=============================================\n'
LogMessage+=$'][ No Windows Primary found. ][\n'
LogMessage+=$'][ Check drives and try again. ][\n'
LogMessage+=$'=============================================\n'
ErrorOccured="True"
fi
if !(mount | grep -cq "/mnt/WinBackup")
then
LogMessage+=$'=============================================\n'
LogMessage+=$'][ No Windows Backup found. ][\n'
LogMessage+=$'][ Check drives and try again. ][\n'
LogMessage+=$'=============================================\n'
ErrorOccured="True"
fi
if test "$ErrorOccured" == "False"
then
#Maybe add cleanup the harddrive later
if [ -f /mnt/WinPrimary/Sync1to2.err ]
then
LogMessage+=$'=============================================\n'
LogMessage+=$'][ Found a Sync1to2.err. ][\n'
LogMessage+=$'][ Are we being run in a loop? ][\n'
LogMessage+=$'=============================================\n'
ErrorOccured="True"
fi
if [ -f /mnt/WinPrimary/Sync1to2.ok ]
then
LogMessage+=$'=============================================\n'
LogMessage+=$'][ Found a Sync1to2.ok. ][\n'
LogMessage+=$'][ Are we being run in a loop? ][\n'
LogMessage+=$'=============================================\n'
ErrorOccured="True"
fi
if [ "$ErrorOccured" == "False" ]
then
LogMessage+=$'Sync Started '$(date)$'\n'
LogMessage+=$(rsync -rpltDzv --delete --progress /mnt/WinPrimary /mnt/WinBackup 2>&1 >/dev/stdout)$'\n'
ExitValue=$?
LogMessage+=$'Sync Finished '$(date)$'\n'
LogMessage+=$'____________________ '$(date)$'\n'
LogMessage+=$(ls /dev/tty*)$'\n'
LogMessage+=$'____________________ '$(date)$'\n'
LogMessage+=$'____________________ '$(date)$'\n'
LogMessage+=$(ls /dev/stdout)$'\n'
LogMessage+=$'____________________ '$(date)$'\n'
if [ $ExitValue != 0 ]
then
ErrorOccured="True"
fi
fi
fi
echo $ErrorOccured
if [ "$ErrorOccured" == "True" ]
then
echo "Sync1to2 errored out" >> /mnt/WinPrimary/Sync1to2.err
echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.err
echo "Finished $(date)" >> /mnt/WinPrimary/Sync1to2.err
echo "$LogMessage" >> /mnt/WinPrimary/Sync1to2.err
echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.err
else
echo "Sync1to2 ok" >> /mnt/WinPrimary/Sync1to2.ok
echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.ok
echo "Finished $(date)" >> /mnt/WinPrimary/Sync1to2.ok
echo "$LogMessage" >> /mnt/WinPrimary/Sync1to2.ok
echo "_______________________________________________________" >> /mnt/WinPrimary/Sync1to2.ok
cp /mnt/WinPrimary/menu.default.lst /mnt/WinPrimary/menu.lst
cp /mnt/WinPrimary/menu.default.lst /mnt/WinBackup/menu.lst
echo Shuting down
fi
/etc/Braums/scripts/LogPing.pl Sync1to2 "$LogMessage"
exit 254
答案 0 :(得分:4)
而不是写入/dev/tty
,如何简单地交换stdout和stderr?
yourcommand 3>&2 2>&1 1>&3 3>&-
这是使用第三个临时变量交换两个变量内容的重定向:
3>&2 # tmp = stderr
2>&1 # stderr = stdout
1>&3 # stdout = tmp
3>&- # close(tmp)