2

I found an answer here for a question on counting number of parameters passed argument to a BASH script. I'm interested on the line : ${1?"Usage: $0 ARGUMENT"} where it throws warning if no parameter is given.

Now I would like to invoke a usage function Usage using : ${1?"Usage: $0 ARGUMENT"} but I do not know how to do it. I tried : ${1?Usage} and BASH throws an error on this line. Can some suggest how to invoke a function using this.

The sample script is as below,

#!/bin/bash

function Usage() {
    echo "Usage: $0 [-q] [-d]"
    echo ""
    echo "where:"
    echo "     -q: Query info"
    echo "     -d: delete info"
    echo ""
}

# Exit if no argument is passed in
: ${1?Usage}

while getopts "qd" opt; do
    case $opt in
        q)
            echo "Query info"
            ;;
        d)
            echo "Delete info"
            ;;
        *)
            Usage;
            exit 1
            ;;
    esac
done
Community
  • 1
  • 1
Panch
  • 1,097
  • 3
  • 12
  • 43
  • 1
    @hek2mgl, updated the question with pseudo code. – Panch Feb 28 '17 at 07:19
  • Why can't you handle this in `getopts` itself?, to which flag is this parameter meant for? – Inian Feb 28 '17 at 07:23
  • 1
    @Inian Having that either `q` or `d` is required, the check for $1 makes sense. AFAIK `getopts` has no facility to say `q` or `d` is required. However, we might set a variable inside `getopts` and check that afterwards. Added that to my answer. – hek2mgl Feb 28 '17 at 07:48
  • Generally, you shouldn't really take any *action* in the `while` loop; just record which options are present. *After* the while loop, you could print the usage message and exit if you have determined that no option were used. (Which, after reading more closely, is exactly what hek2mgl does in the latter half of his answer...) – chepner Feb 28 '17 at 12:29

2 Answers2

4

What about this?

function Usage() {
    echo "Usage: $0 [-q] [-d]"
    echo ""
    echo "where:"
    echo "     -q: Query info"
    echo "     -d: delete info"
    echo ""
}

# Exit if no argument is passed in
: ${1?"$(Usage)"}

Anyhow, I think this is more readable:

if [ $# -lt 1 ] ; then
    Usage
fi

Another idea would be to handle it like this:

mode=""
while getopts "qdh" opt; do
    case $opt in
        q)
            mode="Query info"
            ;;
        d)
            mode="Delete info"
            ;;
        h)
            Usage
            exit 0
            ;;
        *)
            Usage >&2
            exit 1
            ;;
    esac
done

if [ -z "${mode}" ] ; then
    Usage >&2
    exit 1
fi
hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • `getopts` will not print the usage info. – hek2mgl Feb 28 '17 at 07:27
  • @hek2mgl Thanks for the input. I could use the `: ${1?"$(Usage)"}` for printing usage info but your second suggestion is more readable. I wanted to try this `: ${1?"$(Usage)"}` and see how that works since the syntax was new to me. – Panch Feb 28 '17 at 07:33
  • @Inian Personally I would choose a different programming language in the moment when it comes to more complex command line arguments. But yes, you can also print the Usage info based on the results of `getopts`, you would even need to if you want to handle the arguments to options correctly instead of just checking the number of arguments. Again, I recommend [Python](https://docs.python.org/3/howto/argparse.html). :) – hek2mgl Feb 28 '17 at 07:36
  • @hek2mgl: Appreciate it, `++` for demonstrating usage in the form, `${1?"$(Usage)"}` (not used before) – Inian Feb 28 '17 at 07:38
  • 1
    @Inian I would not recommend it, was just suggesting the right syntax. – hek2mgl Feb 28 '17 at 07:42
0

you just need pass argument to the bash script. assume that the bash script's name is hello.sh; for example:

    #!/bin/bash
    # script's name is hello.sh
    : ${1?"Usage: $0 Argument"}
    echo hello,$1

then chmod a+x hello.sh

then we call the script use this : bash hello.sh yourname then echo the "hello,yourname" if you just called use bash hello.sh and no argument for this script you will get your error message like this Usage: hello.sh Argument

enter image description here

zzy
  • 139
  • 6