Help with accidental endless loop


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help with accidental endless loop
# 1  
Old 03-04-2017
Help with accidental endless loop

I was practicing writing simple loops as I am a new bash user and I created this script, which turned out to be an endless loop where the echo output does not stop and I do not see where my mistake is.

Code:
 
#!/bin/bash
echo 'enter a number from 1 to 100'
read number
while [ "$number -le 100 && $number -ge 1" ] 
do      
      echo you entered a good value
done

# 2  
Old 03-05-2017
Hi,
  • The quotes turn the expression $number -le 100 && $number -ge 1 into the string "$number -le 100 && $number -ge 1" . The result is testing whether the string is non-empty. So at any case those quotes should not be there
  • the expression $number -le 100 && $number -ge 1 is not valid syntax. The correct syntax would be [ $number -le 100 ] && [ $number -ge 1 ]
  • Even if the expression would be made valid, then inside the loop no new number is read, giving the user no chance to enter an new entry, so depending on what initial input value was read, either the loop is never entered, or it is run indefinitely.
  • One additional observation: there should be no empty line above the shebang (#!/bin/bash). #! really must be the first two characters in the file for them to have their special meaning.

Last edited by Scrutinizer; 03-05-2017 at 01:44 AM..
# 3  
Old 03-06-2017
Did you want an if ...... then ....... else ........ fi statement block instead of a loop?



Robin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script runs in endless loop

Hi, AM very new to shell scripting and try to run a simple do while loop statement, but it ends up running endlessly. please can anyone assist, dunno what am doing wrong, any useful suggestions will be welcomed. #!/bin/ksh ### To check a running process instance #################... (5 Replies)
Discussion started by: bayoo
5 Replies

2. Shell Programming and Scripting

[Solved] Endless while loop when compare files

Hi All, I've written a script to read 2 files and compare the contents using while loop but somehow when $line is not found in test2, the script will continue looping. Below is my code, pls advise what could went wrong TIA Nick for line in test1.txt | while read line do grep -i... (4 Replies)
Discussion started by: Nick1971
4 Replies

3. Shell Programming and Scripting

KSH - Issue with endless loop.

First time post. I did a search so I didn’t see this specific issue. It seems to be a head scratcher for me. I have an hourly job that on rare occasions, gets into an endless loop. I’ve tried different scenarios but the current version does basically the following. Find all the *.arc files and... (18 Replies)
Discussion started by: Sylvan303
18 Replies

4. UNIX for Dummies Questions & Answers

Accidental deletion of root account

I had created a root account when I installed the Centos 5 into my system. But now the problem I'm facing is that I accidently deleted the root user account in my system. Is there a way to recreate the root account in the system now, without reinstalling the OS? Pls help. (1 Reply)
Discussion started by: anaigini45
1 Replies

5. Shell Programming and Scripting

Preventing an endless loop with recursive grep

When finding a string in files within a directory, one can use this: grep -r "searchstring" dir/subdir/ > listofoccurrences.txt For brevity sake one can enter the intended directory and use this: grep -r "searchstring" . > listofoccurrences.txt which as I found out leads to an endless loop,... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

[PHP] endless loop mimics a cron. Make sure only one instance is running

Hi, PHP user here. I'm using an endless loop to perform to mimic a cron. The script does something every 20 minutes. It sleep()s in the meantime. I have various checks that ensure that only instance can run, including a "gentleman agreement" locked file. However, I'd like to make sure... (2 Replies)
Discussion started by: jjshell
2 Replies

7. UNIX for Dummies Questions & Answers

Protect .profile from accidental delete

Hi, Is there a way to protect users from deleteing their .profile ? For the majority of our users I created a captive login by a .profile that starts a menu-script. In this menu a user can only start our applications and logoff. This prevents users from getting to the unix-prompt. ... (7 Replies)
Discussion started by: picard
7 Replies

8. Shell Programming and Scripting

Endless Loop

Hi, I'm pretty new to UNIX shell scripting and need some help. We have an Informatica interface that dumps any files that have errors into a directory. I need to check that directory periodically for any of up to 9 files that might be in it and run a specific process for each file found. The... (3 Replies)
Discussion started by: JeffR
3 Replies

9. Shell Programming and Scripting

How to remove accidental file

Hi, I have a strange problem. I accidentally created a file named ${1}_$(date+%Y%m%d) and when i am trying to remove it I am getting this error . Can any one suggest me how i can remove this accidental file. (4 Replies)
Discussion started by: dsravan
4 Replies

10. Shell Programming and Scripting

Endless loop - Fork function failed?

I need a quick script that will serve as a sort of "real time monitor" for watching some log files. I am using Bourne shell in HP-UX 10.20. I have basically created a script that never ends, unless of course I manually terminate it. Here's the script (it's called qhistory): clear echo "REAL... (3 Replies)
Discussion started by: cdunavent
3 Replies
Login or Register to Ask a Question