1

Possible Duplicate:
Using getopts in bash shell script to get long and short command line options

This is the code i've written

#! /bin/bash

getopts master $1

while getopts ":master" opt; do
  case $opt in
    master)
      echo "-master was triggered! $1 was entered" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done

and this is the output i'm getting-

]$ ./test123.sh -master 123
./test123.sh: line 3: getopts: `-master': not a valid identifier

How do i define a user defined option?

4

1 に答える 1

3

いくつか問題があると思います。

  1. この ( ) の動作がわかりませんgetopts master $1。削除してください
  2. 単一の char 引数が必要だと思います (つまり、-m と -master)

たとえば、以下はうまくいくようです:

#!/bin/bash
while getopts ":m" opt; do
  case $opt in
    m)
      echo "-m was triggered! $1 was entered" >&2
      ;;
    \?)
      echo "Invalid option: -$OPTARG" >&2
      ;;
  esac
done
于 2012-12-06T11:55:52.967 に答える