Frage

I have script where i need to display usage command in case user miss any mandatory information while executing script.

Usage : Script -s <server> -i <instance> -u <user> -p <password> <query> -w <warning value> -c <critical value>

With explanation about all the OPTIONS

I'm getting values from arguments as below variables fashion. But I want this usage with validations in shell script.

SERVER=$1
INSTANCE=$2
USER=$3
DB_PASSWD=$4
QUERY=$5
VAL_WARN=$6
VAL_CRIT=$7

I have tried using getopts, But failed to use since <query> doesn't have a -q parameter before passing the value.

I have tried finding all other ways, But everyone suggested getopts which is not feasible solution for me.

Please help me on this..

War es hilfreich?

Lösung

Use shift to iterate through all of your arguments, something like:

#!/bin/sh

usage ()
{
  echo 'Usage : Script -s <server> -i <instance> -u <user> -p <password>'
  echo '                  <query> -w <warning value> -c <critical value>'
  exit
}

if [ "$#" -ne 13 ]
then
  usage
fi

while [ "$1" != "" ]; do
case $1 in
        -s )           shift
                       SERVER=$1
                       ;;
        -i )           shift
                       INSTANCE=$1
                       ;;
        -u )           shift
                       USER=$1
                       ;;
        -p )           shift
                       PASSWORD=$1
                       ;;
        -w )           shift
                       WARNINGVAL=$1
                       ;;
        -c )           shift
                       CRITICVAL=$1
                       ;;
        * )            QUERY=$1
    esac
    shift
done

# extra validation suggested by @technosaurus
if [ "$SERVER" = "" ]
then
    usage
fi
if [ "$INSTANCE" = "" ]
then
    usage
fi
if [ "$USER" = "" ]
then
    usage
fi
if [ "$PASSWORD" = "" ]
then
    usage
fi
if [ "$QUERY" = "" ]
then
    usage
fi
if [ "$WARNINGVAL" = "" ]
then
    usage
fi
if [ "$CRITICVAL" = "" ]
then
    usage
fi

echo "ALL IS WELL. SERVER=$SERVER,INSTANCE=$INSTANCE,USER=$USER,PASSWORD=$PASSWORD,QUERY=$QUERY,WARNING=$WARNINGVAL,CRITIC=$CRITICVAL"

Should do the trick.

EDIT: added argument validation in the script as suggested by @technosaurus

Andere Tipps

getopts is bitching for a good reason. you should change your script's interface to conform to what people expect.

alternatively, you could use getopts twice, first for the pre-query options, shift, then for the rest.

try this out

usage()
{
   echo "$0 -s <server> -i <instance> -u <user> -p <password> <query> -w <warning value> -c <critical value>"
}

for i in {0..12}
do
    arg=`expr $i +1`
    test ! "${!arg}" && usage && break
done

hope this helps

Well, a very caveman like way would be like this

usage ()
{
  echo 'Usage : Script -s <server> -i <instance> -u <user> -p <password>'
  echo '                  <query> -w <warning value> -c <critical value>'
  exit
}

[ ${13} ] || usage

This is a non-standard approach, but one that I find very useful. Instead of passing values as arguments to specific flags (which is fairly annoying; the user should not be required to specify every value but reasonable defaults should be supplied), you can simply pass them directly via the environment, so that a typical call would look like:

SERVER=blah INSTANCE=foo Script 

It would be nice if you used lower case variable name so the user doesn't have to shout. This allows the script so simply avoid parsing the command line completely, as the values of the variables will be set when the script begins.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top