Shell Script to Display the Exit Status Using Grep Command Last Updated : 01 Nov, 2022 Comments Improve Suggest changes Like Article Like Report Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action in a shell script. Exit status is following according to their execution Command was successfully executed: 0 ( value of exit status)Command was a failure: 1 - 255 ( value of exit status will be b/w 1 to 255) Exit status: Every Linux command has exit status after execution. Its value can get by calling $? ( shell variable ). It gives the value of previous executed command. Note: Command for printing exit status for previous command: echo $? Here in this article, a shell script will be written to display the exit status using the grep command. It will show an example of shell scripts to display the exit status. Approach: To write the script code for this follow the below steps Use the touch command to create the database which is a text file named gfg.txt.Now get the key from the user if he wants to insert it in the database or check that the book is present in a database or not( means the book has read or not ).Now according to the chosen key make the if-else block and read the book Name from the user.In the first condition check the availability of the book using the grep command and get the exit status value and check if 0 or not. And the second condition simply gets the book name and adds the name to the database.Now, print the status of the book read according to the value of the exit status of the previous command. Shell Script Code: # shell script for display exit status by using grep command # make an file gfg.txt to contain the book name of user that he has read touch gfg.txt # now print user to tell him the scenario echo "Please, enter the some of your book name that you have read or a book name that you want to know read status of that book" echo # print the key to ask user find the book or write the book name echo "Please enter a key like following" echo "Enter 1 to know the book read status" echo "Enter 2 to write the book name in book database ( gfg.txt )" # read the entered key read key echo # check the condition using if-else statement if [[ $key == 1 ]] then # loop to get the name of book continuously while [ 1 ] do # print the initial statement echo "Enter book name about that you want to know read status or -1 for exit" # read the book name and find in gfg.text using grep command read bookName # check condition to break the loop if [[ $bookName == -1 ]] then break else # now find the book in gfg.text count=$(grep -c "$bookName" gfg.txt) # get the exit status of the previous command exitStatus=$? # check the value of exitStatus and print output according that if [[ $exitStatus == 0 ]] then # give the msg to user that he have read that book echo "Your previous command exit status is $exitStatus" echo "You have read the $bookName book." echo else # give the msg to user that he didn't read that book yet. echo "Your previous command exit status is $exitStatus" echo "You didn't read $bookName book yet." echo fi fi done else # loop to get the name of books continuously while [ 1 ] do echo "Enter book name that you want write in database (gfg.txt) or -1 for exit" # read the name of book read bookName # check the condition to break the loop if [[ $bookName != -1 ]] then # write the book name in text file echo $bookName >> gfg.txt else break fi echo done fi In the first example will show the inserting book name in the database ( gfg.text). In the second example will know about the check the book read status ( internal checking using grep command and exit status): Comment More infoAdvertise with us Next Article Shell Script to Display the Exit Status Using Grep Command A aditya32123 Follow Improve Article Tags : Linux-Unix Shell Script Similar Reads Shell Script to Demonstrate Wait Command in Linux Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution 4 min read Shell Script which Works Similar to the Unix Command HEAD TAIL Prerequisites: Bash Scripting, Shell Function Library HEAD and TAIL command print a specified number of lines of a file from the beginning and from the end respectively. If we don't specify a number, the commands print the entire file. We will make a bash script display.sh which takes two to three a 5 min read How to Use the grep Command in Linux with Examples? Grep is a very powerful utility in Linux that is used for searching patterns within files or a stream of text. It's one of those essential tools that system administrators and developers use for parsing logs, cleaning up data, or otherwise dealing with large text apa. This tutorial will walk you thr 4 min read Conditional Statements | Shell Script Conditional Statements: There are total 5 conditional statements which can be used in bash programming if statement if-else statement if..elif..else..fi statement (Else If ladder) if..then..else..if..then..fi..fi..(Nested if) switch statement Their description with syntax is as follows: if statement 3 min read Check If File Exist Inside Tar File Using Tar And Grep Command Linux users are familiar with the popular command-line tool Tar (tape archive), which is used to archive directories and files into a single file called a "tarball." Occasionally, you might need to verify whether a certain file is present in a tar archive without extracting the full archive. This de 4 min read Shell Scripting - True Command A shell provides an interface with the help of which users can interact with the system easily. To directly interact with a shell, we use an operating system. On a Unix-based operating system, every time we write a command using a terminal, we interact with the system. To interpret or analyze Unix c 3 min read How to Display Path of an Executable File in Linux | Which Command In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In th 6 min read Shell Script to Find How Many Terminals Has User Logged-In Here we are going to see Find How Many Terminals Has User Logged In. Using who command to fetch the user list and then using grep command we can find the number of Terminal that the user has logged in the Linux. We can use LOGNAME or UID to identify the user. UID is a unique User ID assigned to ever 2 min read Shell Script To Check the String For a Particular Condition Here, we are going to write a shell script that will echo (print) a suitable message if the accepted string does not meet a particular condition. Suppose we have given some certain conditions then our task is to print the message if the accepted string doesn't match the condition. Example 1: If the 3 min read How to check if a directory or a file exists in system or not using Shell Scripting? Shell scripting is really a powerful and dynamic way to automate your tasks. To test if a directory or file already exists in the system or not we can use shell scripting for the same along with test command. To proceed with the test script lets first check the test manual. To open a manual use the 2 min read Like