Results 1 to 9 of 9

Thread: First Bash Script

  1. #1
    Join Date
    Oct 2007
    Location
    USA - Indiana
    Beans
    557
    Distro
    Ubuntu 10.10 Maverick Meerkat

    First Bash Script

    I am writing my first bash script. It is mostly made for me and people who don't know how to set up Ubuntu once they freshly install it. I got it to do everything I wanted so far. Now, I want to give the user the option to install the Compiz settings manager and Avant Window Navigator. How do I write the script to where it will only install if the user says they want eye candy?

  2. #2
    Join Date
    Mar 2008
    Beans
    4,714
    Distro
    Ubuntu 9.10 Karmic Koala

    Re: First Bash Script

    Code:
    #!/bin/bash
    read -p "Eye candy? ([Y]/n) ";
    if [ "$REPLY" = "n" ]; then
        echo "You pressed 'n'"
    else
        echo "You pressed anything but 'n'"
    fi

  3. #3
    Join Date
    Oct 2007
    Location
    USA - Indiana
    Beans
    557
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: First Bash Script

    hmmm... so would this work?

    Code:
    #!/bin/bash
    read -p "Would you like to install Compiz Effects? (Y/n) ";
    if [ "$REPLY" = "Y" ]; then
        sudo aptitude install compizconf-settings-manager
    if [ "$REPLY" = "n" ]; then
        echo "You chose to skip installation"
    fi

  4. #4
    Join Date
    Oct 2007
    Location
    Serbia, Nis
    Beans
    233
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: First Bash Script

    Code:
    #!/bin/bash
    read -p "Would you like to install Compiz Effects? (Y/n) ";
    if [ "$REPLY" = "Y" ]; then
        sudo aptitude install compizconf-settings-manager
    if [ "$REPLY" = "n" ]; then
        echo "You chose to skip installation"
    fi
    You need one 'fi' just before second if

    Code:
    #!/bin/bash
    read -p "Would you like to install Compiz Effects? (Y/n) ";
    if [ "$REPLY" = "Y" ]; then
        sudo aptitude install compizconf-settings-manager
    fi
    if [ "$REPLY" = "n" ]; then
        echo "You chose to skip installation"
    fi
    .___.___.__...__..___..__....__..__..
    |.__|.__|._\./__\|._.\/..\../..|/..\.
    |._||._||.v.|.\/.|.v././\.|.`7.|.//.|
    |_|.|___|__/.\__/|_|_\_||_|..|_|\__/.

  5. #5
    Join Date
    Oct 2007
    Location
    USA - Indiana
    Beans
    557
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: First Bash Script

    cool... what happens if they type "x" or something? will it ask them the question again?... that's what I want it to do.

  6. #6
    Join Date
    Sep 2006
    Beans
    2,914

    Re: First Bash Script

    then you will need a loop. Personally i use case/esac instead of if/else
    Code:
    while true
    do
    
     read ......
     case $REPLY in
       N|n ) #do something ;;
       Y|y ) # do something ;;
       Q|q ) exit;;
       * ) echo "huh??" ;;
     esac
    
    done

  7. #7

    Re: First Bash Script

    Your prompt gives the idea that 'Y' is the default, and it isn't.
    This would be safer and better:

    #!/bin/bash

    DONE=false
    while [ "$DONE" != true ]
    do
    read -p "Would you like to install Compiz Effects? (Y/n): ";
    case "$REPLY" in
    ""|Y|y)
    sudo aptitude install compizconf-settings-manager
    DONE=true
    ;;

    n|N)
    echo "You chose to skip installation"
    DONE=true
    ;;

    *)
    echo "Invalid input" >&2
    ;;
    esac
    done

  8. #8
    Join Date
    Oct 2007
    Location
    USA - Indiana
    Beans
    557
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: First Bash Script

    Code:
    #!/bin/bash
    
    DONE=false
    while [ "$DONE" != true ]
    do
    read -p "Would you like to install Compiz Effects? (Y/n): ";
    case "$REPLY" in
    ""|Y|y)
    sudo aptitude install compizconf-settings-manager
    DONE=true
    ;;
    
    n|N)
    echo "You chose to skip installation"
    DONE=true
    ;;
    
    *)
    echo "Invalid input" >&2
    ;;
    esac
    done

    Okay... I'm really new at this... so tell me if this is what the code is saying.

    It asks "Would you like to install Compiz Effects? (Y/n)". The reply can be upper case or lower case? If Y or y... it installs. If n or N it says "You chose to skip installation". If the input is not Y,y,n,or N it says "Invalid input" and asks them again?

    I need some direction in finding out how to read this stuff!!!! HAHA!

  9. #9
    Join Date
    Oct 2007
    Location
    Serbia, Nis
    Beans
    233
    Distro
    Ubuntu 8.04 Hardy Heron

    Re: First Bash Script

    I need some direction in finding out how to read this stuff!!!! HAHA!
    Google is your best friend for this


    Cheers
    .___.___.__...__..___..__....__..__..
    |.__|.__|._\./__\|._.\/..\../..|/..\.
    |._||._||.v.|.\/.|.v././\.|.`7.|.//.|
    |_|.|___|__/.\__/|_|_\_||_|..|_|\__/.

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
  •