我有如下所示的脚本。 Q可以是1、3或6。 1 = E,3 = R,6 =S。因此,当我发送到串行端口时,Q应该是E,R或S。 我完全被困住了。有帮助吗?
touch $logfile
echo "file = $file"
# Split the fields into the Ademco/SIA ContactID fields
MT=`echo $EVENT | cut -b5-6`
ACCT=`echo $EVENT | cut -b1-4`
#MT=`echo $EVENT | cut -b5-6`
Q=`echo $EVENT | cut -b7`
XYZ=`echo $EVENT | cut -b8-10`
GG=`echo $EVENT | cut -b11-12`
CCC=`echo $EVENT | cut -b13-15`
S=`echo $EVENT | cut -b16`
###############################################################################
# Start Logging
################################################################################
echo "================================================================================" >> $logfile
echo " Alarm Notification received at `date`" >> $logfile
echo "============================================================================ ====" >> $logfile
echo Alarm String was $EVENT >> $logfile
echo Account Number $ACCT >> $logfile
echo Message Type $MT >> $logfile
echo Event Qualifier $Q >> $logfile
echo Event Code $XYZ >> $logfile
echo Group $GG >> $logfile
echo Zone $CCC >> $logfile
echo Checksum $S >> $logfile
echo "" >> $logfile
echo 5012 $MT$ACCT$Q$XYZ$GG$CCC'\024\r' >> /dev/ttyAMA0
echo 5012 $MT$ACCT$Q$XYZ$GG$CCC >> /home/monitoring/pinCaptur/signals.txt
答案 0 :(得分:1)
您可以使用tr
:
Q="123456"
newQ=$(tr "136" "ERS" <<< "${Q}")
echo ${newQ}
E2R45S
答案 1 :(得分:0)
您可以使用if else
语句或数组将数字1、3、6映射到字母E,R,S。示例:
map=([1]=E [3]=R [6]=S)
Q=1
echo "${map[Q]}" # prints E
Q=3
echo "${map[Q]}" # prints R
Q=6
echo "${map[Q]}" # prints S
还可以考虑使用shellcheck.net来改善脚本。
答案 2 :(得分:0)
在这里需要一个switch语句,如果输入值错误,它将允许您退出。
case $Q in
1)
Q=E
;;
3)
Q=R
;;
6)
Q=S
;;
*)
echo "We have an issue here"
exit 1
esac