problem with for loop and a variable with spaces...Hi


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with for loop and a variable with spaces...Hi
# 8  
Old 04-21-2010
Thanks alister, your command works fine.
Thanks also for your warning, I will be very touchy with input.

Thanks radoulov for helping but your commands does not work with the sentence in a variable.
# 9  
Old 04-21-2010
Quote:
Originally Posted by chebarbudo
[...]
Thanks radoulov for helping but your commands does not work with the sentence in a variable.
Actually I was suggesting to use an array variable instead of scalar variable ...
# 10  
Old 04-21-2010
Yes radoulov, you're right.
Your idea is indeed interesting but I don't think I can easily change my script commands from variable to array.
Your sed example doesn't work with a variable either but I just learned a powerfull command (set).
Thanks for your help anyway.
# 11  
Old 05-08-2010
Hi there,

I don't know if you're still following this thread but I found a way to efficiently use both your ideas.

Because the sentence was read as a string (not an array), I had no other way than using alister's idea:
Quote:
Originally Posted by alister
Code:
$ eval "for word in $sentence; do echo \$word; done"
un
deux trois

But I also had a lot of statements to put in my for loop. So it would have become a little messy:
Code:
$ eval "for word in $sentence; do action1 \$word; action2 \$word; action3 \$word; action4 \$word...; done"

At that point, radoulov's idea was interesting because it left the for loop open to any statement:
Quote:
Originally Posted by radoulov
Code:
sentence=(un "deux trois")
for w in "${sentence[@]}"; do
  printf '%s\n' "$w"
done

So I combined the two ideas and came up with this:
Code:
# That's how I read the sentence anyway:
sentence='un "deux trois" quatre'
# Then I turn it into an array:
eval "sentence=($sentence)"
# Et voila!
for word in "${sentence[@]}"; do
    action1 "$word"
    action2 "$word"
    action3 "$word"
    action4 "$word"
    ...
done

Thank you both!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Awk: problem for loop through variable

Hi, input: AAA|1 my script (the function is just an example): gawk 'BEGIN{FS=OFS="|"} function repeat(str, n, rep, i){ for(i=1; i<=n; i++) rep=rep str return rep } { variable_1=repeat($1,$2) variable_2=repeat($1,$2+1) variable_3=repeat($1,$2+3) ... (5 Replies)
Discussion started by: beca123456
5 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

Array Variable being Assigned Values in Loop, But Gone when Loop Completes???

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... (5 Replies)
Discussion started by: mrm5102
5 Replies

4. Shell Programming and Scripting

Loop with Find—damn spaces!

Hi Guys, I'm trying to find all files with a particular extension and then loop some actions. The problem is that if the files have spaces in their names I get end up being each word as a separate result rather than the entire file. ext=".txt" out=".rtf" for i in $( find "$1" -name "*$ext" );... (9 Replies)
Discussion started by: imonkey
9 Replies

5. Shell Programming and Scripting

Bourne Shell - Problem with while loop variable scope.

Hello I am having issues with a script I'm working on developing on a Solaris machine. The script is intended to find out how many times a particular user (by given userid) has logged into the local system for more than one hour today. Here is my while loop: last $user | grep -v 'sshd'... (7 Replies)
Discussion started by: DaveRich
7 Replies

6. Shell Programming and Scripting

for loop ( string having spaces )

Dear All, i facing problem to use string having spaces in for loop.. file used for FOR LOOP command.txt rpm -t -v ttm -D -r RJLL -h YELP rpm -t -v ttm -D -r RJLL -h ERRT rpm -t -v ttm -D -r RJLL -h TYYE rpm -t -v ttm -D -r RJLL -h POOL CODE using for execute above command... (3 Replies)
Discussion started by: arvindng
3 Replies

7. UNIX for Dummies Questions & Answers

For loop using find with file name spaces

Hello All, This question is actually for the service console of VMware ESX 3.5 but is relevant to this forum I think. I have been advised to use the following commands: for i in `find /vmfs/volumes/Test_VMFS/ -name "*.vmx"` do echo "$i" #sed -i 's/scsi1:0.present =... (3 Replies)
Discussion started by: mronsman
3 Replies

8. Shell Programming and Scripting

Variable problem in for loop with if statement

Hi, Again a little problem. Do not understand good why an empty string is not detected. Here is the program: #!/bin/ksh APR=`date | grep Apr | awk '{print $2$3}'` MAY=`date | grep May | awk '{print $2$3}'` JUN=`date | grep Jun | awk '{print $2$3}'` echo "Variable Apr has value:... (6 Replies)
Discussion started by: ejdv
6 Replies

9. Shell Programming and Scripting

Problem with variable expension using for loop.

Hi, I have a problem with expending variables when used in a for loop: #!/bin/ksh VAR1=aaa VAR2=bbb VAR3=ccc for ITEM in VAR1 VAR2 VAR3 do echo "${ITEM}" done This gives: VAR1 VAR2 VAR3 (2 Replies)
Discussion started by: ejdv
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question