Hello All,
Maybe I'm Missing something here but I have NOOO idea what the heck is going on with this....?
I have a Variable that contains a PATTERN of what I'm considering "Illegal Characters". So what I'm doing is looping
through a string containing some of these "Illegal Characters". Now when these chars are found I add them to the Array
variable, and on the very next line (while still in the same if statement, inside the loop) I print the Array Element which
shows the correct value inside that particular element.
Now after the loop completes I'm trying to print the array with a 'for' loop and for some reason its treating the array as
if it were empty, and I have NO idea why this is happening...?
Here's the Section that isn't working correctly:
#!/bin/bash
declare -a FOUND_CHARS
ILLEGAL_CHARS="[\|\`\&\>\<\'\\\"\{\}\;\!]" #--> CONTAINS ANY 1 OF: | ` & > < ' \ " \ \ { } ; !
ILLEGAL_CHARS_2="\[|\]" #--> CONTAINS ANY 1 OF: [ ]
### FOUND 6 Total Illegal Characters in this Test String "$MY_STR"
# CHARS ARE: {, }, &, <, >, and !
MY_STR="{SOME & STRING WITH ! A COUPLE ILLEGAL <CHARS>}"
echo -e "MY_STR = \"$MY_STR\""
count=0
echo -n "$MY_STR" | while read -n 1 char
do
if [[ "$char" =~ $ILLEGAL_CHARS || "$char" =~ $ILLEGAL_CHARS_2 ]]
then
FOUND_CHARS[$count]="$char"
echo -ne " ILLEGAL char --> \"$char\"\n"
echo -ne "FOUND_CHARS[$count] --> \"${FOUND_CHARS[$count]}\"\n"
let count++
else
echo -ne "\tLEGAL CHARACTER --> \"$char\"\n"
fi
done
echo -e "\n\n"
for sym in ${FOUND_CHARS[@]}
echo "Array Contains:"
do
echo -e "\tsym == $sym"
done
Below is the Output.
The Output from the "while" loop is CORRECT. But then after that "while" loop is done, then it's the "for" loop's turn and it
prints NOTHING...
Here's the Output:
MY_STR = "{SOME & STRING WITH ! A COUPLE ILLEGAL <CHARS>}"
ILLEGAL char --> "{"
FOUND_CHARS[0] --> "{"
LEGAL CHARACTER --> "S"
LEGAL CHARACTER --> "O"
LEGAL CHARACTER --> "M"
LEGAL CHARACTER --> "E"
LEGAL CHARACTER --> ""
ILLEGAL char --> "&"
FOUND_CHARS[1] --> "&"
LEGAL CHARACTER --> ""
LEGAL CHARACTER --> "S"
LEGAL CHARACTER --> "T"
LEGAL CHARACTER --> "R"
LEGAL CHARACTER --> "I"
LEGAL CHARACTER --> "N"
LEGAL CHARACTER --> "G"
LEGAL CHARACTER --> ""
LEGAL CHARACTER --> "W"
LEGAL CHARACTER --> "I"
LEGAL CHARACTER --> "T"
LEGAL CHARACTER --> "H"
LEGAL CHARACTER --> ""
ILLEGAL char --> "!"
FOUND_CHARS[2] --> "!"
LEGAL CHARACTER --> ""
LEGAL CHARACTER --> "A"
LEGAL CHARACTER --> ""
LEGAL CHARACTER --> "C"
LEGAL CHARACTER --> "O"
LEGAL CHARACTER --> "U"
LEGAL CHARACTER --> "P"
LEGAL CHARACTER --> "L"
LEGAL CHARACTER --> "E"
LEGAL CHARACTER --> ""
LEGAL CHARACTER --> "I"
LEGAL CHARACTER --> "L"
LEGAL CHARACTER --> "L"
LEGAL CHARACTER --> "E"
LEGAL CHARACTER --> "G"
LEGAL CHARACTER --> "A"
LEGAL CHARACTER --> "L"
LEGAL CHARACTER --> ""
ILLEGAL char --> "<"
FOUND_CHARS[3] --> "<"
LEGAL CHARACTER --> "C"
LEGAL CHARACTER --> "H"
LEGAL CHARACTER --> "A"
LEGAL CHARACTER --> "R"
LEGAL CHARACTER --> "S"
ILLEGAL char --> ">"
FOUND_CHARS[4] --> ">"
ILLEGAL char --> "}"
FOUND_CHARS[5] --> "}"
Array Contains:
As you can see in the Output, the 'while' the loop is looping through the string and it is printing the Illegal
Characters that it finds, and you can see that the variable inside the loop DOES contain the values...
So why is the array empty after the loop has completed?
Any thoughts would be much appreciated!
Thanks in Advance,
Matt
---------- Post updated at 04:45 PM ---------- Previous update was at 03:55 PM ----------
UPDATE:
Well this look like it has something to do with my "while" loop...
Maybe something like it's spawning a sub-process so when it returns from the sub-process that Array doesn't exist anymore?
If I put the text into a file and loop using this construct (below) instead of what I have now, the array contains the data
after it completes.
NEW LOOP CONSTRUCT:
MY_FILE="~/temp_textFile.txt"
echo "$MY_STR" > $MY_FILE
while read -n 1 char
do
...... CODE .....
done < $MY_FILE
How can I do this without printing to a file first?
Seems like too much work for something as simple as looping through a string character-by-character..?
Thanks,
Matt