Hello there, I am posting to seek help with a KSH script,
I am making a simple calculation program where the user can enter as many numbers as they like, I am getting their input using the read command, however I am not sure how to repeat doing this and storing the input in to new variables until the user wants the program to calculate.
Here is what I have so far
I am thinking maybe using an Array is the best way to go about this, but I am really stumped on how to do this
I guess I'm really missing something here. Why do you believe that an array is needed here? Why not just keep a running total as you gather numbers? This is written assuming ksh, but should work for any POSIX-conforming shell (not csh and not tcsh):
First he collects data, then he can use (and reuse) them to calculate as many things as he needs (even easily changing his code).
--
Bye
Hi Lem,
You suggested an array because pandapowerbox said in the 1st message in this thread:
My question and suggestion were directed to pandapowerbox, not to you. Your script was an excellent example of how to use an array to solve a problem like this; if an array is needed. I'm sorry for the confusion.
Pandapowerbox,
I see a lot of newbie programmers who pay too much attention to the semantics of a possible solution without stepping back to look at the bigger picture. If you start out thinking that you need to add a list of variables and print the sum, then you think about accumulating an array and adding them at the end. But, if your goal is just to get the sum of a list of values, you make the problem much more complex by assuming you need to save the input values as separate variables or as separate members of an array instead of just keeping a running sum as you read each individual value.
I also note that the last line of your starting script:
almost certainly doesn't do what you wanted it to do.
By passing the quoted string to echo, it expands $x1 and $x2, but doesn't invoke bc. And, if you had left off the double quotes, you also have mismatched parentheses. And, if you had used: echo ($x1 + $x2 | bc) or echo (($x1 + $x2 | bc)) you would have had ksh syntax errors. You could have used echo $($x1 + $x2 | bc) and gotten the result you were probably expecting, but if you're learning ksh, you should look at arithmetic expansion that does all of the work in the shell without needing to invoke another utility (bc in this case). Using arithmetic expansion this would simply be: echo $((x1 + x2)).
You have probably already noticed that the suggestions that Lem and I provided both use ksh's arithmetic expansions sum=$(( $sum + ${array[$i]} )) and sum=$((sum + x)), respectively, rather than invoking awk, bc, dc, expr, perl, or some other utility to perform these simple calculations.
Hopefully, the comments Lem and I have made will help you see that there are almost always several ways to do something in shell scripts and help you think about a few of the possibilities as you tackle new problems like this.
Thanks for the detailed replies, they have really helped me a great deal!!
@Don: I decided to maybe use an Array as I wasn't aware of any other way of doing what I wanted to do using KSH ... I have only been using it for a week but I guess I have been proven wrong that I can do what I need without an Array.
Ksh93, bash, ... give also little nicer syntax version for the arithmetic expansion .
You can put everything inside (( )) and use variables without dollar sign $.
This is not Posix compatible, but look easier to read. Works only in the arithmetic expansion using (( )).
In this example input values has saved to the array and to the string (inputstr).
Hello
I have the following output and want the output to look:
FROM:
GigabitEthernet0/0 is up, line protocol is up
1 input errors, 0 CRC, 0 frame, 1 overrun, 0 ignored
275 output errors, 0 collisions, 3 interface resets
GigabitEthernet0/1 is up, line protocol is up
0... (4 Replies)
Hello all
I'm looking to write a simple script (ksh/sh/bsh) to read user input and write it to a file (adding each time) until the user enters QUIT at which point I'm hoping to ask some more questions.
Any help much apprecited (2 Replies)
Hi
I'm looking to write a simple ksh loop reading user input (and write it to a file) until the user enters QUIT at which point I want it to continue.
Does anyone have an example of this type of loop?
Any help much appreciated
Cheers (2 Replies)
Hi folks..
i got a requirement to red multiple directories from STDIN and store them to a variable.
ex:-
echo "Enter directory to add:"
echo " Enter directory to add:"
read value till there is input and when there is no input close the read loop and store variable into an array
... (1 Reply)
Hi,
I have an issue where i run an command in a shell script.
command >/dev/null
ret=$?
echo ret
If the command returns an error i'm redirecting it to /dev/null.
The prob is if an error comes it expects the user to press return to continue.
And hence the return is not echoed. and the end... (4 Replies)
Hello,
I am working with KSH on AIX and I have 2 files generated from different sources... as seen below:
FILE1 FILE2
AAA AAA@ABS0001C
BBB BBB@ABS0003D
CCC CCC@ABS0023A
DDD DDD@ABC0145D
EEE EEE@ABS0090A
FFF FFF@ABS0002A
GGG GGG@ABC0150D
HHH
FILE1 is main main data source,... (4 Replies)
Hi,
In Perl, how can we define a variable make it read the value from the standard input? Meaning, how can have the user type in the value that will be assigned to the variable?
Thanks, (2 Replies)
Hi,
In a shell script i am running a command which is asking for input.
How can i give that automatically.
I have done this before but for the time being can't recall.
Was something like <|
Thanks (6 Replies)
Given a line of text in ksh:
string1 string2 string3 .....stringn
is there a way of automatically assigning each string to an array element? Or just different variables would do.
Thanks,
Jon (1 Reply)
Is there a command that sets a variable length?
I have a input of a variable length field but my output for that field needs to be set to 32 char.
Is there such a command?
I am on a sun box running ksh
Thanks (2 Replies)