检查变量中的字符串

Mik*_*e Q 0 bash

在bash中,我经常检查一个变量的值,它是否可以这样做?有没有更好的办法?

例:

    if [ "`echo ${file} | grep -vE '(Trash|home)'`" ] ;then
Run Code Online (Sandbox Code Playgroud)

这是检查变量文件不包含单词Trash或home,如果它不执行某些操作.关键是我使用echo和grep,我认为这可能是浪费.

另一个例子:

    if [ "`less ${TEMPQUERY} | grep 'http'`" ] ;then
Run Code Online (Sandbox Code Playgroud)

这是检查文件中的字符串http,如果它在文件中,则执行某些操作.

我想我只是想知道别人做了什么,如果有一些强烈的理由我不应该以这种方式编码.无论如何,提前谢谢你的时间.

Ign*_*ams 5

$ file="My home"
$ [[ $file =~ Trash|home ]] ; echo $?
0
$ file="root"
$ [[ $file =~ Trash|home ]] ; echo $?
1

$ cat input.txt
http://...
$ grep -v -q http input.txt ; echo $?
1
Run Code Online (Sandbox Code Playgroud)