0

I have script as shown below. Q can be 1, 3 or 6. So like so 1 = E, 3 = R and 6 = S. So, when I get to sending to serial port, Q should be E, R or S. I'm totally stuck. Any help?

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

3 Answers 3

1

You can use tr :

Q="123456"
newQ=$(tr "136" "ERS" <<< "${Q}")
echo ${newQ}

E2R45S
Sign up to request clarification or add additional context in comments.

Comments

0

a switch statement is what you want here, it'll allow you to exit in case the input value is wrong.

case $Q in
  1)
    Q=E
    ;;
  3)
    Q=R
    ;;
  6)
    Q=S
    ;;
  *)
    echo "We have an issue here"
    exit 1
esac

Comments

0

You can map the numbers 1, 3, 6 to the letters E, R, S using if else statements or an array. Example:

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

Also consider using shellcheck.net to improve your script.

2 Comments

You could use explicit indices instead of populating dummy elements: map=([1]=E [3]=R [6]=S).
@BenjaminW. Thank you for the hint.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.