LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-03-2003, 09:02 PM   #1
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Rep: Reputation: 15
script help


I am working on a labb assn. for class. I have to write a script the replicates the seq. command. I have it working pretty good but if i enter negative number in all three params i go into a infinite loop any help understand where i am going wrong ?????

#!/bin/bash
#Kevin L. Freese
#Week 7 lab

#Display How to message.
HOWTO="How To: Enter <./seq P1 P2 P3> at the command promt "

#Displays the version information.
VERSION="Week 7 Lab. Number Sequencer"

#Make sure user enters three parameters.
[ "$1" = "--version" ] && echo $HOWTO && echo $VERSION && exit
[ "$1" = "--help" ] || [ $# -ne 3 ] && echo "$HOWTO" && exit 1

#Test to make sure user enters correct numbers in the parameters.
[ $1 -gt $3 ] && [ $2 -gt 0 ] && echo "When the starting value is \
greater than the limit, the increment must be negative. Try `seq' --help' \
for more information" && exit 3
[ $1 -lt $3 ] && [ $2 -le 0 ] && echo "When the starting value is \
less than the limit, the increment must be positive. Try `seq' --help' \
for more information" && exit 4

#If the first parameter is less than or equal to the third parameter
#then add the add the fist parameter and the second parameter and
#show sequence.
{ a="$1"; while [ "$a" -le "$3" ]; do echo "$a"; ((a=$a+$2)); done; }

#If the first parameter is less than or equal to the third parameter
#then add the add the fist parameter and the second parameter and
#show sequence.
{ a="$1"; while [ "$a" -ge "$3" ]; do echo "$a"; ((a=$a+$2)); done; }
 
Old 08-04-2003, 04:29 AM   #2
DIYLinux
Member
 
Registered: Jul 2003
Location: NL
Distribution: My own
Posts: 92

Rep: Reputation: 18
Just look at your script: you only need one while loop to do the counting. That is the natural way to count. So why are there two loops.

Run it on a piece of paper, without computer.

Q: did you expect that done would terminate the script ?
 
Old 08-04-2003, 04:53 AM   #3
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
I'll just give you a hint.

It works fine as is, except, check the punctuation in the two echo statements. Remember that "`" backtics have special meaning in bash.

Also you removed the test to determine if P2 is equal to 0 but you only check for that condidion once, e.g. if P1 > P3 then you dont check P2 for 0 so you could get into a loop.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Iptables (with masq) troubleshooting, very simple script attached script and logs. xinu Linux - Networking 13 11-01-2007 04:19 AM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
creating shell script that executes as root regardless of who runs the script? m3kgt Linux - General 13 06-04-2004 10:23 PM
send automatic input to a script called by another script in bash programming jorgecab Programming 2 04-01-2004 12:20 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration