We can determine the exit status of command executed by command `echo $?` if it returns the value 0 its success & any non zero is failure.
Few examples,
1.)
[root@redhat ~]# w
10:02:20 up 27 min, 1 user, load average: 0.00, 0.00, 0.00
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/1 192.168.164.1 09:36 0.00s 0.04s 0.00s w
[root@redhat ~]# echo $?
0
[root@redhat ~]# asda
-bash: asda: command not found
[root@redhat ~]# echo $?
127
[root@redhat ~]#
2.)
[root@redhat ~]# grep asdasf /etc/passwd
[root@redhat ~]# echo $?
1
[root@redhat ~]# grep root /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@redhat ~]# echo $?
0
[root@redhat ~]#
Hope these examples have given more clarity.
Few Exit Codes & its meaning,
1 - Catchall for general errors
2 - Misuse of shell built-ins according to Bash documentation.
126 - Command invoked cannot execute
127 - command not found, may be path issue or misspelt commands
128 - Invalid argument to exit
128 - +n Fatal error signal "n"
130 - Script terminated by Control-C (Control-C is fatal error signal 2, (130 = 128 + 2, see above))
255 - Exit status out of range

No comments:
Post a Comment