The UNIX and Linux Forums  

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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
simple for loop ali560045 Shell Programming and Scripting 3 12-16-2007 10:39 PM
Simple BASH script? JayC89 Shell Programming and Scripting 16 10-02-2007 04:23 PM
Bash while loop problem Kweekwom Shell Programming and Scripting 5 07-22-2007 09:49 PM
simple bash script to ftp? satnamx Shell Programming and Scripting 1 04-21-2006 08:18 AM
Simple Bash Script xaphalanx Shell Programming and Scripting 3 12-21-2005 12:54 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 09-14-2006
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
Simple bash for loop problem

I'm just trying to make a script that runs in command line to echo each line in a text file. Everything i found on google is telling me to do it like this but when I run it it just echos removethese.txt and thats it. Anyone know what im doing wrong?

for i in removethese.txt; do echo $i; done

but if I do this

for i in 1 2 3; do echo $i; done

it works...

essentially i need it to run a command for me and not just echo but I was testing it with an echo first and I can't get that to even run.... *sigh*
Reply With Quote
Forum Sponsor
  #2  
Old 09-14-2006
Registered User
 

Join Date: Aug 2006
Posts: 22
Try this:

while read aLine
do
printf "\n $aLine"
done < removethese.txt
Reply With Quote
  #3  
Old 09-14-2006
Registered User
 

Join Date: Jun 2006
Location: Las Vegas, NV
Posts: 104
sweet, thanks!
Reply With Quote
  #4  
Old 09-14-2006
inquirer's Avatar
Registered User
 

Join Date: Aug 2001
Posts: 78
the reason this did not work:

Code:
for i in removethese.txt; do echo $i; done
is because the for loop does not know what to do with the removethese.txt file. it does not accept files as an input. you can add cat command or similar commands that will read or extract contents of the file then feed them to the for loop. in this case using cat command:

Code:
for i in `cat removethese.txt`; do echo $i; done
thanks
Reply With Quote
  #5  
Old 09-14-2006
Registered User
 

Join Date: Aug 2006
Posts: 30
Talking a dirt code written by me once upone a time........

Code:
n=`wc -l $1 | awk '{print $1}'`
for ((i=1;i<=$n;i++))
do
        echo `head -$i $1 | tail -1`
done
this will also work....preferably, dont use the above code for very huge files(>10mb)
or else u can peacefully use the scripts mentioned earlier
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:36 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0