Tag: menu

  • Bash menu in bash with getopts

    A little more sophisticated script than the former bash menu example to get some arguments from command line.

    #!/bin/bash

    while getopts "u:p:" opt; do

    case $opt in
    u)
    echo "-u was triggered, Parameter: $OPTARG"
    dbuser="$OPTARG"
    ;;
    p)
    echo "-p was triggered, Parameter: $OPTARG"
    dbpass="$OPTARG"
    ;;
    \?)
    echo "Invalid option: -$OPTARG"
    exit 1
    ;;
    :)
    echo "Option -$OPTARG requires an argument."
    exit 1
    ;;
    esac

    done

    # Clear all options and reset the command line
    shift $(( OPTIND -1 ))

    # First parameter
    if [ -z "$1" ]; then
    echo "usage: $0 [-u name] [-p password] file"
    exit
    fi

  • Shell script menu

    This is an example of how to create a bash menu using while and read.

    #!/bin/sh
    echo "Use one of the following options"
    echo " d : run date command"
    echo " : "
    echo " q: quit this menu"
    echo "enter your choice or type "q" to quit : \c"
    read option
    while [ "$option" != "q" ]
    do
    case $option in
    d ) date
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    )
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;

    # ...

    * )
    echo "invalid choice!"
    shift
    echo "enter your choice or type "q" to quit : \c"
    read option
    ;;
    q ) exit 0
    ;;
    esac
    done
    exit 0