|
|
#1 |
|
Chief of Naval Operations
![]() ![]() |
Shell Scripting help (probably basic)
Code:
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:
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:
TIA |
|
|
|
|
|
#2 |
|
Rear Admiral Lower Half
![]() ![]() 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:
Now, let me make sure I have this strait. You want the user to be able to type: ./myscript 8 Code:
(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 |
|
|
|
|
|
#3 | ||
|
Chief of Naval Operations
![]() ![]() |
Quote:
Yes, that's exactly what it needs to do Quote:
ok here's how far I've gotten (yes I took a break for dinner )Code:
Code:
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:
{alright, that's it. I hate scripting.} |
||
|
|
|
|
|
#4 |
|
Rear Admiral Lower Half
![]() ![]() Join Date: Jul 2001
Location: Colorado
Posts: 2,743
|
You're on the right track with this one:
Code:
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 ) |
|
|
|
|
|
#5 |
|
the lemonizer
![]() ![]() ![]() ![]() ![]() |
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. |
|
|
|
|
|
#6 | |
|
Chief of Naval Operations
![]() ![]() |
Quote:
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" ( ) |
|
|
|
|
|
|
#7 | |
|
Chief of Naval Operations
![]() ![]() |
Quote:
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 ![]() |
|
|
|
|
|
|
#8 |
|
Chief of Naval Operations
![]() ![]() |
Code:
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 |
|
|
|
|
|
#9 |
|
Chief of Naval Operations
![]() ![]() |
Code:
Now I want to make $UNUM = $SUM right? |
|
|
|
|
|
#10 | |
|
Chief of Naval Operations
![]() ![]() Join Date: Feb 2001
Posts: 11,733
|
Quote:
![]()
__________________
stay low... keep moving... Last edited by Markel : 04-17-2004 at 04:43 PM. |
|
|
|
|
|
|
#11 | |
|
Chief of Naval Operations
![]() ![]() |
Quote:
If i'm having this many problems just doing this, I'd fear any other language. hell I fear this language! ![]() |
|
|
|
|