Results 1 to 3 of 3

Thread: Need help understanding responses in bash

  1. #1
    Join Date
    Mar 2010
    Location
    skelmersdale, england
    Beans
    209
    Distro
    Ubuntu 10.04 Lucid Lynx

    Need help understanding responses in bash

    Hello everyone,

    I coded my own script to install ssmtp and configure it, but i am not really satisfied.

    I want my script to ask me whether i want to install certain packages. So before it reads "sudo apt-get install ssmtp" - I want it to ask me. Can anyone help me?

    Here is my code.


    #!/bin/bash

    if [ "$(/usr/bin/id -u)" != "0" ]; then
    exec /usr/bin/sudo "$0"
    fi

    echo "Welcome to sTaTiC's gmail setup..."

    echo "Installing SSMTP..."

    sudo apt-get install ssmtp

    [[ $? -eq 0 ]] || { echo "Failed to install SSMTP, exiting..."; exit 1; }

    echo "SSMTP installation complete!"

    echo "Installing heirloom-mailx..."

    sudo apt-get install heirloom-mailx

    [[ $? -eq 0 ]] || { echo "Failed to install heirloom-mailx, exiting..."; exit 1; }

    echo "heirloom-mailx installation complete!"

    echo "configuring ssmtp.conf..."

    [[ -e /etc/ssmtp/ssmtp.conf ]] && cp /etc/ssmtp/ssmtp{.conf,.bak}

    [[ $? -eq 0 ]] || { echo "Failed to create backup /etc/ssmtp/ssmtp.conf, exiting..."; exit 1; }

    cat <<EOF > /etc/ssmtp/ssmtp.conf
    AuthUser=email@googlemail.com
    AuthPass=password
    FromLineOverride=YES
    mailhub=smtp.gmail.com:587
    UseSTARTTLS=YES
    EOF

    [[ $? -eq 0 ]] || { echo "Failed to create backup /etc/ssmtp/ssmtp.conf, exiting..."; exit 1; }

    echo "Installed and configured ssmtp and heirloom-mailx"

    exit

  2. #2
    hakermania's Avatar
    hakermania is offline Τώρα ξέρεις τι γράφω εδώ!
    Join Date
    Aug 2009
    Location
    Greece
    Beans
    1,704
    Distro
    Ubuntu Development Release

    Re: Need help understanding responses in bash

    Code:
    echo "Do you want to install this?"
    read YN
    if [ X"$YN" = X"y" -o X"$YN" = X"Y" ]; then
    apt-get install package
    else
    echo "Skipping package......"
    fi
    Website

    Wallch (Wallpaper Changer): Sourceforge | Launchpad

  3. #3
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!

    Re: Need help understanding responses in bash

    In bash you can do something like:
    Code:
    read -n1 -p "Install package foo? [Y/n] " REPLY
    echo
    REPLY=${REPLY:=Y}     
    if [[ $REPLY = [Yy] ]]; then
      apt-get install foo || { echo "error..."; exit 1; } >&2
    fi

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
  •