複数のOR条件を持つIFループで構文エラーが発生します。

複数のOR条件を持つIFループで構文エラーが発生します。

文字列値の比較中に複数または条件を追加したいと思います。単一の文字列値を使用すると、次のコードが機能します。

 prname ="$projname"
    if prname == 'CheckForUpdate':
        settingsdata['autocompare'] = True
        settingsdata['autorecords'] = True

ただし、複数のOR条件を追加すると、次の構文エラーが発生します。

prname ="$projname"
    if [prname == 'CheckForUpdate'] || [prname == 'Result'];
    then
            settingsdata['autocompare'] = True
            settingsdata['autorecords'] = True

エラー発生:

 if [ prname == 'CheckForUpdate' ] || [ prname == 'Result' ]
                                   ^
SyntaxError: invalid syntax

なぜこれが起こるのかご存知ですか?#!/bin/sh

ベストアンサー1

これが実際にシェルであれば、エラーが多すぎてどこから始めるべきかわかりません。

if prname == 'CheckForUpdate':

コマンドはシェルでprname2つの引数で呼び出されるため、名前付きコマンドが見つから==ないCheckForUpdate: というエラーが発生する可能性がありますprname

if [ prname == 'CheckForUpdate' ] || [ prname == 'Result' ]

より良いですが、prname文字列を2つの文字列と比較するCheckForUpdateのでResult、常にfalseです(CheckForUpdateもResultもoneで始まらないp)。

settingsdata['autocompare'] = True

=ほとんどのシェル実装では、周囲のスペースは構文エラーになります。いいえ仕事。

おすすめ記事