我对 if 语句有疑问,基本上我想分别将 和 的值写入和$tag9700
,如果它们存在的话。如果没有,则写入9700l.log和.$tag9701
9700l.log
9701l.log
9700=,
9701=,
9700l.log
tag9700=`egrep -Eo '9700=[0-2]{0,9}' $filename-PDK-AP-LXFXMR-01*.log`
if [ $tag9700 -eq 0 ]
then
echo $tag9700 >> /home/user/9700l.log
else
echo '9700=,' >> /home/user/9700l.log
fi
tag9701=`egrep -Eo '9701=[A-Z]{0,9}' $filename-PDK-AP-LXFXMR-01*.log`
if [ $tag9701 -eq 0 ]
then
echo $tag9701 >> /home/user/9701l.log
else
echo '9701=,' >> /home/user/9701l.log
fi
结果是该9700l.log
文件应如下所示:
9700=1
9700=0
9700=,(only if it isn't a value grepped in the intial $tag9700)
同样的场景也适用于$tag9701
.
答案1
tag9700=`egrep -Eo '9700=[0-2]{1,9}' $filename-PDK-AP-LXFXMR-01*.log`
echo "${tag9700:-,}" > 9700.logfile
您需要最少的1
匹配。然后,如果匹配不成功,您可以使用 bash 替换属性${var:-...}