开发者

shell script + match word in line without using echo

the following test syntax is part of ksh script

   [[ $PARAM = TRUE ]] &&  [[ ` echo $LINE_FROM_FILE | grep -c Validation ` -eq 1 ]] && print "find Validation word"

Can I get some other creative syntax/solution/command to verify if Validation word exists in LINE_FROM_FILE without to 开发者_开发技巧use the echo command?

LINE_FROM_FILE="123 Validation V125 tcp IP=1.2.56.4"

remark: need to match exactly Validation word

lidia


You can use Parameter expansion that removes everything up to and including Validation and compare that to the normal expansion of the parameter. If they are not equal the parameter contains Validation.

[[ "${LINE_FROM_FILE##* Validation }" != "${LINE_FROM_FILE}" ]]

(Since there are always spaces before and after Validation these can be included in the pattern. I do not know of an other way to match beginning and end of word in shell patterns)


You could do it using case:

case $LINE_FROM_FILE in
    *Validation*)
        echo "found word Validation"
        ;;
    *)
        echo "did not find word Validation"
        ;;
esac


[[ $PARAM = TRUE ]] &&  [[ "$LINE_FROM_FILE" == *Validation* ]] && print "find Validation word"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜