I can't change the value of my variable!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting I can't change the value of my variable!
# 1  
Old 10-25-2012
I can't change the value of my variable!

I made this HEADMAKER variable to pull the header from the first file in the loop, but then to stop so it doesn't override the file with later loops. However, I CANNOT get it to reassign the value of my variable away from "FIRST". I have also tried it with 1 and 0, and with and without quotes and the dollar sign, but it will not reassign it. Pretty frustrated. Anyone know why??

Code:
HEADMAKER="FIRST"

find | sed -e 's:\.::g' -e 's:/::g' -e 's:txt::g' | while read a

do
        if [[ ${a} == "" ]]
        then continue
        fi

        if [[ ${HEADMAKER}=="FIRST" ]]
        then
                head -1 "${a}".txt > OR_Full_WithCounties.txt

                HEADMAKER=$"DONE"
        fi

        awk -F"\t" -v county="${a}" 'NR != 1 {print $0, county}' "${a}".txt | sed 's:\r:\t:g'

done >> OR_Full_WithCounties.txt

echo ${HEADMAKER}

Thanks!!

Last edited by Scott; 10-25-2012 at 07:04 PM.. Reason: You REALLY think YELLOW works on that background?! Changed to RED. Optician bill in the post ;)
# 2  
Old 10-25-2012
The while-loop is executing in a subshell. The final echo statement is in the parent shell, which never sees the change.

This is often a source of bewilderment. Try searching the forum for something like "while loop subshell".

Regards and welcome to the forum,
Alister
# 3  
Old 10-25-2012
One way to avoid this would be:

Code:
find | sed -e 's:\.::g' -e 's:/::g' -e 's:txt::g' > /tmp/$$

while read a
do
...
done < /tmp/$$

rm -f /tmp/$$

# 4  
Old 10-25-2012
Also you can get rid of the first if condition by running find for specific pattern. In your case *.txt files:-

Code:
find . -name "*.txt" | sed -e 's/\.\///g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Change variable value

I have big XML which i want to change all VERSIONNUMBER equal to 1,in existing file values of VERSIONNUMBER will be different as below now i want to change all VERSIONNUMBER values qual to 1.Please help me which will convert versionnumber values. <SHORTCUT OBJECTSUBTYPE ="" OBJECTTYPE ... (5 Replies)
Discussion started by: katakamvivek
5 Replies

2. UNIX for Dummies Questions & Answers

Change Date in Variable

I am hoping someone can help me with this issue. I am extracting a date from a file that is in DD/MM/YYYY format using... expiry_date=`echo ${line}| awk -F, '{ print $4 }' | tr -d '\r'` What I need to do is amend the value so instead of getting 30/12/2016 I end up with 12/30/2016. I have... (11 Replies)
Discussion started by: theref
11 Replies

3. Shell Programming and Scripting

Change the value of variable in a file

Hi Friends, I have shell script file1.sh which has reference to enviornment file called Content of filenev.sh SKIP_FLAG=N ORACLE_HOME=/u01/oracle/database My requirement is to change the value of SKIP_FLAG to Y in filenv.sh from file1.sh. Could anyone please help me to do... (2 Replies)
Discussion started by: vfrg
2 Replies

4. Shell Programming and Scripting

Change Variable Value from Multiple Scripts and Use these Variable

Hi to All, Please find below details. file_config.config export file1_status="SUCCESS" export file2_status="SUCCESS" file_one.sh I am calling another two shell script from these script. I need to pass individual two script status (If it's "FAILED") to file_main.sh. file_main.sh I... (2 Replies)
Discussion started by: div_Neev
2 Replies

5. Shell Programming and Scripting

Change the filename variable value

Hi guys, I have a variable where i am storing the filename (with full path). I just need the value before ".txt". But instead of getting the filename i am getting the contents of the filename. FileName=/appl/data/Input/US/Test.txt a=`awk -F"." '{print $1}' ${FileName}` echo $a... (3 Replies)
Discussion started by: mac4rfree
3 Replies

6. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

7. Shell Programming and Scripting

How do I change a variable to something only if it's empty?

I feel like it is just a matter of using the $ operators correctly, but I can't seem to get it... hostname="network" ip="192.168.1.1" netmask="" variables=( $hostname $ip $netmask ) for var in ${variables} do if ; then $var="--" fi done echo... (7 Replies)
Discussion started by: etranman1
7 Replies

8. Shell Programming and Scripting

Change only the name of a variable

if I have a variable named z inside a java file, is there any way to globally change the name of the variable and all its occurences as a variable? (but not any other z that is not part of that variable name) (3 Replies)
Discussion started by: lydiaflamp
3 Replies

9. Shell Programming and Scripting

Variable name change in a loop

I need to do something like this:for I in var1 var2 var3 ; do $I = "Something calculated inside the loop" doneObviously it doesn't work...but is it possible doing that in other ways? Thanks in advance. (6 Replies)
Discussion started by: canduc17
6 Replies

10. Shell Programming and Scripting

how to change the variable in the file

Hi all, I have problem regarding how to change the quantity in the file. I will explain you by an example. I have one file whose name is sample. The content of file are as follows: sample ----------------------------------------------------------- 1 334 3562 2345 5324 85657 75666 845 ... (0 Replies)
Discussion started by: Aniket
0 Replies
Login or Register to Ask a Question