I've got a debug function in my script (that runs via nemo.action) that uses zenith popup to ask if the user want to stop or continue when an error occurs. However, when this happens inside a loop, the exit command in the script or is completely ignored.

Isn't there anyway to force the exit to stop the script completely? I want it dead as a dodo! It must be able to kill it self..

this is part of my debug:

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..

It's the same error function that's getting triggered:

jerror () {
    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
    if [ "$jdec" != 0 ]
        jdbug "Stopping the script by user request due error. ($jdeck)"
        exit 1
        jdbug "User choose to continue the script"

this part of the script is calling the function:

mv -f  -v "$item" "$path/$name"    &>>  $debuglog  # Rename
    if [ $jok -ne 0 ]
        jdbug "Stopped while renaming file/directory  - it failed"
        jerror "Stopped while renaming file/directory.\nSee debug log for more info..."
  • How do you call your function? – Kusalananda Aug 22 '20 at 19:54
  • @Kusalananda just with "jerror (the reason I called)" – JoBe Aug 22 '20 at 20:05
  • updated question with answer.. – JoBe Aug 22 '20 at 20:21
  • Is that call inside a compound statement, or a pipeline, or a loop? It may be run in a subshell, and the exit would only exit the subshell, and not the outer command. We might need to see the whole script. – Paul_Pedant Aug 22 '20 at 20:22
  • it's run inside a loop : while IFS= read -d '' -r item do ... the script itself is on 522 lines. – JoBe Aug 22 '20 at 20:23

I found out that this isn't easy, so I solved it by storing the exit request in a file.

By adding the string printf "%d" "1" > "/tmp/exitcode.dat" I stored the value 1 in a temporary file.

   printf "%d" "1" > "/tmp/exitcode.dat"  # Storing the exit code
   exit 1                                # Will break of of the loop

After the loops "done" command, you will need to re-read the file

if [[ -f "/tmp/exitcode.dat" ]] 
 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

a shorter code would be to just create the file.

    touch "/tmp/exitcode.dat"      # Create the file
    exit 1                         # Exit the loop

and after the done command

if [[ -f "/tmp/exitcode.dat" ]]; Then   # See if the file exists
        rm "/tmp/exitcode.dat"              # Cleanup
        exit 1                    # Exit the script

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.