Problem with variable value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem with variable value
# 1  
Old 02-22-2010
Problem with variable value

Can anyone help me?...I don't know why the second 'echo $contador' always shows 0 (zero):

Code:
  1 #!/bin/bash
  2 contador=0
  3 while read linea
  4 do
  5    echo
  6    echo "$linea" | while IFS="" read -n 1 caracter
  7    do
  8       contador=$((${contador}+1))
  9       echo $contador
 10    done
 11    echo $linea
 12    echo $contador
 13    
 14 done < leerfichero.sh

Smilie
# 2  
Old 02-22-2010
can you provide with some information ?
# 3  
Old 02-22-2010
Of course.
When executed, the output (echo in line 9) is:
1
2
3
....until the length of the first line of the file "leerarchivo.sh"
but..the 'echo' in line 12 prints 0.
This is the result.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/bash
0

I would like the last '0' were 12! I don't understand why..
# 4  
Old 02-22-2010
Because

Code:
  6 ...while IFS="" read -n 1 caracter
  7    do
  8       contador=$((${contador}+1))
  9       echo $contador
 10    done

is executing in a subshell. $contador inside that loop and $contador outside of it have nothing to do with each other.
# 5  
Old 02-22-2010
pipes invokes subshells.

Code:
echo "$linea" | while IFS="" read -n 1 caracter

the while loop is starting in a sub shell and all the processing (until done) is processing in the same shell. when the loops finishes, the execution returns to the parent shell and the modified value of contador left in the older (subshell).

what you are getting (zero) is the value of the variable in the parent shell.

you can force the command to execute in the same shell with (...).

try:

Code:
contador=0
while read linea
do
    echo ""
    echo "$linea" | (while IFS="" read -n 1 caracter
    do
       contador=$((${contador}+1))
       echo $contador
    done
    echo $linea
    echo $contador )
done < leerfichero.sh

# 6  
Old 02-22-2010
Thank you very much
I'm a newbie in shell scripting!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

problem in assigning value to variable have value fo other variable

my script is some thing like this i11="{1,2,3,4,5,6,7,8,9,10,11,}" echo "enter value" read value ..............suppose i11 x="$value" echo "$($value)" .............the echo should be {1,2,3,4,5,6,7,8,9,10,11,} but its showing "i11" only. plz help me out to get desired... (10 Replies)
Discussion started by: sagar_1986
10 Replies

2. Shell Programming and Scripting

Problem with a variable withing a variable

hello there, basically im screwed with a variable that should take the last modification date of a file. my code is fileCreationTime=$(( `ls -l $fileName | tr -s " " | cut -d " " -f6` )) my problem arise coz when the code is executed and stored in a file the return value is 1993 and not... (4 Replies)
Discussion started by: thurft
4 Replies

3. Shell Programming and Scripting

problem in variable comparision

Hi All, I need to compare the string variable with input value which have more than one word which also should be case insensitive. #!/bin/sh echo "please choose your choice: " read choice if ` ] then echo "correct" else echo "wrong" fi but i am getting the output for first word... (3 Replies)
Discussion started by: kalai
3 Replies

4. Shell Programming and Scripting

awk variable problem

Hi How are you people .. I am facing another problem regarding awk below is my code -bash-3.00$ export a=10 -bash-3.00$ echo $a 10 -bash-3.00$ echo "" | nawk -v var=$a ' { if(var=10) { print "abc" } else { print "def"} } ' abc -bash-3.00$ echo "" | nawk -v var=$a ' { if(var==10)... (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. Shell Programming and Scripting

Problem in storing value to variable

#bash curVer=`cat /var/sadm/clsversion | cut -f 2 -d "_"` echo "CurVer:$curVer" ls |grep -v tar| grep -v sh| grep -v log|cut -f 1 -d "_" | sort -u >tmp1 for line in $(cat tmp1) do ver=`echo $line_$curVer` ls $line* |sort >tmp2 grep -n ${ver} tmp2 >/dev/null ... (2 Replies)
Discussion started by: rajamohan
2 Replies

6. Shell Programming and Scripting

Problem wit the $? variable

Hi Friends, I have a problem when i run this piece of script, and do echo $? it always gives me value 0, even if the string "CCRCWebServerINSTALLDIR" is not present in that file. Why is that so, can you give me suggestion to the changes i need to make? $find... (3 Replies)
Discussion started by: asirohi
3 Replies

7. Shell Programming and Scripting

Problem with variable use

Hi, I need a little help with variable use: I have the a file with follow format: Type: Test Profile: 010 84240 27 15 84900 11 09 84993 55 44 84762 12 12 I need to look the value in the line containing "Profile" and put in front of the lines containing numbers... (6 Replies)
Discussion started by: cgkmal
6 Replies

8. Shell Programming and Scripting

Help me in this variable problem

suppose i declare a=b b=30 echo $(echo $a) i need to get the value as 30 i dont know if im right but it prints only 'b' as output. How can i get the value referred by 'b' through the value of 'a' (4 Replies)
Discussion started by: SankarV
4 Replies

9. UNIX for Dummies Questions & Answers

grep for a variable problem

I am trying to use two files and walk through the first one to see if a value from each record resides in the other file. I am reading the file record by record and awking out the first field into a varaiable. That is working fine. When I try to write my grep command it gives me an error. ... (7 Replies)
Discussion started by: MizzGail
7 Replies

10. Shell Programming and Scripting

Problem with variable type

Dear All, I am trying to write an script to calculate geometric centre of selected residues of a protein structure. Say I have a PDB file (a text containing the x, y, and z coordinates of atoms of a protein). I want to extract the X coordinates of the atoms belonging to the interested residues... (2 Replies)
Discussion started by: siavoush
2 Replies
Login or Register to Ask a Question