Use file as list in for loop...


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Use file as list in for loop...
# 8  
Old 06-14-2007
Quote:
Originally Posted by earnstaf
Can I use a file as the list in a for loop? I tried it like this:

Code:
workstation=( $(cat /home/me/file) )
for ws in $workstation

Code:
Code:
workstation=$(cat /home/me/file)
for ws in $workstation
Or:
Code:
for ws in "${workstation[@]}"
Those will work with the example file you gave, but it is almost always the wrong way to read a file.
Quote:
do command done

Quote:

The contents of file are like:

abc
def
ghi

It only read the initial line.

Does each item have to be quoted? Do they have to all be on the same line? ...or is it not possible at all?

Thanks for your help.

The usual way to process a file in a script is:

Code:
while IFS= read -r line
do
   command
done < /home/me/file

# 9  
Old 06-14-2007
Quote:
Originally Posted by porter
according to the specification of what the input file looks like, this is what was requested.
what WAS requested was 'a solution' which you did provide, but what I am saying there's no need for a 'cat' here and 'cat' here is UUOC here.
# 10  
Old 06-17-2007
hey u can do like this

workstation=`cat /home/me/file`
for ws in $workstation
do
command
done
# 11  
Old 06-18-2007
When thinking about a solution to read a file, size is for sure
the main consideration.
The solution of placing the entire file into a single variable
is definitely a bad solution for large files.
# 12  
Old 06-18-2007
Quote:
Originally Posted by Shell_Life
When thinking about a solution to read a file, size is for sure
the main consideration.
The solution of placing the entire file into a single variable
is definitely a bad solution for large files.
Understood.. thanks for clearing that up.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Efficient way to loop through list

i have a variable that contains a list of files: varA="/var/tmp/hello_0_myapp /var/tmp/mello_1_myapp /var/tmp/jello_2_myapp /var/tmp/fello_3_myapp" And i'm calling this variable in several functions: col0 () { FILE=$(echo "${varA}" | egrep "_0_myapp" | sed "s~_0_myapp~~g")... (5 Replies)
Discussion started by: SkySmart
5 Replies

2. Shell Programming and Scripting

While loop a file containing list of file names until the files are found?

Hi, I have a control file which will contain all filenames(300) files. Loop through all the file names in the control files and check the existence of this file in another directory(same server). I need to infinitely(2 hrs) run this while loop until all the files are found. Once a file is found,... (5 Replies)
Discussion started by: laknar
5 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Homework & Coursework Questions

Loop to Convert a list from an input file and output it to another file

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: A) Write a script, which will take input from a file and convert the number from Centigrade to Fahrenheit... (5 Replies)
Discussion started by: AliTheSnake
5 Replies

5. Shell Programming and Scripting

scp list of files using FOR LOOP in ksh

hi i want to scp files from remote server B to my local server A... and i have a file containing list of all files to be scped from remote server B there is a passwordless connectivity set between remote server and my local server. need a ksh script.. with a for loop that goes through... (2 Replies)
Discussion started by: billpeter3010
2 Replies

6. UNIX for Dummies Questions & Answers

Iterate/Loop Through XML Node List

I need to load an XML file and loop through a list of nodes in it to execute a shell script for each one using the attributes for each node as parameters for the script. Any ideas? Any help will be much appreciated. (1 Reply)
Discussion started by: bradlecat
1 Replies

7. Shell Programming and Scripting

How to loop(Iterate) through List with foreach(csh)

Hey all,, I know cshell is harmful:) but I am using this just "to know" - for educational purposes!... not for a long-term use. lets say i have a list.. set arr=(x y z e f) I wanna iterate the list with foreach ,, not with while.!! foreach i $arr echo $i end does not work (2 Replies)
Discussion started by: eawedat
2 Replies

8. UNIX for Dummies Questions & Answers

For Loop for a list of tab delimited variables

Hello, I need to run a command for a set of input variables that are present in a tab delimited file, a sample of which is shown below: 1 3749 1 4129 1 5980 2 6201 2 9925 2 6894 3 1338 3 6477 3 6242 3 3632 Every row represents the two input values... (2 Replies)
Discussion started by: Gussifinknottle
2 Replies

9. UNIX for Dummies Questions & Answers

List & While Loop

I want to store a set of string values in a 1-D array and then iterate the list by using a loop. Please help this dummy:) (1 Reply)
Discussion started by: ngagemaniac
1 Replies

10. Shell Programming and Scripting

while loop from list

is it possible to run a while loop by comparing a variable with a list ? taglist="<html> </html> <head> </head> <body> </body> <p> </p> <ul> </ul> <li> </li> <em> </em> <title> </title>" read newtag while ] do echo "Entry is not in list, please try again' read newtag done... (6 Replies)
Discussion started by: forever_49ers
6 Replies
Login or Register to Ask a Question