Okay, so I'm having trouble dealing with Bash and it's lack of global variables. The problem I'm having is that inside the while loop, I'm having to do a for loop, and inside that for loop I'm going to be calling a script and I need to dynamically change the variable $NAME. Unfortunately, I won't know what it is until I've tried a few different options. The variable never wants to change after it's been set in the original process. I've done a lot of research about this today and I've found that it's because reading a file in such a file opens a subshell and it can't edit values in it's parent process. I've tried making several files to source to export the variable dynamically, but that doesn't work either.
EDIT: THIS EXAMPLE IS TRIVIAL, BUT IT'S THE SAME LOGIC I'M USING AT WORK. CAN'T SHOW WORK CODE.
#!/usr/bin/env bash
FILE=$1
SCRIPT="FOO"
NAME="TEST"
#create script
cat > "${SCRIPT}" <<EOF
HELLO WORLD, $NAME
EOF
while read line; do
echo $line
echo $SCRIPT
for i in `seq 1 10`; do
(
NAME="MEGAMAN"
echo $NAME
)
done
done < "$FILE"
#echo $NAME
cat > "${SCRIPT}" <<EOF
HELLO WORLD, $NAME
EOF
My output:
jose@jose-desktop:~/workspace/Script $ ./Main FOO
HELLO WORLD, TEST
FOO
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN
MEGAMAN **this output is fine
And the file it outputs:
HELLO WORLD, TEST **This file should say MEGAMAN.