我的脚本中有一个调试功能(通过 nemo.action 运行),它使用 zenith 弹出窗口询问用户在发生错误时是否要停止或继续。但是,当这种情况发生在循环内时,exit
脚本中的命令 或 会被完全忽略。
无论如何,没有强制退出来完全停止脚本吗? 我希望它像渡渡鸟一样死掉! 应该是可以自己杀掉的..
这是我调试的一部分:
44:57.198 • Stopped while renaming file/directory - it failed
44:57.199 • Error triggered, issue:
44:57.201 • Stopped while renaming file/directory.\nSee debug log for more info...
45:20.563 • Stopping the script by user request due error. ()
^^^ this line gets written to the log when the user choose to exit
vvv but it continues to run the script until a new error occurs
45:20.564 • Could not find ‘/temp/sani.txt‘ Have no value to assign to $jdir0
45:20.566 • Error triggered, issue:
45:20.567 • Could not find ‘/temp/sani.txt‘ Have no value to assign to $jdir0
45:24.670 • Stopping the script by user request due error. ()
^^^ and when pressing stop here, the script will halt..
这是被触发的相同错误函数:
jerror () {
enl
jdbugen "Error triggered, issue:\n\n"
jdbug "$@"
debuglogread=$(tail -10 "$debuglog")
zenity --question --text="($jdec)Error Triggered\nIssue:\n\n ‘$@‘\n\n$debuglogread\n\nContinue?" --width=400 --height=200
jdec=$?
if [ "$jdec" != 0 ]
then
jdbug "Stopping the script by user request due error. ($jdeck)"
exit 1
else
jdbug "User choose to continue the script"
fi
enl
}
脚本的这部分正在调用函数:
mv -f -v "$item" "$path/$name" &>> $debuglog # Rename
jok=$?
if [ $jok -ne 0 ]
then
jdbug "Stopped while renaming file/directory - it failed"
jerror "Stopped while renaming file/directory.\nSee debug log for more info..."
fi
答案1
我发现这并不容易,所以我通过将退出请求存储在文件中来解决它。
通过添加字符串,printf "%d" "1" > "/tmp/exitcode.dat"
我将值 1 存储在临时文件中。
printf "%d" "1" > "/tmp/exitcode.dat" # Storing the exit code
exit 1 # Will break of of the loop
循环之后“完毕“命令,您将需要重新读取文件
if [[ -f "/tmp/exitcode.dat" ]]
then
jexit="$(</tmp/exitcode.dat)" # Read the stored code
rm "/tmp/exitcode.dat" # Clean up and remove the file
if [ $jexit eq 1 ]; then exit 1; # Check if exit code is 1
fi
更短的代码就是创建文件。
touch "/tmp/exitcode.dat" # Create the file
exit 1 # Exit the loop
并在之后完毕命令
if [[ -f "/tmp/exitcode.dat" ]]; Then # See if the file exists
rm "/tmp/exitcode.dat" # Cleanup
exit 1 # Exit the script
fi