problem with while loop in BASH shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting problem with while loop in BASH shell
# 1  
Old 11-08-2008
problem with while loop in BASH shell

I have file named script1 as follows:

#!/bin/bash
count="0"
echo "hello"
echo "$count"
while [ $count -le 5 ]
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 have typed this in Textpad and I am running it in Cygwin..

I checked in textpad and there is no newline at the end of done..

also to invoke the script i am using ./script1 on the command prompt in the terminal window
# 2  
Old 11-08-2008
Well it works for me.
Code:
bash-3.00$ cat x
#!/bin/bash
count="0"
echo "hello"
echo "$count"
while [ $count -le 5 ]
do
echo "$count"
count=`expr $count + 1`
done

bash-3.00$ ./x
hello
0
0
1
2
3
4
5
bash-3.00$

# 3  
Old 11-08-2008
Could be a problem with the file format, try to remove the CR's with:

Code:
tr -d '\r' < your_file > newfile

# 4  
Old 11-08-2008
ok it worked after removing the carriage returns!
thanks a ton..
# 5  
Old 11-08-2008
ok that helped but now i have this code:

#!/usr/bin/bash
bininfotable=(20 20 20 20 20)
j=0
#find . -name "bin.txt" -type f -exec cat {} \; > nikhil.txt
while [ ${j} -le 5]
do
awk '/%/' /cygdrive/c/unix/bin.txt | awk '{print $3}'> $[collectbininfo[i]]
if [$[collectbininfo[j]] >= $[bininfotable[j]]] then
#$file_to_send = $[collectbininfo[i]]
echo "!! WAKE UP TIME TO ACT!!"
echo You have an error! Please see attached file
end if
j='expr $j +1'
done
-------------------------------

this gives me same error even after removing the carriage returns..
# 6  
Old 11-09-2008
I would suggest you to convert the file formats from DOS to UNIX using any of the standard text editors(emacs).

How to Convert DOS and UNIX text files &mdash; Vasudeva Service
# 7  
Old 11-09-2008
thanks it worked..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash Shell loop - Help !

Dear all Linux lover, I am a new learner to Bash Shell script and I would like to writing a script to to repeat my script. This mean I would like to have multiple same of result after running the .sh. ####### TIMES_NO=0 echo -n "Please enter the number for times to repeat ?" read... (10 Replies)
Discussion started by: Rocky888
10 Replies

2. UNIX for Dummies Questions & Answers

For loop in bash shell

Hi, I am using a for loop to manipulate files data_1.txt through data_100.txt. The for-loop is set up like this: for i in {1..100}; do cut -f1 data_$i.txt > output$i.txt I get the following error message when I run the code: cannot open `data.txt' for reading: No such file or directory... (4 Replies)
Discussion started by: evelibertine
4 Replies

3. UNIX for Dummies Questions & Answers

Problem with multiple grep in bash loop

Hello, I am trying to create a matrix of 0's and 1's depending on whether a gene and sample name are found in the same line in a file called results.txt. An example of the results.txt file is (tab-delimited): Sample1 Gene1 ## Gene2 ## Sample2 Gene2 ## Gene 4 ## Sample3 Gene3 ... (2 Replies)
Discussion started by: InfoSeeker2
2 Replies

4. Shell Programming and Scripting

problem access array outside of loop in bash

Below is a test script I was trying to use so that I could understand why the logic was not working in a larger script. While accessing and printing array data inside the while loop, everything is fine. Outside the loop, i guess everything is null?? The for loop that is meant to cycle... (4 Replies)
Discussion started by: adlmostwanted
4 Replies

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

6. Shell Programming and Scripting

bash & Ksh loop problem

hi i was trying to optimize one script and i came across this problem .. i am putting some pseudo code here $ >cat a.sh ls | while read I do i=$(($i + 1)) done echo "total no of files : " $ >ksh a.sh total no of files : $ >bash a.sh total no of files : why is... (1 Reply)
Discussion started by: zedex
1 Replies

7. UNIX for Dummies Questions & Answers

Anyone know?: How the 'for'-loop could stop working in interactive bash shell?!

It is happening with my sessions already second time: a 'for'-loop for some reason stop to work as expected. That means or it is looping without exitting, or it is not loop even once. Here example of my try when it is not processing even one loop. You can see, I start new subshell and... (14 Replies)
Discussion started by: alex_5161
14 Replies

8. Shell Programming and Scripting

if loop not working in BASH shell

i have this code for a simple if loop: #!/bin/bash array="1 2 3 4 5" array2="5 6 7 8 9" if } -gt ${array} ]; then echo "${array2} is greater than ${array}!!" fi the error is ./script8: line 9: [: too many arguments ./script8: line 9: [: too many arguments ./script8: line 9: [:... (10 Replies)
Discussion started by: npatwardhan
10 Replies

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

10. Shell Programming and Scripting

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 ... (4 Replies)
Discussion started by: kingdbag
4 Replies
Login or Register to Ask a Question