Skip to content

Instantly share code, notes, and snippets.

@viceo
Forked from jehiah/simple_args_parsing.sh
Last active June 6, 2020 01:12
Show Gist options
  • Select an option

  • Save viceo/85bdd18f4fc6bcbe161c73f3d4adf858 to your computer and use it in GitHub Desktop.

Select an option

Save viceo/85bdd18f4fc6bcbe161c73f3d4adf858 to your computer and use it in GitHub Desktop.

Revisions

  1. viceo revised this gist Jun 6, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion simple_args_parsing.sh
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@
    ENVIRONMENT="dev"
    DB_PATH="/data/db"

    function usage()
    usage()
    {
    echo "if this was a real script you would see something useful here"
    echo ""
  2. @jehiah jehiah created this gist Mar 4, 2011.
    49 changes: 49 additions & 0 deletions simple_args_parsing.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    #!/bin/sh

    #
    # a simple way to parse shell script arguments
    #
    # please edit and use to your hearts content
    #


    ENVIRONMENT="dev"
    DB_PATH="/data/db"

    function usage()
    {
    echo "if this was a real script you would see something useful here"
    echo ""
    echo "./simple_args_parsing.sh"
    echo "\t-h --help"
    echo "\t--environment=$ENVIRONMENT"
    echo "\t--db-path=$DB_PATH"
    echo ""
    }

    while [ "$1" != "" ]; do
    PARAM=`echo $1 | awk -F= '{print $1}'`
    VALUE=`echo $1 | awk -F= '{print $2}'`
    case $PARAM in
    -h | --help)
    usage
    exit
    ;;
    --environment)
    ENVIRONMENT=$VALUE
    ;;
    --db-path)
    DB_PATH=$VALUE
    ;;
    *)
    echo "ERROR: unknown parameter \"$PARAM\""
    usage
    exit 1
    ;;
    esac
    shift
    done


    echo "ENVIRONMENT is $ENVIRONMENT";
    echo "DB_PATH is $DB_PATH";