how to do this using linux commnds ??
I want to get 10 numbers from user
and to find their sum.....
i need it like this
enter num1 [i must enter number here]
enter num2
enter num3
and after 10 num, it must display the sum
how to do this using linux commnds ??
I want to get 10 numbers from user
and to find their sum.....
i need it like this
enter num1 [i must enter number here]
enter num2
enter num3
and after 10 num, it must display the sum
I don't think that you would be able to do this using shell scripts. I think you would have to run a program to do it for you.
Code:#! /bin/bash declare -i sum for (( i = 1 ; i < 11 ; i++ )) do echo "Please enter number $i" read input sum="$sum + $input" done echo $sum
thanks,..
i heard of some self excuting file called shelling scripting
how do i add this codes to that ?
Just paste it in a text file, give it a name and you're done. The line
indicates it's is a bash shell script. To run it, make the file executable and run it in a terminal.Code:#!/bin/bash
Disclaimer: I am currently suffering from severe CSD (Compulsive Sarcasm Disorder).
My Site | Linux User #452328 | Running Arch Linux on Sony Vaio VGN-SZ61XN/C since October 2008
If graphics are available, you might want to check out the "zenity" package -
included with Ubuntu desktop editions by default;
seeMy version of Your solution...Code:man zenity
file: sum
to make it executable:Code:#!/bin/bash sum="0" for x in 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th do num=`zenity --scale --title "Enter A Number" --min-value 1 --max-value 99 \ --text "Please, select your ${x} number." --value 50` sum="$(( sum + num ))" done zenity --info --title "Grand Total" --text "The Sum is:\n $sum" #End of Fileto run it:Code:chmod +x /path/to/file/sumif the script is in your Working Directory, you will still have to tellCode:/path/to/file/sum
the shell the "path" to find it: a single dot meaning "THIS Directory" -
because more than likely, your current directory
is no in your global "PATH" for executables ...Notes: shells weren't really designed with arithmetic in mind;Code:./sum
So...
notice the quotation in the simple assignment statement sum="0" -
becase as far as the shell is concerned the 0 character is just text.
notice the use of the "$(( ... ))" construct to force the shell to do
C-style arithmetic - and that's not a mistake inside of it - you don't have a to
reference variables with the `$` character inside the "$(( ... ))" construct
Last edited by asmoore82; March 1st, 2009 at 05:55 PM.
Bookmarks