Shell Script to Display the Exit Status Using Grep Command
Last Updated :
01 Nov, 2022
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):
Share your thoughts in the comments
Please Login to comment...