› Forums › Basic skills › Programming › Shell scripting (in bash) › Detecting whether a process completed successfully
- This topic has 3 replies, 2 voices, and was last updated 7 years, 1 month ago by Simon.
-
AuthorPosts
-
-
November 27, 2016 at 12:05 #6091
Here’s a neat little construction that checks the exit status of a process. In this example, I’m using HLEd to find MLFs that have problems:
HLEd -I $MLF /dev/null if [ $? -ne 0 ]; then echo "Error from HLEd - there is a problem with " ${MLF}
I’m asking
HLEd
to load the MLF, and to do nothing (it needs a “editing command” file, for which I am providing the special empty system file/dev/null
).$?
contains the exit status of the most recent process. A value of 0 indicates success. -
November 21, 2017 at 18:01 #8506
Could you please give me some guidance on how to write something into my script which runs all the scripts consecutively so that if an error occurs it catches which username the error is for and prints this into a text file, so that these users can be excluded? Thank you
-
November 21, 2017 at 20:55 #8507
#!/usr/local/bin/bash echo "This is the child script" # let's run an HTK command, but with a deliberate mistake to cause an error HInit -T 1 this_file_does_not_exist # at this point, the special shell variable $? contains the exit status of HInit # for readability, let's put that status into a variable with a nicer name STATUS=$? # test whether the return status is 0 (indicates success) if [ ${STATUS} = 0 ] then echo "HInit ran without error" else echo "Something went wrong in HInit, which exited with code "${STATUS} # a good idea is to exit this script with the same error code # (or some other non-zero value, if you prefer) # so that anything calling this script can also detect the error exit ${STATUS} fi
-
November 21, 2017 at 20:58 #8508
Put the above code in a file called
child_script
and try calling it from another script, to demonstrate thatchild_script
correctly returns an exit status#!/usr/local/bin/bash echo "This is the parent script" # let's call the child script ./child_script # at this point, the special shell variable $? contains the exit status of child_script # for readability, let's put that status into a variable with a nicer name STATUS=$? if [ ${STATUS} = 0 ] then echo "The child script ran OK" else echo "In parent script: child script returned error code "${STATUS} # exiting with any non-zero value indicates an error exit 1 fi
-
-
AuthorPosts
- You must be logged in to reply to this topic.