Hi expert,
I'm newbie in awk programming and now I have difficulty how to make awk array in ash shell. I have code below.
As you know ash shell doesn't support array and I cannot install bash
in my embedded environment.
So in order this code to work in ash shell then all array needs to be converted
Problem now while creating array in ash shell :
scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')
and calling array in ash shell :
echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
$HEYU $(echo ${scene[${phone#*~}]})
Can somebody help me on this?
#!/bin/sh
HEYU=/usr/bin/heyu # Path to Heyu
SED=/usr/bin/sed
SORTAZ=0 # If 0, will sort by house code.
get_scenes() {
SCENES=$($HEYU show usersyns | grep "usersyn")
if [ "$SORTAZ" -eq "1" ]
then
SCENES=$(echo "$SCENES" | sort)
fi
IFS=$(echo -en "\n\b")
for line in $SCENES
do
let SCNCNT=$SCNCNT+1
scene[$SCNCNT]=$(echo $line | awk '{ print $2 }')
echo -n "S: " >> $LOG
echo $line | awk '{ print "SCENE~" $2 "~'$SCNCNT'" }' | sed 's/_/ /g' | \
sed 's/.*/\L&/; s/[a-z]*/\u&/g' | tee -a $LOG
done
IFS=$ORIGIFS
}
set_scene() {
echo -n "S: " >> $LOG
echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
$HEYU $(echo ${scene[${phone#*~}]})
sleep 1
get_updates "$DEVICES"
}
get_scenes
set_scene
The awk array is very simple, just start using it and it is created. It is really a hash map. Your challenge is to learn enough awk to put the array to use. There are many fine online tutorials, just Google 'awk tutorial array', like: AWK Arrays Explained with 5 Practical Examples
I suspect you will have more problems than just the arrays. For example the sed uses GNU extensions, which are likely not supported by the (busybox?) version in your embedded environment, nor is the -e option to echo. Also the code seems inefficient with several calls to subshells and external commands within a loop.
yes, i need to install gnu sed instead of using busybox sed
gnu sed doesn't need to have lots of memory space, so i'm ok
but bash needs lots of memory, my embedded device can't have this
echo "MSG~heyu $(echo ${scene[${phone#*~}]})" | tee -a $LOG
to
echo "MSG~heyu $(echo $(eval echo \$$scene${phone#*~}))" | tee -a $LOG
$HEYU $(echo ${scene[${phone#*~}]})
to
$HEYU $(echo $(eval echo \$$scene${phone#*~}))
After replace, everything works as expected
However now I have another problem in my other code using array variable
The code is below
How to modify this code to work with ash shell ??
LIST=(1 2 3)
for i in "${LIST[@]}"; do
echo "example.$i "
done