J P M J P M - 1 month ago 8
Bash Question

If/Else In Regular Expression Scripting

I'm trying to script something to read parts of a report. I thought I knew how to use if/else logic inside of this, but I keep getting errors.

Code:

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"


I'm getting errors on line 13:

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

agc agc
Answer

Replace the if ... fi block with:

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
Comments