[Log In ] [New Posts] []
Go Back   GotApex? Forums Forums > General Topics > Software, OS, and the Internet
User Name
Password

Reply
 
Thread Tools Search this Thread Display Modes
Old 04-07-2004, 06:56 PM   #1
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Shell Scripting help (probably basic)

Code:
num=0 sum=0 while [ $num -lt 5 ] do num=`expr $num + 1` sum=`expr $sum + $num` echo "$num" done echo "The sum of the first $num integers is $sum"

This is the script I have to work from

What I have to do is take this script and modify so it "takes and integer on the command and counts up by one until that integer is reached". Later on there is going to be two switches thrown in, but that is soo ahead of me it's not funny.

So this is what I have so far:
Code:
num=0 sum=0 if [ $# -gt 1 ] then while [ $num -lt 5 ] do num=`expr $num + 1` sum=`expr $sum + $num` echo "$num" done echo "The sum of the first $num integers is $sum"
mock all you want, this is my first attempt. I'm not sure what really to do. I think this takes some sort of creative thinking that I don't posses.

Now, I don't want someone to write it out, but could someone help down the path?

My next guess is (after the 'then' statement) something like this:
Code:
num=" `expr $num * 0` " sum=" `expr $sum + 1` "
But I'm not sure that works, and to be honest, this whole thing confuses me. I'd appreciate some guidance.

TIA
__________________

"Nija is the dark soul of gotapex. We don't like to talk about him." - LPMiller
Nija is offline   Reply With Quote
Old 04-07-2004, 07:13 PM   #2
Cubsfan
Rear Admiral Lower Half
 
Cubsfan's Avatar
 
Join Date: Jul 2001
Location: Colorado
Posts: 2,743
I'll see if I can offer any insights.

First, just so you know, it's usually a good idea to have all of your variables all capital. For instance, "NUM" and "SUM"

Also, formatting usually consists of about 2 spaces (or 4, or 8) for each space. For instance:

Code:
num=0 sum=0 if [ $# -gt 1 ] ; then while [ $num -lt 5 ] do num=`expr $num + 1` sum=`expr $sum + $num` echo "$num" done echo "The sum of the first $num integers is $sum"
(BTW, you need to close off your if statement with "fi")

Now, let me make sure I have this strait. You want the user to be able to type:

./myscript 8

Code:
And then print out 1 2 3 4 5 6 7 8 8 Reached!
Or something like that, right?
(Ok, maybe what you mean is that you want to output 1+2+3+4+5+6+7+8?)

I'll try to give you a couple of hints. First, in order to access the command line arguments, you can use the following:
$# --> Number of command line arguments
$0 --> command name
$1... arguments.

So, if your script had:
./myscript A B 123
then
$# == 3
$0 = ./myscript
$1 = A
$2 = B
$3 = 123

None of this actually answers your question, but hopefully it gets you going in the correct direction. Post back if you need any more help
Cubsfan is offline   Reply With Quote
Old 04-07-2004, 09:45 PM   #3
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Quote:
Originally posted by Cubsfan
Now, let me make sure I have this strait. You want the user to be able to type:

./myscript 8

Code:
And then print out 1 2 3 4 5 6 7 8 8 Reached!
Or something like that, right?

Yes, that's exactly what it needs to do
Quote:
(Ok, maybe what you mean is that you want to output 1+2+3+4+5+6+7+8?)

I'll try to give you a couple of hints. First, in order to access the command line arguments, you can use the following:
$# --> Number of command line arguments
$0 --> command name
$1... arguments.

So, if your script had:
./myscript A B 123
then
$# == 3
$0 = ./myscript
$1 = A
$2 = B
$3 = 123

None of this actually answers your question, but hopefully it gets you going in the correct direction. Post back if you need any more help!

ok here's how far I've gotten (yes I took a break for dinner )
Code:
NUM=0 SUM=0 while [ $NUM -eq 1 ] do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` echo "$num" done echo "The sum of the first $num integers is $sum"
I know I'm not finished. I want to make it to make $SUM + 1 until it reaches whatever $NUM was originally right? I was thinking maybe inserting this:
Code:
$SUM -eq $NUM
(modified from my handout of:
int1 -eq int2)

I don't know if it helps, but I can use the If, Case, Until and/or While constructs to do this.

Maybe this:
Code:
NUM=0 SUM=0 if [ $# -gt 0 ] then for num if $@ do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` done echo "The sum of the first $num integers is $sum"

{alright, that's it. I hate scripting.}
Nija is offline   Reply With Quote
Old 04-08-2004, 06:07 AM   #4
Cubsfan
Rear Admiral Lower Half
 
Cubsfan's Avatar
 
Join Date: Jul 2001
Location: Colorado
Posts: 2,743
You're on the right track with this one:
Code:
NUM=0 SUM=0 while [ $NUM -eq 1 ] do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` echo "$num" done echo "The sum of the first $num integers is $sum"

A couple of more hints:

I'm not sure if you know this or not, but you can use a variable on both sides of the equals sign. In other words, if
A=5
B=5
then
A=`expr $A * $B`
would leave A=25. and B=5. This might be able to eliminate the need for one of your variables.

Your while statement has:
while [ $NUM -eq 1 ]
Basically, what's in the brackets is the exit condition. You'll be in this loop as long as this is true. So what you want to think about is "when do I want to leave this loop".

(If I'm beating around the bush too much let me know. Just didn't want to give away too much since you said you didn't want that )
Cubsfan is offline   Reply With Quote
Old 04-08-2004, 12:09 PM   #5
sho.gun
the lemonizer
 
sho.gun's Avatar
 
Join Date: Apr 2001
Location: Calabasas, CA
Posts: 5,373
Send a message via AIM to sho.gun
I've never done shell scripting in my entire life but from what I've read your problem is this:

You have an original input number, let's call it X. You would like to print out numbers starting from 1 until it reaches X. You would also like to print out the sum of those numbers.

So to me it seems like all you need is one for loop:

sum = 0

for i = 1 to $1
echo i
echo -n (<- i think that's how you do new line)
sum = sum + i
next

echo "your sum is" & sum

I'm sure there's mistakes on the code sytanx (I'm assuming you're using vb and I don't know vb), but the logic should be the same. $1 is the original number that the user inputs as an argument.
sho.gun is offline   Reply With Quote
Old 04-08-2004, 12:24 PM   #6
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Quote:
Originally posted by sho.gun
I've never done shell scripting in my entire life but from what I've read your problem is this:

You have an original input number, let's call it X. You would like to print out numbers starting from 1 until it reaches X. You would also like to print out the sum of those numbers.

So to me it seems like all you need is one for loop:

sum = 0

for i = 1 to $1
echo i
echo -n (<- i think that's how you do new line)
sum = sum + i
next

echo "your sum is" & sum

I'm sure there's mistakes on the code sytanx (I'm assuming you're using vb and I don't know vb), but the logic should be the same. $1 is the original number that the user inputs as an argument.

Actually I think it's C (i don't think VB was around when UNIX was written )

ugh.. I can't work on this right now, I have actual work to do , no time for "non-work related internetting" ( )
Nija is offline   Reply With Quote
Old 04-08-2004, 12:51 PM   #7
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Quote:
Originally posted by Cubsfan
You're on the right track with this one:
Code:
NUM=0 SUM=0 while [ $NUM -eq 1 ] do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` echo "$num" done echo "The sum of the first $num integers is $sum"

A couple of more hints:

I'm not sure if you know this or not, but you can use a variable on both sides of the equals sign. In other words, if
A=5
B=5
then
A=`expr $A * $B`
would leave A=25. and B=5. This might be able to eliminate the need for one of your variables.

Your while statement has:
while [ $NUM -eq 1 ]
Basically, what's in the brackets is the exit condition. You'll be in this loop as long as this is true. So what you want to think about is "when do I want to leave this loop".

(If I'm beating around the bush too much let me know. Just didn't want to give away too much since you said you didn't want that )

Damnit, and I thought I was gonna be doing good with switching to the IF statement *snaps fingers* of course, i still don't know what I'm doing *laughs at self*

And to be honest, I'm completely lost (but I still don't want the answer told to me [of course if it was, I probably wouldn't know it ])

I just can't seem to wrap my brain around these concepts. I did a little googling last night, trying to help my cause (search words were shell scripting, beginner + shell scripting, writing shell scripts, etc. etc.) and that did nothing but confuse me even further.

Good thing I have a month until this assignment is due
Nija is offline   Reply With Quote
Old 04-15-2004, 08:51 PM   #8
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Code:
NUM=0 SUM=0 while [ $NUM -eq 1 ] do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` echo "$num" done echo "The sum of the first $num integers is $sum"

So it still looks like that.

Is there a way I can make it add the 1 until it equals $NUM?

on a side note, this is humbling
Nija is offline   Reply With Quote
Old 04-17-2004, 03:11 PM   #9
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Code:
NUM=0 SUM=0 read UNUM while [ $NUM -eq 1 ] do NUM=`expr $1 * 0` SUM=`expr $NUM + 1` echo "$num" done echo "The sum of the first $num integers is $sum"

Now I want to make $UNUM = $SUM right?
Nija is offline   Reply With Quote
Old 04-17-2004, 04:41 PM   #10
Markel
Chief of Naval Operations
 
Markel's Avatar
 
Join Date: Feb 2001
Posts: 11,733
Quote:
Originally Posted by Nija
{alright, that's it. I hate scripting.}
What you really need to do is learn to code in assembler. You haven't lived until you've written major projects using it.
__________________
stay low... keep moving...

Last edited by Markel : 04-17-2004 at 04:43 PM.
Markel is offline   Reply With Quote
Old 04-17-2004, 05:30 PM   #11
Nija
Chief of Naval Operations
 
Nija's Avatar
 
Join Date: May 2001
Location: City of The Dead
Posts: 13,552
Send a message via ICQ to Nija Send a message via AIM to Nija Send a message via Yahoo to Nija
Quote:
Originally Posted by Markel
What you really need to do is learn to code in assembler. You haven't lived until you've written major projects using it.

If i'm having this many problems just doing this, I'd fear any other language.

hell I fear this language!
Nija is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -7. The time now is 05:17 PM.