Simple bash for loop problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple bash for loop problem
# 1  
Old 09-14-2006
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*
# 2  
Old 09-14-2006
Try this:

while read aLine
do
printf "\n $aLine"
done < removethese.txt
# 3  
Old 09-14-2006
sweet, thanks!
# 4  
Old 09-15-2006
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
# 5  
Old 09-15-2006
Computer 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) Smilie
or else u can peacefully use the scripts mentioned earlier Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with simple bash script involving a loop

Dear unix wizards, I'd be very grateful for your help with the following. I have a hypothetical file (file.txt) with three columns: 111 4 0.01 112 3 0.02 113 2 0.03 114 1 0.04 115 1 0.06 116 2 0.02 117 3 0.01 118 4 0.05 Column 2 consists of pairs of integers from 1 to 4 (each... (5 Replies)
Discussion started by: aberg
5 Replies

2. Shell Programming and Scripting

Bash - sftp simple script problem

Hello, when running the scripts below I am not getting message bb2. Can you please help? #!/bin/bash TLOG=/tmp/bb/amatest.log FTPRESULTS=/tmp/bb/amlist export TLOG FTPRESULTS >$TLOG >$FTPRESULTS echo bb1 sftp -oPort=2222 XXXXXXXXXXXXX@sftp.userssedi.com <<EOF cd... (5 Replies)
Discussion started by: biljana
5 Replies

3. Shell Programming and Scripting

a simple for loop in bash and zsh

Hi! I am just starting to learn scripting. I am trying a simple script in bash and zsh I have two questions: First: Why zsh does not expand the var M? What I am doing wrong? localhost galanom # bash -c 'M="m1 m2 m3 m4 m5"; for x in $M; do echo "<$x>"; done' <m1> <m2> <m3> <m4> <m5>... (1 Reply)
Discussion started by: galanom
1 Replies

4. Shell Programming and Scripting

Simple bash script problem

#!/bin/bash cd /media/disk-2 Running ./run.sh it's not changing directory.Why? (6 Replies)
Discussion started by: cola
6 Replies

5. UNIX for Dummies Questions & Answers

simple script with while loop getting problem

Hello forum memebers. can you correct the simple while program. #! /bin/ksh count=10 while do echo $count count='expr$count-1' done I think it will print 10 to 1 numbers but it running for indefinite times. (2 Replies)
Discussion started by: rajkumar_g
2 Replies

6. Shell Programming and Scripting

Simple while-loop problem

Maybe because its Friday, but I can't get a simple while loop to work! #!bin/bash i=0 while do echo "Hello" ((i++)) done (17 Replies)
Discussion started by: linuxkid
17 Replies

7. Shell Programming and Scripting

Problem with for loop in bash

I'm trying to do a script where I want to see if all users home directories are only writable by owner. However, in my script I do not know how to implement the for loop so that all directories are checked. In mine, I am only checking the permissions for the first directory found. I do know that a... (3 Replies)
Discussion started by: detatchedd
3 Replies

8. Shell Programming and Scripting

problem with while loop in BASH shell

I have file named script1 as follows: #!/bin/bash count="0" echo "hello" echo "$count" while do echo "$count" count=`expr $count + 1` done ----------- when I run it, I get ./script1: line 9: syntax error near unexpected token `done' ./script1: line 9: `done' I... (6 Replies)
Discussion started by: npatwardhan
6 Replies

9. Shell Programming and Scripting

Simple loop in Bash

Hi, I want to do a simple loop where I have one column of text in a file and I want the loop to read each line of the file and do a simple command. The text file will be something like this: hostname1 hostname2 hostname3 hostname4 I am using Bash and have already come up with this to... (1 Reply)
Discussion started by: BrewDudeBob
1 Replies

10. Shell Programming and Scripting

Bash while loop problem

Hi, I'm having a problem with the while loop in bash. I try the following script: #!/bin/bash while true do echo "test" done When I try this, it gives me this error: while: Too few arguments. What am I doing wrong? Thanks (5 Replies)
Discussion started by: Kweekwom
5 Replies
Login or Register to Ask a Question