Results 1 to 4 of 4

Thread: bash statement

  1. #1
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Thumbs down bash statement

    The below example has some fancy name in the programming community, but i cant remember what it is
    But how do you enter one statement if a number is a even number and another if it isint? Do bash have a function which you can feed the number and it then says if a number is even or not.

    Code:
    i="1"
    
    ##The loop is endless
    while [ ! $i -eq 1 ]
    do
    if $i -eq 1,3,5,7,9,11 and so on then do something elif $i -eq 2,4,6,8,10,12 and so on then do something else
    done
    Thanks on advance.

  2. #2
    Join Date
    May 2007
    Location
    Leeds, UK
    Beans
    1,664
    Distro
    Ubuntu 13.10 Saucy Salamander

    Re: bash statement

    Testing for odd/even is usually done by dividing by 2 and looking at the remainder, i.e. the modulus (%) of 2

    Code:
    $ echo $(( 5 % 2 ))
    1
    $ echo $(( 4 % 2 ))
    0
    When the remainder is zero, it's even (divisible by 2), otherwise it's odd.
    Please create new threads for new questions.
    Please wrap code in code tags using the '#' button or enter it in your post like this: [code]...[/code].

  3. #3
    Join Date
    Dec 2009
    Location
    germany
    Beans
    1,020
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash statement

    hi
    are you looking for something like this (modul 2)
    echo -n "give me a number: "
    read x
    case $((x % 2)) in
    0) echo "$x ist eine gerade zahl";;
    1) echo "$x ist nicht gerade";;
    *) echo "nix is $x"
    esac

    ciao
    "What is the robbing of a bank compared to the FOUNDING of a bank?" Berthold Brecht

  4. #4
    Join Date
    Jan 2009
    Location
    Denmark
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: bash statement

    Thanks for the suggestions guys.
    r-senior that was exactly what i was looking for ,)

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
  •