Shell 脚本实战:利用 Grep 命令展示退出状态

Linux 为用户提供了一个非常酷的命令行工具以及图形用户界面(GUI),让我们可以通过运行命令来执行各种任务。所有这些命令在执行后都会返回一个状态。我们可以利用这个执行返回值来显示错误信息,或者在 Shell 脚本中执行后续的操作。

根据命令的执行情况,退出状态如下:

  • 命令执行成功:退出状态值为 0
  • 命令执行失败:退出状态值为 1 – 255(值介于 1 到 255 之间)

退出状态(Exit Status)

每个 Linux 命令执行完成后都会有一个退出状态。我们可以通过调用 $?(Shell 变量)来获取它的值。这个变量保存了上一个执行命令的返回值。

注意:用于打印上一个命令退出状态的命令是: echo $?

在这篇文章中,我们将编写一个 Shell 脚本,通过使用 grep 命令来展示 退出状态。我们将通过一个具体的 Shell 脚本示例来演示如何获取和显示这一状态。

实现思路

为了编写这个脚本代码,我们可以遵循以下步骤:

  • 使用 INLINECODE18196ca2 命令创建一个数据库文件,这里是一个名为 INLINECODE725559e7 的文本文件。
  • 现在,我们需要从用户那里获取一个按键(key),以决定是将数据插入数据库,还是检查某本书是否存在于数据库中(即是否已读)。
  • 根据用户输入的按键,构建 if-else 逻辑块,并从用户处读取书名。
  • 在第一种情况中,使用 grep 命令检查书籍的可用性,并获取其退出状态值,判断是否为 0。第二种情况则简单地获取书名并将其添加到数据库中。
  • 最后,根据上一个命令的退出状态值,打印出相应的书籍阅读状态。

Shell 脚本代码

# 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

示例运行

在第一个示例中,我们将演示如何在数据库(gfg.txt)中插入书名。

!示例图片

!示例图片

在第二个示例中,我们将演示如何检查书籍的阅读状态(内部通过 grep 命令和退出状态进行检查):

!示例图片

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。如需转载,请注明文章出处豆丁博客和来源网址。https://shluqu.cn/42771.html
点赞
0.00 平均评分 (0% 分数) - 0