1

私はシェルスクリプトにかなり慣れてgetoptsいないので、何らかの理由でスクリプトが URL に到達できない場合にダウンロード URL コマンドを上書きできるフラグ ( ) をスクリプトに追加する必要があります。たとえば、フラグを追加してもスクリプトは終了しません。URL に到達できない場合は続行することを選択できます。

現在、私は

if "$?" -ne "0" then
echo "can't reach the url, n\ aborting"
exit

ここで、コマンドgetoptsを無視することを選択できる場所にフラグを追加する必要があります。"$?' - ne "0"

getopts がどのように機能するのかわかりません。かなり新しいものです。誰かがそれについてどうすればよいか教えてもらえますか?

4

1 に答える 1

1

オプションが 1 つしかない場合は、チェックする方が簡単な場合があります$1

# put download command here
if (( $? != 0 )) && [[ $1 != -c ]]; then
    echo -e "can't reach the url, \n aborting"
    exit
fi
# put stuff to do if continuing here

他のオプションを受け入れる場合は、引数が含まれている可能性があるため、getoptsを使用する必要があります。

#!/bin/bash
usage () { echo "Here is how to use this program"; }

cont=false

# g and m require arguments, c and h do not, the initial colon is for silent error handling
options=':cg:hm:' # additional option characters go here
while getopts $options option
do
    case $option in
        c  ) cont=true;;
        g  ) echo "The argument for -g is $OPTARG"; g_option=$OPTARG;; #placeholder example
        h  ) usage; exit;;
        m  ) echo "The argument for -m is $OPTARG"; m_option=$OPTARG;; #placeholder example
        # more option processing can go here
        \? ) echo "Unknown option: -$OPTARG"
        :  ) echo "Missing option argument for -$OPTARG";;
        *  ) echo "Unimplimented option: -$OPTARG";;
    esac
done

shift $(($OPTIND - 1))

# put download command here
if (( $? != 0 )) && ! $cont; then
    echo -e "can't reach the url, \n aborting"
    exit
fi
# put stuff to do if continuing here
于 2012-06-23T20:44:38.930 に答える