Problem taking input from file with for loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem taking input from file with for loop
# 1  
Old 12-03-2008
Problem taking input from file with for loop

I am trying to take input from a file and direct it into a bash script. This script is meant to be a foreach loop. I would like the script to process each item in the list one by one and direct the output to a file.


[root@uverse ~]# cat 1loop
#!/bin/bash
# this 2>&1 to redirect STDERR & STDOUT to file
LIST=$1
for i in "$LIST"; do
ping -c 2 "$LIST"; &> /root/error_mess
done

I have a file called ipaddresses with one ip per line.
This script works if I supply one ip after the script name upon execution, but not like this
#./1loop < ipaddresses
# 2  
Old 12-03-2008
You can read the file like this:

Code:
#!/bin/bash

while read line
do
  # do something with "$line"
done < ipaddresses

Regards
# 3  
Old 12-03-2008
Thanks It seems so easy when you show me how.
BTW it works well.
# 4  
Old 12-03-2008
Stderr & stdout

I am unsure where and how to place stderr and stdout to a file in this while loop. I have attempted many ways and it is not working.
# 5  
Old 12-03-2008
Something like this?

Code:
#!/bin/bash

while read line
do
  ping -c 2 "$line" >> /root/error_mess 2>&1
done < ipaddresses

Regards
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem in taking input Shell script

I was writing a shell script, where i need to run a command through script and then take input from user and later terminate it. i am not sure how to take input in a proper format immediately after a command. example: below command line is used to import some data in to database (LDAP)... (3 Replies)
Discussion started by: Mridul17
3 Replies

2. Shell Programming and Scripting

Search pattern in a file taking input from another file

Hi, Below is my requirement File1: svasjsdhvassdvasdhhgvasddhvasdhasdjhvasdjsahvasdjvdasjdvvsadjhv vdjvsdjasvdasdjbasdjbasdjhasbdasjhdbjheasbdasjdsajhbjasbjasbhddjb svfsdhgvfdshgvfsdhfvsdadhfvsajhvasjdhvsajhdvsadjvhasjhdvjhsadjahs File2: sdh hgv I need a command such that... (8 Replies)
Discussion started by: imrandec85
8 Replies

3. Shell Programming and Scripting

While loop problem taking too long

while read myhosts do while read discovered do echo "$discovered" done < $LOGFILE | grep -Pi "|" | egrep... (7 Replies)
Discussion started by: SkySmart
7 Replies

4. Shell Programming and Scripting

Installing a bin file by taking input from a properties file

I need to install a bin file in UNIX which requires user interaction for giving some information like user id , path, sid etc. All these information is stored in a properties file in the same location. So if i give ./file.bin -f propfile.properties will it install the file taking all the... (1 Reply)
Discussion started by: coolmohere
1 Replies

5. Shell Programming and Scripting

How to ftp multiple files by taking the file name from a input file.

Hi, I'm working on a script which has to copy multiple files from one server to another server. The list of files that are to be copied is present in a file say input.txt. vi input.txt abc.c welcome.c new.c welcome1.c for ftp'ing a single file say 'new.c' the following code... (2 Replies)
Discussion started by: i.srini89
2 Replies

6. Shell Programming and Scripting

Shell Script to execute Oracle query taking input from a file to form query

Hi, I need to query Oracle database for 100 users. I have these 100 users in a file. I need a shell script which would read this User file (one user at a time) & query database. For instance: USER CITY --------- ---------- A CITY_A B CITY_B C ... (2 Replies)
Discussion started by: DevendraG
2 Replies

7. Shell Programming and Scripting

Taking one input at a time

Legends, Please help me to come out of the below Bermuda triangle. I have four inputs in a shell script: A B C D Now, If A is passed by user then, B C D will be ignored. If C is passed by user, then A B D will be ignored. Regards, Sandy (11 Replies)
Discussion started by: sdosanjh
11 Replies

8. Shell Programming and Scripting

Problem while taking input from User

Hi All, I am facing a problem while taking input from user in Shell Script. Basically , I want to read a file line by line. And I want to remove certain lines of that file.But before removing those lines , I need to ask the user whether user wants to remove the lines. If user inputs "Yes" , the... (3 Replies)
Discussion started by: swapnil.nawale
3 Replies

9. UNIX for Dummies Questions & Answers

Taking a Users input to for the basis of a Grep

Hi I'm currently putting together a script that will grep a set of data for a particular input, specified by the user. I'm getting myself all tangled up with my Echo's and reads. What i'm looking to do is get it so that it prompts the user to enter a value (echo) and then uses their input... (5 Replies)
Discussion started by: Great Uncle Kip
5 Replies

10. Shell Programming and Scripting

need help with using input file in the while loop

here's my code: ls -lt | /usr/bin/egrep `date +"%Y%m%d(05|06|07|08|09|10|11|12|13|14|15|16|17|18|19)"` | \ /usr/bin/egrep -i .dat | /usr/bin/awk '{print $9}' > $LockboxFile1 if ] then ... (2 Replies)
Discussion started by: sytycd712
2 Replies
Login or Register to Ask a Question