문제

Please could someone tell me what is the hyphen operator in below scripts?

[ "${MYDATA_OPT-}" ] && set "$MYDATA_OPT" "$@"

data=${MYDATA_VER-1}

Is there some quick documentation of these operators?

I am also trying to understand below scripts

shift $(($OPTIND -1))

and

while getopts vhx: c; do
    case "$c" in
            v) let data=data+1 ;;
            h) usage ; exit 0 ;;
            x) . $OPTARG ;;
            \?) usage ; exit 2 ;;
            *) usage ; exit 2 ;;
    esac
done

Thanks a lot in advance.

도움이 되었습니까?

해결책

The form quoted in the bash man page under Paremeter Expansions

3.5.3 Shell Parameter Expansion

${parameter:-word}

If parameter is unset or null, the expansion of word is substituted. Otherwise, the value of parameter is substituted.

producing

${MY_DATA_VER:-1}

would expand to 1 if MY_DATA_VER was either unset, or set to the empty string. Your example, however, omits the colon:

${MY_DATA_VER-1}

This is also documented in the man page, although rather obliquely in a single sentence just before the specific operators are documented (from the 4.1 man page):

Omitting the colon results in a test only for a parameter that is unset.

So in your example, the expansion produces 1 if MY_DATA_VER was never set, or whatever value MY_DATA_VER has (which could be the empty string) if it is set.


The shift example simply removes the first $OPTIND-1 shell arguments. For the significance of OPTIND and a description of how getopts works, consult the bash man page or try help getopts at the bash prompt.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top