String concatenation not working in a loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting String concatenation not working in a loop
# 1  
Old 01-26-2010
String concatenation not working in a loop

Hi,

First post, so I hope someone can help me with this weirdness Smilie

I have a number files with some rows of information I want to extract, at the same time I want to add to a string some details from the file. I have found two different ways of looping over rows in a file, but one method doesn't let me concatenarte strings and I don't know why....

The first method sets the IFS environmental variable. I don't really favour this method, but it works..

Code:
#!/bin/bash -
OLDIFS=$IFS
IFS='
'

STE="cat"
  
for FILE in $( ls *-df | cut -f 4 -d / ); do
  N=0
  for LINE in $( cat $FILE ); do
    N=$((N+1))
    STE="$STE""dog"
  done
done
echo "$STE" && exit

and produces:
catdogdogdogdogdogdogdogdogdogdogdogdogdogdogdogdo gdogdogdogdogdogdogdogdogdogdogdogdog


Method 2 uses "read" but prevents the concatenation of the string:
Code:
#!/bin/bash -
STE="cat"
  
for FILE in $( ls *-df | cut -f 4 -d / ); do
  N=0
  cat "$FILE" | while read LINE ; do
    N=$((N+1))
    STE="$STE""dog"
  done
done
echo "$STE" && exit

and produces:
cat

Why is there a difference in the two methods? I would rather not be changing environmental variables and a simple loop is my preferred choice.

Thanks for your help, as this has been driving me crazy...
Stephen
# 2  
Old 01-26-2010
The difference lies in the use of a pipe if you are using bash. This would work in ksh
Code:
cat "$FILE" | while read LINE ; do
    N=$((N+1))
    STE="$STE""dog"
  done

In bash (contrary to ksh) the last command in the pipe does not run in the current process so the variable STE loses its value once it finishes.

S.
# 3  
Old 01-26-2010
So if I want to use BASH (the company standard), the only option is to change the IFS variable? And then change it back again?

Thanks,
LITW
# 4  
Old 01-26-2010
As Scrutinizer mentioned, with bash the commands after the pipe run in a subshell and variables in a subshell are not visible outside the block of code in the subshell and they are not accessible to the parent process.

You can avoid the use of a pipe (and useless use of cat) like:

Code:
STE="cat"
  
for FILE in $( ls *-df | cut -f 4 -d / ); do
  N=0
  while read LINE ; do
    N=$((N+1))
    STE="$STE""dog"
  done < "$FILE"
done
echo "$STE" && exit

# 5  
Old 01-26-2010
Thanks to you both, I am really glad I joined this forum now Smilie

Works like a charm. A bit of a follow up question though, is it bad practise to change the IFS variable like in the first example?

Thanks again,
LITW
# 6  
Old 01-26-2010
If you don't read lines (filenames) with spaces it isn't necessary to change the IFS.

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with String concatenation

I have a script which is migrated from AIX to Linux & now while running it is no able to concatenate string values The string concatenation step under while loop is not displaying desired result Please find below the piece of code: while read EXT_FILE ; do EXT_FILE=$EXT_FILE.ext.sent echo... (7 Replies)
Discussion started by: PreetArul
7 Replies

2. Shell Programming and Scripting

String variable concatenation through loop problem

Hi Team!! Please can anyone tell me why the following line does not work properly? str3+=$str2 it seems that str3 variable does not keep its value in order to be concatenated in the next iteration! Thus when i print the result of the line above it returns the str2 value What i want to do is to... (8 Replies)
Discussion started by: paladinaeon
8 Replies

3. Shell Programming and Scripting

Concatenation in awk not working

Hello I want to achieve the following. However the concatenation is not working mv `ls -ltr *myfile*.log|awk '{print $9}'` `ls -ltr *myfile*.log|awk '{print `date +'%d%m%y%k%M%S'` $9}'` I tried awk '{x=`date +'%d%m%y%k%M%S'` print $x "" $9}' awk '{x=`date +'%d%m%y%k%M%S'`... (2 Replies)
Discussion started by: Chetanz
2 Replies

4. Shell Programming and Scripting

String concatenation problem

Hi there, I'm writing a basic script where I want to make a string of 2 numeric fields from a file, which I have done, but the behavior is rather confusing. I have a file of random values such as: 1 2 3 4 5 6 7 8 9 10 and my awk code is: BEGIN { FS = " " } { str = str $1 $2 } END {... (7 Replies)
Discussion started by: HMChadwick
7 Replies

5. Shell Programming and Scripting

String concatenation

Hi, I have two files. cat file.txt a b c d cat file1.txt j k l m I need the output as a:j (12 Replies)
Discussion started by: nareshkumar522
12 Replies

6. Shell Programming and Scripting

string comparison not working inside while loop

The string comparison highlighted below is not working fine. Please help: while read line do # Get File name by deleting everything that preceedes and follows Filename as printed in cvs status' output f_name=`echo $line | sed -e 's/^File://' -e 's/ *Status:.*//' | awk '{print $NF}'` ... (4 Replies)
Discussion started by: sudvishw
4 Replies

7. Shell Programming and Scripting

String comparison not working inside while loop

Hi, In the code included below, the string comparision is not working fine. Please help while (( find_out >= i )) do file=`head -$i f.out|tail -1` dir=`dirname $file` cd $dir Status="" if ; then Status=`cvs -Q status... (3 Replies)
Discussion started by: sudvishw
3 Replies

8. Shell Programming and Scripting

String Concatenation

Hi All, I need to concatenate the values in the array into a variable. Currently the code is : for (( i=1 ; i <= $minCount ; i++ )) do var="${var}""${sample_file}" done The output is : /tmp/1/tmp/2/tmp/3/tmp/4/tmp/5/tmp/6/tmp/7/tmp/8/tmp/9/tmp/10 I need a space between... (1 Reply)
Discussion started by: sh_kk
1 Replies

9. Shell Programming and Scripting

String handling is not working inside if loop

Hi All, I am comparing two strings inside an if condition if the strings are same then it should go inside the loop else it should execute code given in else part. But there is a but inside my script Even if the if condition is true it is not going inside the loop also it is executing... (4 Replies)
Discussion started by: usha rao
4 Replies

10. UNIX for Dummies Questions & Answers

string concatenation

my input file contains thousands of lines like below 234A dept of education 9788 dept of commerce 8677 dept of engineering How do i add a delimeter ':' after FIRST 4 CHARACTERS in a line 234A:dept of education 9788:dept of commerce 8677:dept of engineering (7 Replies)
Discussion started by: systemsb
7 Replies
Login or Register to Ask a Question