检查文件是不是存在以及是不是包含特定字符串

Posted

技术标签:

【中文标题】检查文件是不是存在以及是不是包含特定字符串【英文标题】:Check if file exists and whether it contains a specific string检查文件是否存在以及是否包含特定字符串 【发布时间】:2011-04-15 14:24:30 【问题描述】:

我想检查 file2.sh 是否存在,以及特定单词 poet 是否是文件的一部分。我使用grep 来创建变量used_var

#!/bin/ksh

file_name=/home/file2.sh                  
used_var=`grep "poet" $file_name`    

如何检查used_var 是否有一些价值?

【问题讨论】:

【参考方案1】:

您应该使用grep -q 标志进行安静输出。请参阅下面的手册页:

ma​​n grep 输出:

 General Output Control

  -q, --quiet, --silent
              Quiet;  do  not write anything to standard output.  Exit immediately with zero status
              if any match is  found,  even  if  an  error  was  detected.   Also  see  the  -s  or
              --no-messages option.  (-q is specified by POSIX.)

此 KornShell (ksh) 脚本演示了 grep quiet 输出,是您问题的解决方案。

grepUtil.ksh:

#!/bin/ksh

#Initialize Variables
file=poet.txt
var=""
dir=tempDir
dirPath="/"$dir"/"
searchString="poet"

#Function to initialize variables
initialize()
    echo "Entering initialize"
    echo "Exiting initialize"


#Function to create File with Input
#Params: 1Directory 2File 3String to write to FileName
createFileWithInput()
    echo "Entering createFileWithInput"
    orgDirectory=$PWD
    cd $1
    > $2
    print $3 >> $2
    cd $orgDirectory
    echo "Exiting createFileWithInput"


#Function to create File with Input
#Params: 1directoryName
createDir()
    echo "Entering createDir"
    mkdir -p $1
    echo "Exiting createDir"


#Params: 1FileName
readLine()
    echo "Entering readLine"
    file=$1
    while read line
    do
        #assign last line to var
        var="$line"
    done <"$file"
    echo "Exiting readLine"

#Check if file exists 
#Params: 1File
doesFileExit()
    echo "Entering doesFileExit"
    orgDirectory=$PWD
    cd $PWD$dirPath
    #echo $PWD
    if [[ -e "$1" ]]; then
        echo "$1 exists"
    else
        echo "$1 does not exist"
    fi
    cd $orgDirectory
    echo "Exiting doesFileExit"

#Check if file contains a string quietly
#Params: 1Directory Path 2File 3String to seach for in File
doesFileContainStringQuiet()
    echo "Entering doesFileContainStringQuiet"
    orgDirectory=$PWD
    cd $PWD$1
    #echo $PWD
    grep -q $3 $2
    if [ $? -eq 0 ];then
        echo "$3 found in $2"
    else
        echo "$3 not found in $2"
    fi
    cd $orgDirectory
    echo "Exiting doesFileContainStringQuiet"

#Check if file contains a string with output
#Params: 1Directory Path 2File 3String to seach for in File
doesFileContainString()
    echo "Entering doesFileContainString"
    orgDirectory=$PWD
    cd $PWD$1
    #echo $PWD
    grep $3 $2
    if [ $? -eq 0 ];then
        echo "$3 found in $2"
    else
        echo "$3 not found in $2"
    fi
    cd $orgDirectory
    echo "Exiting doesFileContainString"


#-----------
#---Main----
#-----------
echo "Starting: $PWD/$0 with Input Parameters: 1: $1 2: $2 3: $3"
#initialize #function call#
createDir $dir #function call#
createFileWithInput $dir $file $searchString #function call#
doesFileExit $file #function call#
if [ $? -eq 0 ];then
    doesFileContainStringQuiet $dirPath $file $searchString #function call#
    doesFileContainString $dirPath $file $searchString #function call#
fi
echo "Exiting: $PWD/$0"

grepUtil.ksh 输出:

user@foo /tmp
$ ksh grepUtil.ksh
Starting: /tmp/grepUtil.ksh with Input Parameters: 1:  2:  3:
Entering createDir
Exiting createDir
Entering createFileWithInput
Exiting createFileWithInput
Entering doesFileExit
poet.txt exists
Exiting doesFileExit
Entering doesFileContainStringQuiet
poet found in poet.txt
Exiting doesFileContainStringQuiet
Entering doesFileContainString
poet
poet found in poet.txt
Exiting doesFileContainString
Exiting: /tmp/grepUtil.ksh

【讨论】:

【参考方案2】:

您可以这样做:

if grep -q "poet" $file_name
then
    echo "poet was found in $file_name"
fi

============

以下是一些常用的测试:

   -d FILE
          FILE exists and is a directory
   -e FILE
          FILE exists
   -f FILE
          FILE exists and is a regular file
   -h FILE
          FILE exists and is a symbolic link (same as -L)
   -r FILE
          FILE exists and is readable
   -s FILE
          FILE exists and has a size greater than zero
   -w FILE
          FILE exists and is writable
   -x FILE
          FILE exists and is executable
   -z STRING
          the length of STRING is zero

例子:

if [ -e "$file_name" ] && [ ! -z "$used_var" ]
then
    echo "$file_name exists and $used_var is not empty"
fi

【讨论】:

-q 顺便说一句“安静”。【参考方案3】:
if test -e "$file_name";then
 ...
fi

if grep -q "poet" $file_name; then
  ..
fi

【讨论】:

【参考方案4】:

如果您安装了 test 二进制文件或 ksh 具有匹配的内置函数,则可以使用它来执行检查。通常/bin/[ 是指向test 的符号链接:

if [ -e "$file_name" ]; then
  echo "File exists"
fi

if [ -z "$used_var" ]; then
  echo "Variable is empty"
fi

【讨论】:

【参考方案5】:

test -e 将测试文件是否存在。如果测试成功,test 命令返回零值,否则返回 1。

测试可以写成test -e或使用[]

[ -e "$file_name" ] && grep "poet" $file_name

除非您确实需要 grep 的输出,否则您可以测试返回值,因为如果没有匹配项,grep 将返回 1,如果有任何匹配项,则返回零。

一般来说,您可以使用[ "string" ] 测试字符串是否为非空,如果非空则返回 0,如果为空则返回 1

【讨论】:

以上是关于检查文件是不是存在以及是不是包含特定字符串的主要内容,如果未能解决你的问题,请参考以下文章

如何检查特定文件是不是存在以及使用哪个退出代码?

检查字符串是不是仅包含特定字符? [复制]

Shell如何检查文件中的一行中是不是存在模式

JavaScript/jQuery - 如何检查字符串是不是包含特定单词

如何正确检查字符串是不是不包含特定单词?

如何检查字符串是不是包含特定单词?