lee barley
lee barley

Reputation: 1

need to change variable in this script

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

Upvotes: 0

Views: 61

Answers (3)

Socowi
Socowi

Reputation: 27370

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.

Upvotes: 0

Guillaume Quintard
Guillaume Quintard

Reputation: 619

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

Upvotes: 0

Walter A
Walter A

Reputation: 20032

You can use tr :

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

E2R45S

Upvotes: 1

Related Questions