Using
system documentation resources97
hello Boston!hello Singapore!hello World!As you see, it’s simple to use the conditionals in a script and provide a lot of control over the conditions under which a command is run. We now need to control when something may not be running correctly.
That’s what the exit codes (or error codes) are for. Let’s go for it!
Exit codesWhen
a program is run, it provides an
exit code, specifying whether it ran OK or whether there was an issue. That exit code is stored in a special variable called Lets take a look at it by running ls hello.sh:
[root@rhel-instance
]# ls hello.shhello.sh[root@rhel-instance
]# echo $?0When
the program runs OK,
the exit code is zero, What happens when we try to list a file that doesn’t exist (or run
any other command incorrectly, or that is having issues Let’s try listing a nonexistent file:
[root@rhel-instance
]# ls nonexistentfile.txtls: cannot access 'nonexistentfile.txt': No such file orb
]# echo $?2You seethe exit code is not zero. We will go to the documentation and check the number associated with it to understand the nature of the issue.
When
running a command in a script, check for the exit code and act accordingly. Let’s now review whereto find further
information on the commands, such
as exit codes or other options, in the following section.
Share with your friends: