I have this structure in Shell Script BASH
i=0
while read linha_reg
do
REG_ADDRESS=$(echo $linha_reg | cut -f1 -d ' ')
[ $REG_ADDRESS -lt 16] && T[$i]=$REG_ADDRESS || S[$i]=$REG_ADDRESS
i=$((i+1))
done < register.txt
while read linha_inst
do
RS=$(echo $linha_inst | cut -f4 -d ' ')
RS=$(echo $RS | cut -f1 -d ',')
RS_INDEX=${RS:2:2}
RS_LETTER=${RS:1:1}
[ "$RS_LETTER" == "s" ] && echo ${S[$RS_INDEX]}
done < instruction.txt
With this snippet I have only a blank space in the echo ${S[$RS_INDEX]} when I execute it on Terminal. However, if I write
echo ${S[@]}
It outputs the entire array, as requested by the echo. If I write
echo ${S[3]}
Doesn't work also.
I'm lost because I don't know what explains this and I want it to return only a single element, as the big snippet does but it doesn't output by unknown reasons.
Cheers
$iwhether you assign toTorS; that will leave gaps in the array you don't assign to.Swithecho ${#S[@]}to verify you have more than 1 element. Then you know how many to expect. You may also want to post example lines of register.txt and instruction.txt. Usingecho | cut ...is a wonky way to do what should be done by substring extraction.T[x]andS[y]in order to keep you indexes straight. useiforSand sayjforT.i=0; j=0; .. [ $REG_ADDRESS -lt 16] && { T[$i]=$REG_ADDRESS; ((i++)); } || { S[$j]=$REG_ADDRESS; ((j++)); }--or--declare -a T; declare -a Sand then useT+=$REG_ADDRESSandS+=$REG_ADDRESST+=($REG_ADDRESS), otherwise you simply perform string concatenation on${T[0]}.