Why doesn't this loop end?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Why doesn't this loop end?
# 1  
Old 05-17-2009
Why doesn't this loop end?

Simple script, takes an cmd line argument and counts down to 1.

Code:
NUMBER=$1
# One argument must be provided, otherwise don't execute
if [ $# != 1 ]
then
    echo "Error. Enter one argument "
    exit 0
elif [ $# == 1 ]
then
        echo " "
fi
# Integer value must be greater than zero
while [ $NUMBER > 0 ]
do
   echo "$NUMBER"
# If the value of the argument is greater than one, insert a comma after the integer
   if [ $NUMBER > 1 ]
   then
     echo ", "
fi
# Decrease the value of NUMBER by one with each iteration
NUMBER=$(($NUMBER - 1))
done
echo "$NUMBER"

two issues I am working, the first is that the loop does not end, it keeps counting down forever. The second problem, I don't want each # and , on a newline, I want them all on the same. I did this in PERL and it works fine, now I need to write it as a bash script having issues.
# 2  
Old 05-17-2009
while [ $NUMBER -gt 0 ]
numeric comparison.
# 3  
Old 05-17-2009
Quote:
Originally Posted by jim mcnamara
while [ $NUMBER -gt 0 ]
numeric comparison.
The output keeps running into negative integers forever, shouldn't the loop terminate once the condition of the variable becomes -le 0?
# 4  
Old 05-17-2009
Should work if you change the line as suggested by Jim.

Regards
# 5  
Old 05-17-2009
Quote:
Originally Posted by Franklin52
Should work if you change the line as suggested by Jim.

Regards

Hmmm, I thought '>' was the same a '-gt', but I made the change and it worked. I am still learning the in & outs of all of this, thanks.
# 6  
Old 05-17-2009
[ "$x" > "$y" ] is for comparing string variables not numbers.
# 7  
Old 05-17-2009
Quote:
Originally Posted by jim mcnamara
[ "$x" > "$y" ] is for comparing string variables not numbers.
It is just like
test "$x" > "$y"
so it means: create file $y and fill it with the output of the command test "$x".
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Do loop doesn't iterate

I'm trying to send the file list as parameter to another job and execute it. But the loop doesn't work, the inner job is running only once and not twice as expected for filelist in $(ls -rt *.txt | tail -2) do echo $filelist export filelist cmd="$Program -config $configfile -autoexec... (11 Replies)
Discussion started by: asandy1234
11 Replies

2. Shell Programming and Scripting

Nested While loop doesn't end

Hi, Below is my script in which i am using nested while loop to read two files and move the files to a remote server. My issue is that the 2nd while loop doesn't stop executing and it keeps on executing. Can someone please let me know where i have gone wrong. myFile=$ESER_TEST_FILES ... (2 Replies)
Discussion started by: funonnet
2 Replies

3. Shell Programming and Scripting

for loop two commands at the end

cat vfiler_volumes.list | while read var1 var2 ; do ssh -n "$var1" df -h "$var2" >> test this part works, however i would like to add an echo $var1 >> test before each ssh iteration. any pointers ? thanx (1 Reply)
Discussion started by: riegersteve
1 Replies

4. Shell Programming and Scripting

Script doesn't work in loop but does if not

I have a script that only works if I remove it from the looping scenario. #!/bin/bash # Set the field seperator to a newline ##IFS=" ##" # Loop through the file ##for line in `cat nlist.txt`;do # put the line into a variable. ##dbuser=$line echo "copying plugin..." ... (6 Replies)
Discussion started by: bugeye
6 Replies

5. Shell Programming and Scripting

For loop using input file doesn't expand variables

Hi, I'm using a for loop reading from an input file that contains files, whose path includes a variable name. But the for loop doesn't expand the variable and therefore can't find the file. Here's an example: File BACKUPFILES /home/John/alpha /home/Sue/beta... (8 Replies)
Discussion started by: Hesiod
8 Replies

6. Shell Programming and Scripting

tail -XXX with grep doesn't work in while loop

Hi all, I need some help. my shell script doesn't work especially in the loop. #!/bin/sh -xv export ORA_ADMIN=/oracle/home/admin export ORACLE_SID=ORA_SID cat ${ORA_ADMIN}/param_alert_log.ora | while read MSG do #echo $MSG #echo "tail -400... (8 Replies)
Discussion started by: sidobre
8 Replies

7. Shell Programming and Scripting

for loop doesn't work

I have a script which uses below for loop: for (( i = 0 ; i <= 5; i++ )) do echo "Welcome $i times" done But when I run the script, it gives error message: Syntex Error : Bad for loop variable Can anyone guide to run it? Thanks in advance. (10 Replies)
Discussion started by: naw_deepak
10 Replies

8. UNIX for Dummies Questions & Answers

a for loop that doesn't make sense

I've been referring bash info for processes and came across a structure for a process which is defined like typedef struct process { struct process *next; char ** argv . . . }process; What I don't understand is that in the program there's a for loop which goes like this job... (2 Replies)
Discussion started by: sdsd
2 Replies

9. Shell Programming and Scripting

while read loop w/ a nested if statement - doesn't treat each entry individually

Hi - Trying to take a list of ldap suffixes in a file, run an ldapsearch command on them, then run a grep command to see if it's a match, if not, then flag that and send an email alert. The list file (ldaplist) would look like - *********** o=company a o=company b *********** **... (7 Replies)
Discussion started by: littlefrog
7 Replies

10. UNIX for Dummies Questions & Answers

Why script For...Loop doesn't work. Seek help

I have written a script to run on UNIX server. When I tested, it always hanged on after "date +"%D %T: XXXXXX script started." part. Then it wouldn't go further. UNIX server gave me one error message. I used the same code in another script. It works fine. I think the major problem may be in... (3 Replies)
Discussion started by: duke0001
3 Replies
Login or Register to Ask a Question