ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
now i made a few changes
{ a="$1"; while [ "$a" -le "$2" ]; do echo "$a"; ((a=$a+$2)); done; }
it works fine when i enter three positive numbers but dosent work with negative numbers. Any clues for a newbie student. ??
Smells like homework to me. Why would you want to do this in a bash script when the seq command itself is something that's commonly used in bash scripts?
well like you said its homework. We need to duplicate the functionality of the seq command with a script . I need to use a determinate loop and pasrse the command line input variables for accuracy. Should the user enter the wrong number, or type, of variables it needs to display an error message and needs to support .
--help: displays help and exits
--version: display the help screen and the version number
I am so lost im just know understanding the basic stuff
I think the problem here is the questions you're asking. If it's homework, the last thing you should ask is "how do I do this?". What good does it do you to have someone else do it for you?
Perhaps you might consider asking questions to help you learn how to do it yourself.
First is understanding how to use command line options. They are stored in variables $1, $2, $3, $4, etc.
Code:
#!/bin/bash
echo "1 is $1"
echo "2 is $2"
Code:
/home/thelinuxduck/shell/klfreese> ./clargs.sh 2e r3
1 is 2e
2 is r3
If you're really confused on where to start, then do this:
Write out the steps that you think you have to go through in order to accomplish this task. Don't worry about writing code, just write it out. Then, if you aren't sure of the logic, post what you have, and ask for comments on the logic. We all will be more than happy to help you learn how to fix the logic.
Then, write your first rev of the code. Be patient and try to get the various aspects of it to work. If you're really stuck, then post your code, and we'll help you learn how to find the problems and fix them.
It worked as is except the last line has a typo:
{ a="$1"; while [ "$a" -ge "$3" ]; do echo "$a"; ((a=$a-$2)); done; }
Should be like this:
{ a="$1"; while [ "$a" -ge "$3" ]; do echo "$a"; ((a=$a+$2)); done; }
You should still add the numbers because the increment should be negative and, if you subtract a negative you are adding!
You might also want to put in a couple more tests on the P1 P2 P3 parameters like the real seq:
[ $1 -gt $3 ] && [ $2 -gt 0 ] && echo "When the starting value is greater than the limit, the increment must be negative." && exit 3
[ $1 -lt $3 ] && [ $2 -lt 0 ] && echo "When the starting value is less than the limit, the increment must be positive." && exit 4
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.