全部博文(362)
分类:
2010-10-05 17:14:09
#!/bin/bash echo hello echo $? # Exit status 0 returned because command executed successfully. lskdf # Unrecognized command. echo $? # Non-zero exit status returned because command failed to execute. echo exit 113 # Will return 113 to shell. # To verify this, type "echo $?" after script terminates. # By convention, an 'exit 0' indicates success, #+ while a non-zero exit value means an error or anomalous condition.
Example 6-2. Negating a condition using !
true # The "true" builtin.
echo "exit status of \"true\" = $?" # 0
! true
echo "exit status of \"! true\" = $?" # 1
# Note that the "!" needs a space between it and the command.
# !true leads to a "command not found" error
#
# The '!' operator prefixing a command invokes the Bash history mechanism.
true
!true
# No error this time, but no negation either.
# It just repeats the previous command (true).
# =========================================================== #
# Preceding a _pipe_ with ! inverts the exit status returned.
ls | bogus_command # bash: bogus_command: command not found
echo $? # 127
! ls | bogus_command # bash: bogus_command: command not found
echo $? # 0
# Note that the ! does not change the execution of the pipe.
# Only the exit status changes.
# =========================================================== #
# Thanks, St閜hane Chazelas and Kristopher Newsome.
特定的退出状态码具有, 所以用户不应该在脚本中指定它.