[英]Linux bash script -
I am trying to use whether or not a line contains a date as a condition for an if statement: 我正在尝试使用行是否包含日期作为if语句的条件:
if [grep -n -v '[0-9][0-9][0-9][0-9]' $line |wc -l==0]
then
...
The above returns an error. 以上返回错误。 I don't necessarily need to use grep.
我不一定需要使用grep。 The line processed by grep would look like:
grep处理的行如下所示:
1984 Dan Marino QB Miami Dolphins
Any help is appreciated. 任何帮助表示赞赏。
if [[ $(echo $line | grep -q '[0-9][0-9][0-9][0-9]') ]]; then
# do something
fi
You can check this using bash built-ins: 您可以使用内置的bash进行检查:
re='\b[[:digit:]]{4}\b'
if [[ $line =~ $re ]] ; then
echo ok;
fi
[grep -n -v '[0-9][0-9][0-9][0-9]' $line |wc -l==0]
problem 1: [(space).....(space)]
you need those spaces 问题1:
[(space).....(space)]
您需要那些空格
problem 2: there is no [ foo==bar ]
you can do something like [ $(echo "0") = "0" ]
or [[ $(echo "0") == 0 ]]
here the $(echo "0")
is an example, you should fill with your commands. 问题2:有没有
[ foo==bar ]
你可以这样做[ $(echo "0") = "0" ]
或[[ $(echo "0") == 0 ]]
这里的$(echo "0")
例如$(echo "0")
,则应填写命令。
You can just call grep with -q
option and check the return value: 您可以使用
-q
选项调用grep并检查返回值:
if [ $(grep -qv '[0-9][0-9][0-9][0-9]' $line) -eq 0 ]; then
# ...
fi
使用命令替换和正确的bash语法。
[[ "`grep -n -v '[0-9][0-9][0-9][0-9]' $line | wc -l`" -eq 0 ]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.