English 中文(简体)
壳牌指挥——以指挥产出为依据的条件?
原标题:Shell command - condition based on output of command?
  • 时间:2010-06-29 06:57:45
  •  标签:
  • shell

如果在文本档案中没有显示扼杀物,我会试图控制一些炮弹。 如果给我一个错误的话,我会把这一界线推向指挥线。

if [ $(cat textfile.txt | grep "search string") -eq "" ]; then; echo "some string"; fi;

错误:

-bash: [: -eq: unary operator expected
最佳回答

如您使用<代码>[,以进行比较,则需要使用=而不是-eq。 你们也需要一些引言。

if [ "$(cat textfile.txt | grep  search string )" = "" ]; then; echo "some string"; fi;

注:grep 可将档案名称作为理由,因此没有必要cat <>/code>。 也可直接使用<代码>grep的回报价值: 如果找不到搜身区,则将可恢复1号。

if [ "$(grep  search string  textfile.txt)" ]; then
  echo "some string";
fi

更紧凑的方式是使用逻辑和<代码>和 amp;&。

grep "search string" textfile.txt && echo "some string"
问题回答

www.un.org/Depts/DGACM/index_french.htm 如果能够找到所要求的线路(如果不是的话,如果出现错误的话,则有2条),那么你就只能使用:

grep "search string" textfile.txt >/dev/null 2>&1
if [[ $? -ne 0 ]] ; then
    echo  Not found 
fi

如果您really希望使用方言(和你可能的话,请引用这些指示,以便你不要太多的论据用于<条码>。

if [ "$(cat textfile.txt | grep  search string )" == "" ] ; then
    echo "It s not there!"
fi
grep -F -q -e  search string  textfile.txt || echo  Not found 

注:

  • -F prevents the interpretation of the search string as a regular expression.
  • -q suppresses all output and returns immediately after the first instance was found, making the search much faster if the string occurs at the beginning of a large file.
  • -e specifies the pattern explicitly, allowing patterns that start with a dash.
  • Use single quotes unless you want variable substitutions.

此处无需方括号。 由于<代码>[实际上是指挥,任何指挥均可在你使用时使用。 因此,我们可以在这里使用<代码>grep。 由于<代码>grep,无需使用<代码>cat。 而且,你们有两处太多的半殖民地。

if grep -q "search string" textfile.txt; then echo "some string"; fi

if grep "search string" textfile.txt > /dev/null 2>&1; then echo "some string"; fi




相关问题
KornShell- Creating a fixed width text file

I need to create a simple fixed width text file in KornShell (ksh). My current attempt using printf to pad the string isn t working out very well. What s the shortest, cleanest way to create a fixed ...

unix find command

how to use the find command to find files/directories which are not matching the pattern. for eg: find <some options > -name "dontfile.txt" should give me output of all the find whose file ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

Case insensitive comparison of strings in shell script

The == operator is used to compare two strings in shell script. However, I want to compare two strings ignoring case, how can it be done? Is there any standard command for this?

1. 露台式照像

在Windows XP中,没有一时出现指挥时,是否有办法操作一种灰色文字? 我常常需要把“善待”(工作)与“灰色”同起来,即使我的文字没有产出,......

热门标签