Results 1 to 6 of 6

Thread: linux cmds to get 10 numbers from user and add them

  1. #1
    Join Date
    Dec 2008
    Location
    Mars
    Beans
    265
    Distro
    Ubuntu 10.04 Lucid Lynx

    Question linux cmds to get 10 numbers from user and add them

    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

  2. #2
    Join Date
    Jun 2008
    Location
    Manchester, UK
    Beans
    425
    Distro
    Ubuntu Development Release

    Re: linux cmds to get 10 numbers from user and add them

    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.

  3. #3
    Join Date
    Jan 2009
    Beans
    43

    Re: linux cmds to get 10 numbers from user and add them

    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

  4. #4
    Join Date
    Dec 2008
    Location
    Mars
    Beans
    265
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: linux cmds to get 10 numbers from user and add them

    thanks,..

    i heard of some self excuting file called shelling scripting

    how do i add this codes to that ?

  5. #5
    Join Date
    Apr 2007
    Location
    Belgium
    Beans
    1,528

    Re: linux cmds to get 10 numbers from user and add them

    Just paste it in a text file, give it a name and you're done. The line
    Code:
    #!/bin/bash
    indicates it's is a bash shell script. To run it, make the file executable and run it in a terminal.
    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

  6. #6
    Join Date
    Jul 2007
    Location
    Burlington, NC
    Beans
    1,995
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: linux cmds to get 10 numbers from user and add them

    If graphics are available, you might want to check out the "zenity" package -
    included with Ubuntu desktop editions by default;
    see
    Code:
    man zenity
    My version of Your solution...
    file: sum
    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 File
    to make it executable:
    Code:
    chmod +x /path/to/file/sum
    to run it:
    Code:
    /path/to/file/sum
    if the script is in your Working Directory, you will still have to tell
    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 ...
    Code:
    ./sum
    Notes: shells weren't really designed with arithmetic in mind;
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •