0

を使用bash script/python/perl scriptすると、コマンドの出力が文字列なしであるかどうかを示すことができますか?

curl -i http://www.google.com


HTTP/1.1 302 Found
Location: http://www.google.com.hk/
Cache-Control: private
Content-Type: text/html; charset=UTF-8

私がやりたいことは次のとおりです。

  1. 出力に 302 が含まれる場合、何も出力しない
  2. それ以外の場合は、印刷します302 is missed
4

5 に答える 5

1
$ grep -q 302 << EOF || echo "302 is missed"
> HTTP/1.1 302 Found
> Location: http://www.google.com.hk/
> Cache-Control: private
> Content-Type: text/html; charset=UTF-8
> EOF
$ grep -q 302 << EOF || echo "302 is missed"
> HTTP/1.1 312 Found
> Location: http://www.google.com.hk/
> Cache-Control: private
> Content-Type: text/html; charset=UTF-8
> EOF
302 is missed
于 2012-12-14T15:10:40.413 に答える
1

次のような意味ですか。

if [ ! `echo 302 | grep 302` ] ; then echo 302 is missed; fi

echo 302適切なコマンドに置き換えることができる場所...

同様に:

echo 302 | grep 302 > /dev/null || echo "302 is missed"
于 2012-12-14T15:11:07.513 に答える
1

curl|grepSilentedの結果を test として使用できます。

if ! `curl -i -s http://www.google.com|grep -q 302` ; then echo "302 is missed" ; fi
于 2012-12-14T15:14:42.790 に答える
0

いつものように、最初にテストしますが、これでうまくいくと思います。

perl -e '$pat = shift; print "$pat is missed\n" unless qx{@ARGV} =~ /\Q$pat\E/' 302 curl -i http://www.google.com
于 2012-12-14T15:27:24.603 に答える