天天看點

Linux Bash:在使用者輸入目錄中查找含有某字元串的檔案,列印行号

 #!/bin/bash

######## My homework for Network Operating System#########

######## To find files that contains the entered string###########

#######################################################

if test $# -ne 2####判斷參數是否合法

then

        echo "Please enter the Directory that you want to search:/c"

        read dir#####讀入使用者輸入目錄

        echo "Please enter the word you want to find:/c"

        read word###讀入使用者輸入字元串

        dir=${dir}*

        if grep -n $word $dir

        then

                echo "The result is listed above!"

                exit 0

        else

                echo "Sorry,The word was not found."

                exit -1

        fi

else

        dir=$1

        word=$2

        dir=${dir}*

        if grep -n $word $dir#這裡我用grep帶n參數來過濾出來檔案。

        then

                echo "The result is listed above!"

        else

                echo "Sorry,The word was not found."

        fi

fi

exit 0

繼續閱讀