The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
while loop inside while loop panknil Shell Programming and Scripting 0 01-07-2008 12:49 PM
For loop xramm HP-UX 3 10-10-2007 02:20 PM
While Loop hemangjani Shell Programming and Scripting 2 11-02-2006 11:01 AM
for loop munnabhai1 Shell Programming and Scripting 3 04-06-2006 02:30 PM
how to get the similar function in while loop or for loop trynew Shell Programming and Scripting 3 06-17-2002 11:09 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-08-2005
smtpgeek smtpgeek is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 11
for loop help

Hello-

I am new to the forum(forst post) and I am enthralled with the power of programming. I am writing a shell script to go into an apache config to add a ServerAlias mine to about 400 virtual servers.

I am probably approaching this in the most difficult of ways since I am not familiar with the commands. BUT...

The first question I have is how to use a file with a list in a for loop. So I have a file, named file.txt and it has 500 lines in it. I want the for loop to assign each line to the variable to process.

I tried something like this:

for i in 'cat file.txt'
do

this does not work since it wants to do something to "cat file.txt" How can I get it to process the list which is in the file.txt


-Bob
  #2 (permalink)  
Old 11-08-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Code:
#!/bin/ksh

for iter in $(< file.txt )
do
   echo "line-> [${iter}]"
done
  #3 (permalink)  
Old 11-08-2005
sssow sssow is offline
Registered User
  
 

Join Date: Aug 2001
Posts: 179
You may be having single quotes..Use backticks instead and it should work

for i in `cat file.txt`
do
echo $i
done

or

for i in $(cat file.txt)
do
echo $i
done
  #4 (permalink)  
Old 11-08-2005
smtpgeek smtpgeek is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 11
Wow, you guys are fast!!

The problem I am running into is that the lines have two space delimited words on them. I would like to get the whole line into the the $i. How would I do that?

-Bob
  #5 (permalink)  
Old 11-08-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
Quote:
Originally Posted by smtpgeek
Wow, you guys are fast!!

The problem I am running into is that the lines have two space delimited words on them. I would like to get the whole line into the the $i. How would I do that?

-Bob
Code:
#!/bin/ksh

while read iter
do
   echo "line->[${iter}]"
done < file.txt
  #6 (permalink)  
Old 11-08-2005
smtpgeek smtpgeek is offline
Registered User
  
 

Join Date: Nov 2005
Posts: 11
That, my friends works very well. I hope you do mot mind me following up a more difficult question. This is where you may realize that my algorithm may not be up to the preschooler level.

If I knew regular expressions better, I would have probably attacked this directly with that. I am working within my framework of limited knowledge of the commands. I know that there are all kinds of resources on the web to learn this, but that is the problem.

Anyway, dont laugh....too hard. I am using

#!/bin/sh

#make a list of the lines I need to use- edit and append on next line
sed '/ServerName/!d; /domain.com/!d' httpd.conf > temp.txt

#thankyou kindly for the help
while read i
do

#I am trying to change add a ServerAlias to each of the sub-domains. Dont ask, #these customers are nuts. This is my attempt and does not work.
convert=$(sed 's/ServerName /ServerAlias www./g')

# would then try to use sed to append the line right under the original($i) I got.
sed '/$i/ a\
$convert'
done < temp.txt

Maybe you could let me know of a better way to go about doing this. I am realizing this approach is flawed.

-Bob
  #7 (permalink)  
Old 11-08-2005
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
could you post an input file AND the desired output, pls - it might be easier to understand the objective.

change the confidential data to something meaningless if there's a concern.
Closed Thread

Bookmarks

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:04 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0