Shell: How to determine the exit status of Linux and UNIX command
by NIXCRAFT on FEBRUARY 11, 2006 · 14 COMMENTS· last updated at JUNE 1, 2008
Q. Can you explain the exit status of shell and commands under Linux / UNIX operating system?
A. All UNIX and Linux command has a several parameters or variables that can be use to find out the exit status of command. Please note that these parameters or variables may only be referenced assignment to them is not allowed. You can use $? to find out the exit status of command. $? always expands to the status of the most recently executed foreground command or pipeline. For example, you run the command cal:
$ cal
Now to see exit status of cal command type following command:
$ echo $?
Output:
0
Zero means command executed successfully, if exit status returns non-zero value then your command failed to execute. For example run command called cyberciti
$ cyberciti
Output:
bash: cyberciti: command not found
Display exit status of the command:
$ echo $?
Output:
127
Value 127 (non-zero) indicates command cyberciti failed to execute. You can use exit status in shell scripting too. You can store result of exit status in variable. Consider following shell script:
#!/bin/bash
echo -n "Enter user name : "
read USR
cut -d: -f1 /etc/passwd | grep "$USR" > /dev/null
OUT=$?
if [ $OUT -eq 0 ];then
echo "User account found!"
else
echo "User account does not exists in /etc/passwd file!"
fi
Save and execute the script as follows:
$ chmod +x script.sh
$ ./script.sh
Output:
Enter user name : jradmin
User account does not exists in /etc/passwd file
Try it one more time:
$ ./script.sh
Output:
Enter user name : vivek
User account found
As you can see, I have used grep command to find out user name stored in USR variable. If grep command finds user name in /etc/passwd command output it would return exit status of zero. This is stored in OUT variable. Next, if command makes decision based upon exit status stored in OUT variable.
分享到:
相关推荐
### 如何确定已安装在Catalyst 6500/6000系列交换机中的主管模块类型 #### 引言 本文档提供了一些简单的检查步骤,以便您能够确定Cisco Catalyst 6000或6500系列交换机所使用的主管引擎模块的类型。...
集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 ...
Determine which development techniques work best for you, and practice the important skill of debugging Learn relationships among input and output, decisions and loops, classes and methods, strings ...
JEDEC JESD33B:2004(R2018) Standard Method for Measuring and Using the Temperature Coefficient of Resistance to Determine the Temperature of a Metallization Line JEDEC JESD33B标准是由JEDECSolid ...
In this tutorial, we'll cover some of the basics of Unicode-encoded text and Unicode files, and how to view and manipulate it in UltraEdit. Search and delete lines found UEStudio and UltraEdit provide...
Installing from Source ...The x.py command can be run directly on most Unix systems in the following format: ./x.py <subcommand> [flags] This is how the documentation and examples assume yo
The two things needed to build firewalls and QoS with Linux are two packages named netfilter and iproute. While netfilter is a packet-filtering framework included in the Linux kernels 2.4 and 2.6, ip...
How to determine how many trees to use in a random forest Just where does the "randomness" come from Out of Bag Errors & Cross Validation - how good of a fit did the machine learning algorithm make...
The trick is to determine when to use the different parts of the brain, and to do this, we need to think harder (and smarter) about how we think. Jonah Lehrer arms us with the tools we need, ...
Before we look at how SQL Server uses and manages its memory, we need to ensure a full understanding of the more common memory related terms. The following definitions will help you understand how SQL...
It covers the basics of the tool, including how to create and edit XAML files, manipulate controls, and use animations and transitions. #### Common Properties (Chapter 4 & Appendix A) Chapters 4 and...
review of hydrologic and hydraulic methods to determine instream environmental flow,Onikpo Claude Randyx Martin,,To provide to their lives, humans need water. So it is used for irrigation purposes,...
` stores the exit status of the last executed command. This can be used to determine if a command succeeded or failed. - **Internal File Separator**: `$IFS` defines the internal field separator used ...
holds an exclusive lock at the table level, and another transaction (T2) holds an exclusive lock at the row level, each of the transactions believe they have exclusive access to the resource....
标题 "The skin does not exist: Unable to determine the release version" 提到的问题,通常是与软件部署或更新时出现的错误有关,尤其是与版本控制或者界面显示有关。这可能是指在使用某些开源工具或框架时,系统...
You’ll learn how to determine what kind of tool is appropriate for which purpose, and how certain tools can be combined to form the foundation of a good application architecture. You’ll learn how ...
gives advice and guidance on how to determine optimal policies for system administration. employs probabilistic rather than Boolean methods. illustrates key points with examples and exercises. The ...
The vision is “To produce the best UNIX-like operating system package possible, with due respect to the original software tools ideology as well as usability, performance and stability.” The ...