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 07-29-2003, 06:37 PM   #1
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Rep: Reputation: 15
script help


can anyone tell me how to replicate the seq command using a bash shell script ?
 
Old 07-29-2003, 07:42 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
What exactly is the seq command?

Cheers,
Tink
 
Old 07-29-2003, 08:16 PM   #3
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
seq() { a="$1"; while [ "$a" -le "$2" ]; do echo "$a"; ((a++)); done; }
 
Old 07-29-2003, 08:46 PM   #4
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Original Poster
Rep: Reputation: 15
i dont understand im sorry im newbie. so how would you input the numbers to be seq.??
 
Old 07-30-2003, 02:53 AM   #5
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
that wouyld be the contents of a script file, so you can save it as seq2 or something, and use it in the same way.
 
Old 07-30-2003, 07:08 PM   #6
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Original Poster
Rep: Reputation: 15
thanks

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. ??
 
Old 07-31-2003, 10:09 PM   #7
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Original Poster
Rep: Reputation: 15
pls. help me
 
Old 07-31-2003, 10:18 PM   #8
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
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?
 
Old 07-31-2003, 10:50 PM   #9
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Original Poster
Rep: Reputation: 15
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

hope someone can help
 
Old 08-01-2003, 04:48 PM   #10
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
klfreese:

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
Second is knowing how to test the command line args to see if they hold legitimate values, or enough were given. Here's the bash how-to on condionals: http://www.tldp.org/LDP/abs/html/comparison-ops.html

Third, is knowing how to do loops. Here is the bash how-to on loops: http://www.tldp.org/LDP/abs/html/loops.html

Fourth, is knowing how to do variable arithmetic. here is the bash how-to with some examples: http://www.tldp.org/LDP/abs/html/arithexp.html

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.

That should be enough to get you going.
 
Old 08-02-2003, 02:42 AM   #11
klfreese
Member
 
Registered: Jul 2003
Distribution: Suse 10
Posts: 55

Original Poster
Rep: Reputation: 15
script help

This is what i have so far it work but wont work with the 1st or second param being a neg number any sugestions ?



#!/bin/bash
#
#
HOWTO="How To: ./seq P1 P2 P3 "
#
VERSION="Number Sequencer"
#
[ "$1" = "--version" ] && echo $HOWTO && echo $VERSION && exit
#
[ "$1" = "--help" ] || [ $# -ne 3 ] && echo "$HOWTO" && exit 1
#
[ $2 -eq 0 ] && echo "Parameter 2 can not be 0 - ABORT" && exit 2
#
{ a="$1"; while [ "$a" -le "$3" ]; do echo "$a"; ((a=$a+$2)); done; }
#
{ a="$1"; while [ "$a" -ge "$3" ]; do echo "$a"; ((a=$a-$2)); done; }
 
Old 08-02-2003, 05:17 AM   #12
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
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

<edit> oops

Last edited by /bin/bash; 08-02-2003 at 05:22 AM.
 
Old 08-02-2003, 05:48 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
please DO NOT ask homework question son this site, you are set homework for a reason. cheating is bad mmmmmmmmmkay?

i'd also thank members for not replying to questions about homework in future (even though it is obviously in good nature)
 
Old 08-02-2003, 07:48 AM   #14
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
Yeah! From now on if someone asks you if this is homework the correct reply is "No this is just something I'm tinkering with."
 
  


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 04:46 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