[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
# 8  
Old 12-07-2012
You must have altered IFS somewhere, since that string isn't even splitting while unquoted.
# 9  
Old 12-11-2012
Yes, I am using IFS at an earlier place and it is very much required there. The below piece of code shows that.

Code:
 
IFS=' '
while read -r dir; do
(
        cd $dir
        files2del=$(find ./ -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' )
        echo "$files2del"
for i in $files2del
                do
                        echo "Arg is ="
                done
)
done < dirlist_1.txt

Could you please advise how do I get the for loop correct.
# 10  
Old 12-11-2012
You can just specify IFS for the read instead of changing it for the entire program:

Code:
while IFS=" " read -r dir; do
(
        cd $dir
        files2del=$(find ./ -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' )
        echo "$files2del"
for i in $files2del
                do
                        echo "Arg is ="
                done
)
done < dirlist_1.txt

This should let $files2del split on any whitespace.
# 11  
Old 12-11-2012
But really, you shouldn't be putting an open-ended list like that into a variable. I don't think you need to cd either.

Code:
while IFS=" " read -r dir
do
        find "$dir" -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' | while read i
        do
                echo "got file $i"
        done
done < dirlist_1.txt

# 12  
Old 12-11-2012
Or even just:

Code:
while IFS=" " read -r dir
do
        find "$dir" -type d ! -name . -prune -o -mtime +$NUM_DAYS -name '*.dat' -exec command_to_run_for_each_file '+'
done < dirlist_1.txt

This User Gave Thanks to Corona688 For This Post:
# 13  
Old 12-13-2012
Its working perfectly now. Thanks to all !!!
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question