Visit The New, Modern Unix Linux Community


[SOLVED] UNIX FOR loop to read a variable with multiple values


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] UNIX FOR loop to read a variable with multiple values
# 1  
[SOLVED] UNIX FOR loop to read a variable with multiple values

Hi,

I have a variable which stores file names as a result of find command. I need to delete all these files one by one, i.e. by a loop. Can anyone tell me how can it be done?
The variable
Code:
f2d

has the file names like these
Code:
 
abc.txt
bcd.txt
fff.txt
gef.txt

Now I have used a loop as below;

Code:
 
for i in $f2d
do
   rm $i
done

But this is not giving me the desired output. It is taking all the files at a time and not interating through the loop. Please advise.

I am using
Code:
# ! /bin/sh

# 2  
It is working as you expected..

just check..

Code:
$ echo $f2d
abc.txt bcd.txt fff.txt gef.txt

$ echo "$f2d"
abc.txt
bcd.txt
fff.txt
gef.txt

$ for i in $f2d
do
echo $i
echo "dd"
done

abc.txt
dd
bcd.txt
dd
fff.txt
dd
gef.txt
dd

$ for i in "$f2d"
do
echo $i
echo "dd"
done

abc.txt bcd.txt fff.txt gef.txt
dd

# 3  
Its still not working for me.

Code:
 
for i in $f2d
do 
   echo " Arg is = $i "
done

The output is
Code:
 
Arg is = abc.txt
bcd.txt
fff.txt
gef.txt

The
Code:
Arg is =

should appear each time a file name is written. Please suggest what went wrong.
# 4  
And it does:
Code:
ran:/home/vbe $ echo $0
sh
ran:/home/vbe $ echo "$f2d"
abc.txt
bcd.txt
fff.txt
gef.txt
ran:/home/vbe $ for i in $f2d^Jdo ^J   echo " Arg is = $i "^Jdone
 Arg is = abc.txt 
 Arg is = bcd.txt 
 Arg is = fff.txt 
 Arg is = gef.txt 
ran:/home/vbe $

As pamu already shown previously...
So if it does not work, then you havent given us the exact value in the variable I suspect a line feed or newline with end of line... OR:
Look again carefully at pamu's post... specially the second part... doesnt that remind you of something?
Code:
ran:/home/vbe $ for i in "$f2d"^Jdo ^J   echo " Arg is = $i "^Jdone
 Arg is = abc.txt
bcd.txt
fff.txt
gef.txt


Last edited by vbe; 12-06-2012 at 10:48 AM.. Reason: completing with second code
# 5  
My variable looks like this

Code:
 
f2d=$(find ./ -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' )
 
echo "Files to delete =$f2d"

Output is

Code:
 
Files to delete =./fgh.dat
./ggg.dat
./sss.dat
./test_2

And the FOR loop is still not working. Please advise.
# 6  
And what does just
Code:
echo $f2d

outputs?
# 7  
The output for
Code:
echo $f2d

is

Code:
 
./fgh.dat
./ggg.dat
./sss.dat
./test_2


Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #519
Difficulty: Easy
3,201 = 0b110010000001
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read multiple values into one variable

Hello everybody, I am trying to assign multiple values from text into a single variable in bash. Source TXT: 1 THIS IS THE ENTITY1 NAME1 2 THIS IS THE ENTITY2 NAME2 3 THIS IS THE ENTITY3 NAME3 while read id entity name do printf "$id" "$entity" "$name" done <... (2 Replies)
Discussion started by: mrcrowley
2 Replies

2. Shell Programming and Scripting

Read record from the text file contain multiple separated values & assign those values to variables

I have a file containing multiple values, some of them are pipe separated which are to be read as separate values and some of them are single value all are these need to store in variables. I need to read this file which is an input to my script Config.txt file name, first path, second... (7 Replies)
Discussion started by: ketanraut
7 Replies

3. Shell Programming and Scripting

Passing multiple column values to UNIX variable

sqlplus -s $USER_ID@$SID/$PWD<<EOF>sql_1.txt set feedback off set heading off select 114032 as c_1 from dual ; EOF for i in `cat sql_1.txt` do sh script_1.sh $i Currently i am passing one column value to the single unix variable. How can i pass the values from 2... (2 Replies)
Discussion started by: rafa_fed2
2 Replies

4. Shell Programming and Scripting

[Solved] How do i deal with values on multiple lines?

Hi, I have a file which has the contents: sh-4.2# pwd /tmp sh-4.2# cat servernfiles server1 /var/tmp/file server2 /var/tmp/file1 I want to manage each line one after the other. I have this basic script : #!/bin/sh HOST=`cat /tmp/servernfiles | awk '{print $1}'` CMD=`cat... (6 Replies)
Discussion started by: chandika_diran
6 Replies

5. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

6. 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

7. Shell Programming and Scripting

[Solved] Assigning a value to a variable name then running a loop on these values

Hi, I was wondering if anyone could assist me for (what is probably) a very straightforward answer. I have input files containing something like File 1 Apples Apples Apples Apples File 2 Bananas Bananas Bananas Bananas (4 Replies)
Discussion started by: hubleo
4 Replies

8. Shell Programming and Scripting

Using multiple values for single variable in a loop

Hello Guys, I have a small loop problem as below. I have 3 different values to be used while running the same script - va1="some-value1" va2="some-value2" va3="some-value3" Now I want to use these three variable values to be used for running the same command, like - while... (1 Reply)
Discussion started by: rockf1bull
1 Replies

9. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

10. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

Featured Tech Videos