I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input.
I admit I am really bad at understanding his teaching, for one. He teaches this class as you had previous experience with programming and when i ask him questions he is very evasive with his answers. My problem is that he didn't have a lecture about flowcharts and i don't understand how to make it work for unlimited numbers introduced.
I was able to make a small script that can add 5 numbers from arg and 5 from input but that is all. he told me to use whiles and fors but i cant seem to grasp the logic so now I am asking you for my help.
I will post here his requirements for the script and also the code for the script that i wrote hoping that somebody can help me.
I appreciate all the help and i will check later with you.
Thank you. 1. The problem statement, all variables and given/known data
Write a bash program to:
a) Input zero or more integers from standard input (one per line)
b) Input zero or more integers from command line arguments
c) Output the sum of all input numbers on standard output
It is not necessary to check for invalid input or to format the output.
2. Relevant commands, code, scripts, algorithms:
Free to use any of if statements, elif statements, while loops, for loops to achieve desired result.
3. The attempts at a solution (include all code and scripts):
Unfortunately script accepts only 5 numbers and the professor wants unlimited input.
if [ $# -lt 1 ]
then
echo Please enter you numbers \:
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The total is $sum
elif [ $# -le 5 ]
then
read num1
read num2
read num3
read num4
read num5
sum=$(( $num1 + $num2 + $num3 + $num4 + $num5 ))
echo The grand total is $(( $sum + $1 + $2 + $3 + $4 + $5 ))
fi
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Diablo Valley College, PleasatHill, Ca, Stuart Fogg, Comsc117 I cant add URL if I don't have 5 posts, sorry.
Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
by the way, you should also take a look at the += operator so that you can pass the read value as the incremental value of the sum that would be updated "on the fly"
You also could consider the
the colon means /bin/true (this is for making endless loop).
to go out of the while loop, you could use break (exit would take you out of the script)
You can also have an eye to the trap command so you could handle a scenario like :
the script always ask number (infinite loop)
when the user want to display the sum, he press <Cntrl>+<C> key.
That will raise a INT signal that can be trapped (see trap command) to get out of the wile loop and display the sum.
I could write it but that wouldn't help you to learn, so now i think you have enough clue to go on
your prof wants a loop I think, for the command line part.
There is a problem with arguments you need to know
you can reference $1 .. $9 the way I show. More than that and it becomes ${10} ${11} and so on. I would suggest using the shift command to move each argument into the $1 variable.
you need a loop for the arguments as well
This User Gave Thanks to jim mcnamara For This Post:
You know I don't want to create any problems to anybody so i do not want the solution what i want is to figure it out how to create unlimited input. I know it may be limited to the operating system limit so I thank everybody for helping.
Thank you guys for all the help,
with all the information that you provided plus my discussion with the professor
I was able to finish the script close to what the professor required.
Hi,
I need a help with arithmetic calculations in my script. I have two variables: a=17; b=1712
I want to perform ($a/$b)*100 with two decimals in the result.
I tried with following:
res=$((100*a/b))
res=`echo "scale=2; $a / $b" | bc`
But I am not getting the decimal values.... (4 Replies)
Hello everybody,
I decided to take a Unix Introduction class and have never had experience with programming. Everything was fine until recently when the Prof. started shell scripting and he wants us to make a small script to add unlimited numbers from arguments and from standard input.
I... (1 Reply)
I am begining to learn bourne shell and as a practice I have written a script which when given the purchase price and percentage of discount calculates the savings.
I somehow cannot figure out why my script fails to do arthimatic calculation on real numbers.
Could anyone look at the script... (5 Replies)
Hello,
was just wondering how to compare strings in unix? I mean as in C there is a function strcmp() in string.h, is there any function in unix for that? I tried using
if
and all such variations but didn't succeed. Any help would be appreciated. Thanks in advance :) (9 Replies)
I'm new to this and I need help with writing a script. The following assignment is as follows...
Create a shell script named Project3-8 located in a directory supported by the File System Hierarchy Standard.
The script needs to perform the following pseudocode in the order shown:
-Display... (1 Reply)
I have to complete my assignment and i need help...
These are two simple shell script programs :
1.) Write a shell script to display files in the current directory in the following format :
FileName Size Date Protection Owner
_______ ____ ___ ________ _____
2.) Write a... (1 Reply)
advance happy new year to all,
i am having a script.The purpose of the scripts is as follows.If the current month is march,june,september or december ,inc_flg should be set to '1' otherwise inc_flg should be set to '2'
month= date +"%m"
if || || ||
; then
inc_flg = 1
else
... (6 Replies)