2010-09-22 90 views

回答

6

test -e將測試文件是否存在。如果測試成功,則測試命令返回零值,否則返回1。

測試可以寫無論是作爲test -e或使用[]

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

除非你真正需要的grep的輸出,你可以測試返回值的grep會返回1,如果沒有比賽和零,如果有是任何。

總體而言,你可以測試一個字符串是否非空使用[ "string" ]將返回0,如果非空則爲1空

4

如果你有二進制安裝testksh具有內置的匹配功能,你可以用它來執行你的檢查。通常/bin/[是一個符號鏈接test

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

if [ -z "$used_var" ]; then 
    echo "Variable is empty" 
fi 
12
if test -e "$file_name";then 
... 
fi 

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

代替存儲的grep的輸出變量,然後檢查是否變量是空的,你可以這樣做:

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 
+0

-q是順便說一下 「安靜」。 – brannigan 2017-03-22 09:43:04

1

您應該使用grep-q標誌安靜輸出。請參見下面的手冊頁:

人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安靜的輸出,是你的問題的解決方案。

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: 1}Directory 2}File 3}String 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: 1}directoryName 
createDir(){ 
    echo "Entering createDir" 
    mkdir -p ${1} 
    echo "Exiting createDir" 
} 

#Params: 1}FileName 
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: 1}File 
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: 1}Directory Path 2}File 3}String 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: 1}Directory Path 2}File 3}String 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輸出:

[email protected] /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 
相關問題