【问题标题】:If/Else In Regular Expression Scripting正则表达式脚本中的 If/Else
【发布时间】:2016-10-06 02:24:18
【问题描述】:

我正在尝试编写脚本来阅读报告的某些部分。我以为我知道如何在其中使用 if/else 逻辑,但我不断收到错误。

代码:

printf "The report type is: "
egrep -o 'METAR|SPECI' metar1.txt

printf "Station: "
egrep -o 'K[A-Z]{3}' metar1.txt

printf "Day of the month: "
date_time=$(egrep -o '[0-9]{6}Z' metar1.txt)
echo "$date_time"|cut -c1-2
printf "Time of day: "
echo "$date_time"|cut -c3-6

if [[ egrep 'AUTO' metar1.txt ]];
then
    echo "This is a fully automated report."
elif [[ egrep -o 'COR' metar1.txt ]];
then
    echo "This is a corrected observation."
else
    echo "There is neither 'AUTO' or 'COR' in this report."
fi

wind_degree=$(egrep -o '\s[0-9]{5}G?[0-9]?[0-9]?KT\s' metar1.txt|cut -c2-4)
wind_speed=$(egrep -o '\s[0-9]{5}G?[0-9]?[0-9]?KT\s' metar1.txt|cut -c5-6)
printf "Winds are from $wind_degree degree at $wind_speed knots\n"

我在第 13 行遇到错误:

line 13: conditional binary operator expected, 
line 13: syntax error near `'AUTO'', 
line 13: `if [[ egrep 'AUTO' metar1.txt ]];'

【问题讨论】:

    标签: regex shell scripting


    【解决方案1】:

    if ... fi 块替换为:

    if    egrep 'AUTO'   metar1.txt ;  then
        echo "This is a fully automated report."
    elif  egrep -o 'COR' metar1.txt ; then
        echo "This is a corrected observation."
    else
        echo "There is neither 'AUTO' or 'COR' in this report."
    fi
    

    【讨论】:

      猜你喜欢
      • 2012-10-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多