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.
分享到:
相关推荐
How to Determine the Type of Supervisor Module
ABSTRACT: Using a large-scale dataset from three leading online peer-to-peer (P2P) lending platforms in Indonesia from 2014 to 2018, we investigate the determinants of platform interest rate and ...
集合了 所有的 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 ...
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
Write Java program called AverageNumbers.java to determine the average of several numbers.
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...
We investigate the problem of ...control of the EDCA and HCCA mechanisms, which allows us to determine the optimal sharing of the wireless channel between the two access mechanisms. I. INTRODUCTION
Linux Forensics is the most comprehensive and up-to-date resource for those wishing to quickly and efficiently perform forensics on Linux systems. It is also a great asset for anyone that would like ...
The book tackles the process of optimization by first describing how to determine where a game is limited and then providing detailed solutions and examples to solving this limitation. All the ...
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...
Delve into the capabilities of SharePoint® 2010 -- and determine the best way to put this technology to work for your organization. With this practical guide, you'll gain project management practices...
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, ...
Using SAS to Determine the Revenue Impacts From Emerging Products
How to evaluate the performance of a parallel program 19 Introducing Python 21 Python in a parallel world 26 Introducing processes and threads 26 Start working with processes in Python 27 Start ...
A Novel Method to Determine the Redshifts of Active Galaxies Based on Wavelet Transform
Written by the leading experts in the field, this comprehensive guide shows you how to implement the SAP Business Information Warehouse (BW) and create useful applications for business analysis of ...
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...