2

I have a script, which should accept 2 arguments. (-s and -d). If the -d argument is not given, i want to delete my debug file. Same with -s. How do I check if either $1 or $2 is -s or -d?

Shure with 2 arguments i could do that "Brute Force":

if test $1 != "-d" && test $2 != "-d" then
rm $debug
fi

But if I have more than 2 it will get complicated, so what is the proper way to check if any of the arguments is "-d" ?

inetphantom
  • 2,498
  • 4
  • 38
  • 61

3 Answers3

2

Here's an oversimplified arguments parsing for you so you get the idea:

#!/bin/bash

arg_s=0
arg_d=0

show_help() { printf 'Help me\n'; }
show_version() { printf 'version -∞\n'; }
show_usage() { printf 'Usage: %s [-s|-d|-v|-h] [args...]\n' "${0##*/}"; }
invalid_option() { printf >&2 "you can't use option %s. You dumbo\n" "$1"; show_usage; exit 1; }

while (($#)); do
    [[ $1 = -- ]] && { shift; break; }
    [[ $1 = -?* ]] || break
    case $1 in
        (-s) arg_s=1 ;;
        (-d) arg_d=1 ;;
        (-h) show_help; exit 0 ;;
        (-v) show_version; exit 0 ;;
        (-*) invalid_option "$1" ;;
    esac
    shift
done

printf 'You passed the -s flag: %d\n' "$arg_s"
printf 'You passed the -d flag: %d\n' "$arg_d"
printf 'Remaining arguments:\n'
printf '   %s\n' "$@"

Note that it would need some extra work to handle flags like -ds that means -d -s1, a little bit of extra work to have options accepting parameters, and some extra work to handle long options. All this is doable without any major problems.


1 you can have a look at my mock which that has an argument parsing that supports that.

Community
  • 1
  • 1
gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
  • This only allows flags at the start of the command. For example if you pass `test -d blah` it will say `you passed the -d flag: 0` –  Feb 23 '15 at 09:00
  • @JID I don't understand your comment. If you run `./test -d blah` you'll get `You passed the -d flag: 1` and `Remaining arguments: blah`. – gniourf_gniourf Feb 23 '15 at 13:17
  • 1
    Sorry, i meant if you passed them three args like `./Script test -d blah` –  Feb 23 '15 at 13:27
  • 1
    @JID you're right. But it's not very difficult to handle this case either. I don't want to modify the script since OP's requirement seems rather simple (and some tools do exhibit this behavior). And did I mention the word _oversimplified_ in my answer? It does handle the mandatory `--` though `:)`. – gniourf_gniourf Feb 23 '15 at 13:42
  • 1
    @JID this is mentioned by POSIX, e.g., [here](http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_04) – gniourf_gniourf Feb 23 '15 at 13:49
1

Use getopt(1) to parse command-line options in a shell script.

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

You can implement a basic loop as follow:

for arg in "$@"; do
    if [ "$arg" = '-d' ]; then
        rm -f $debug
    fi
done

For more complex parsing, you should consider to use the builtin-command getopts

mcoolive
  • 3,805
  • 1
  • 27
  • 32
  • A `for` loop is not suited to argument parsing at all. If a switch takes an argument, you can't really handle that in a simple way. A `while` loop is by far superior for this task. – gniourf_gniourf Feb 22 '15 at 23:59