bashスクリプトで私のelifがelse文と見なされるのはなぜですか?

bashスクリプトで私のelifがelse文と見なされるのはなぜですか?

私はwgetを使用してREST APIを介してサーバーから情報を取得するbashスクリプトを作成しています。 getoptsを使用してスクリプトに提供されているオプションを解析し、ifステートメントを使用して指定されたオプションに従ってスクリプトを正しくリダイレ​​クトします。スクリプト本文(wget呼び出しなど)に移動すると、elifはヘルプメニューを印刷し、そうでない場合はエラーメッセージを出力します。しかし、私のelifはelseステートメントとして機能するようです。私が実行したとき:

>./jira -h

ヘルプメニューから正しい回答を受け取りました。

----------jira options----------
Required:
-d [data/issueID]
-u [username] -> [username] is your JIRA username
-p [password] -> [password] is your JIRA password

Optional:
-q -> quiet, i.e. no output to console
-h -> help menu

ただし、エラーメッセージを表示する必要があるタスクを実行すると、ヘルプメニューが表示されます。

>./jira -u jsimmons

----------jira options----------
Required:
-d [data/issueID]
-u [username] -> [username] is your JIRA username
-p [password] -> [password] is your JIRA password

Optional:
-q -> quiet, i.e. no output to console
-h -> help menu 

私のスクリプトは次のとおりです。

#!/bin/bash

#using getopts to parse options
while getopts ":hqd:u:p:" opt; do
        case $opt in
                h)
                        help="true"
                        ;;      
                q)
                        quiet="true"
                        ;;      
                d)
                        data=$OPTARG
                        ;;
                u)
                        username=$OPTARG
                        ;;
                p)
                        password=$OPTARG
                        ;;
                \?)
                        echo "Invalid option: -$OPTARG" >&2
                        ;;
                :)
                        echo "Option -$OPTARG requires an argument." >&2
                        ;;
        esac
done

#check if required options have been set
if [[ -n $data && -n $username && -n $password ]]; then 

        wget -q \
            --http-user=$username \
            --http-passwd=$password \
            --header="Content-Type: application/json" [URI]


        #placing issue info into variable

        response=$(< $data) 


        #using heredoc to run python script
        #python script uses regular expressions to find the value of the field 
        #customfield_10701 ie the branch version 

        output=$(python - <<EOF
import re

matchObj = re.search(
     r'(?<=customfield_10701":").*(?=","customfield_10702)',
     '$response',
     re.I
)
if(matchObj):
        print(matchObj.group())

EOF
)


        #writes branch version in .txt file

        echo $output>branchversion.txt 


        #prints the branch version if the quiet option hasn't been set 
        if [ -z $quiet ]; then
                echo "-------------------------------------------" 
                echo ""
                echo "The branch version for issue $data is:"
                cat branchversion.txt
                echo ""
        fi

        #removes file that wget creates containing all data members for the issue

        rm $data
elif [ -n $help ]; then 
        #if help option has been set    
        echo "" 
        echo "----------jira options----------"
        echo "Required:"
        echo "-d [data/issueID]"
        echo "-u [username] -> [username] is your JIRA username"
        echo "-p [password] -> [password] is your JIRA password"
        echo ""
        echo "Optional:"
        echo "-q -> quiet, i.e. no output to console"
        echo "-h -> help menu"
        echo ""
        #http GET data members for issue
else
        #if not all required options or help have been set      
        echo "Error: Missing argument(s)"
        echo "Usage: ./jira [option(s)] -d [data] -u [username] -p [password]"
        echo "" 
        echo "Try: ./jira -h for more options"
fi

ベストアンサー1

この-nオプションは、文字列の長さがゼロでないことを確認します。

if [ ... ]; then #posix compliant condition tests

if [[ ... ]]; then #extended condition tests

拡張された条件付きテストはposixとは異なる動作をするようです。

> if [ -n $unsetVar ];then echo yes ; fi
yes
>

> if [ -n "$unsetVar" ];then echo yes ; fi
>

> if [[ -n $unsetVar ]];then echo yes ; fi
>

両方に拡張条件を使用するか、[[ ... ]]変数を引用符で囲みます。あなたのelif声明はこれまで常に正確です。

おすすめ記事