Find the answer to your Linux question:
Results 1 to 4 of 4
I have decided to teach myself how to use Linux and have started on shell scripting. I have written a couple of minor scripts so far, things like display username, ...
  1. #1
    Just Joined!
    Join Date
    Jan 2009
    Posts
    1

    Shell Scripting Parameters

    I have decided to teach myself how to use Linux and have started on shell scripting. I have written a couple of minor scripts so far, things like display username, directory and date, receive a directory name as a parameter and check if it exists or not. Things of that nature. The script I am currently trying to write is one I am having trouble with.

    What I would like to do is to create a script where the user is prompted to input a number. The user can then input as many numbers as they would like and the numbers would all be added together and the sum display on the screen. If no number is entered then zero will be displayed on the screen.

    Here is the code that I came up with as a starting point


    #!/bin/bash

    echo "Enter any number 1-9 :"
    read number1

    if [ $number1=[1-9] ]
    then
    read number2
    else
    echo "Zero"
    if [ $number2=[1-9] ]
    then
    read number3
    else
    echo "$number1"
    if [ $number3=[1-9] ]
    then
    read number4
    else
    echo "The sum is: @sum = $number1 + $number2
    fi
    fi
    fi

    Now it lets me input the first number, then has a blank space and no matter what I enter next it terminates. After entering my first number why isn't it prompting me for a second number? I am almost positive there is something wrong with the if statements but can't figure out what. Shouldn't

    if [ $number1=[1-9] ]

    check to see if the value for parameter number1 is a number between 1 and 9?

  2. #2
    Just Joined! cheapscotchron's Avatar
    Join Date
    Dec 2008
    Location
    swamps of jersey
    Posts
    68
    It's not prompting you for the second number because your condition is never true. try using sed to check for integer between 1 and 9

    nonums="$(echo $answer |
    sed 's/[1-9]//g')"
    if [ ! -z "$nonums" ] ; then
    echo "Not an integer value."
    exit 0
    fi
    fi

  3. #3
    Just Joined!
    Join Date
    Oct 2004
    Posts
    62
    Hi marvelous_D,
    while i was examining your problem, you received one answer.
    There are a lot of ways to solve your problem... here is another one;
    Code:
     
    # sum.sh
    sum=''
    operation=''
    while echo "Enter any number (CTRL C to exit)"; read n;
    do
        sum=`expr $sum + $n`
        operation=`expr $operation+$n`
        echo `expr ${operation/#+/ }=$sum`
    done
    
    # example:
    
    # Enter any number (CTRL C to exit)
    # 1
    # 1=1
    # Enter any number (CTRL C to exit)
    # 3
    # 1+3=4
    # Enter any number (CTRL C to exit)
    # 5
    # 1+3+5=9
    Bye.

  4. #4
    Linux User
    Join Date
    May 2008
    Location
    NYC, moved from KS & MO
    Posts
    251
    Code:
    #!/bin/bash
    n=' '  # just set n to anything but empty
    nums=""   # this holds the (valid) numbers that have been entered
    sum=0    # initialise variable sum 
    until [ "$n" = "" ]; do    #if only Return Carriage is entered, end the script
        echo "Enter an integer, or nothing to end"
        read n
        if [ -n "$n" ]; then    # if something is entered, validate if it's an integer
            if [[ $n =~ ^-?[0-9]+$ ]]; then
                sum=$((sum+n))
                nums="$nums $n"
                echo "numbers entered:$nums"
            else
                echo "  Error: Expecting an integer"
            fi
        fi
    done
    if [ -n "$nums" ]; then     # if user has entered valid input
        echo -e "the sum of$nums is\n$sum"
    else
        echo "you didn't enter any valid numbers."
    fi
    The above code has been tested on OpenSUSE 10.3 and Gentoo 2007.0.

Posting Permissions

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