
事象と確認事項
Windowsのバッチで下記の、ソースコード1の条件分岐を指定したところ、
後半4,5つ目の条件を通らなく詰まっております。
ソースコード2のようにすれば通すことができますが原因がわからず、もしわかるようでしたらお教えいただけませんでしょうか?
コードの記載方法も改善箇所のご指摘いただけますと助かります。
該当のソースコード1
dos
1@echo off 2setlocal 3 4set B1=FALSE 5set B2=TRUE 6set B3=TRUE 7 8if "%B1%" == "TRUE" ( 9 set words="%B1% %B2% %B3% Result1" 10) else if "%B1%" == "FALSE" if "%B2%" == "FALSE" if "%B3%" == "FALSE" ( 11 set words="%B1% %B2% %B3% Result2" 12) else if "%B1%" == "FALSE" if "%B2%" == "FALSE" if "%B3%" == "TRUE" ( 13 set words="%B1% %B2% %B3% Result3" 14) else if "%B1%" == "FALSE" if "%B2%" == "TRUE" if "%B3%" == "FALSE" ( 15 set words="%B1% %B2% %B3% Result4" 16) else if "%B1%" == "FALSE" if "%B2%" == "TRUE" if "%B3%" == "TRUE" ( 17 set words="%B1% %B2% %B3% Result5" 18) 19 20echo %words% 21 22endlocal 23exit /b 0 24
※バッチファイル名はiv.bat
実行結果
環境変数B1~B3を変更して各条件について網羅するようテストしました。
4つ目の条件から結果が取得できません。
C:\pg\test>iv "TRUE TRUE TRUE Result1" C:\pg\test>iv "FALSE FALSE FALSE Result2" C:\pg\test>iv "FALSE FALSE TRUE Result3" C:\pg\test>iv ECHO は <OFF> です。 C:\pg\test>iv ECHO は <OFF> です。
該当のソースコード2(解消版)
試しに下記のようにelse if を切り離すと正しい結果を取得できます。
DOS:iv2.bat
1@echo off 2setlocal 3 4set B1=FALSE 5set B2=TRUE 6set B3=TRUE 7 8if "%B1%" == "TRUE" ( 9 set words="%B1% %B2% %B3% Result1" 10) else if "%B1%" == "FALSE" if "%B2%" == "FALSE" if "%B3%" == "FALSE" ( 11 set words="%B1% %B2% %B3% Result2" 12) else if "%B1%" == "FALSE" if "%B2%" == "FALSE" if "%B3%" == "TRUE" ( 13 set words="%B1% %B2% %B3% Result3" 14) 15rem 切り離すとOK 16if "%B1%" == "FALSE" if "%B2%" == "TRUE" if "%B3%" == "FALSE" ( 17 set words="%B1% %B2% %B3% Result4" 18) else if "%B1%" == "FALSE" if "%B2%" == "TRUE" if "%B3%" == "TRUE" ( 19 set words="%B1% %B2% %B3% Result5" 20) 21 22echo %words% 23
実行結果(解消版)
C:\pg\test>iv "FALSE TRUE TRUE Result5"
補足情報(FW/ツールのバージョンなど)
OS 名: Microsoft Windows 10 Home
OS バージョン: 10.0.17763 N/A ビルド 17763
追記(回答を踏まえた修正版)
Y.H.さんに頂いた最もスッキリした形ではありませんが、
今回の反省を踏まえた修正版を追記します。
- ifを連結した時にand風の挙動をする場合もあるがかならずしもそうならない
- else if のあとで評価される条件式は一つ(ifを連結したandもどきは利用できない)
- ifの省略形を使わない(可読性以前に正しい処理が記述されない)
Dos
1@echo off 2setlocal 3 4set B1=FALSE 5set B2=TRUE 6set B3=TRUE 7 8if "%B1%" == "TRUE" ( 9 set words="%B1% %B2% %B3% Result1" 10) else if "%B1%" == "FALSE" ( 11 if "%B2%" == "FALSE" ( 12 if "%B3%" == "FALSE" ( 13 set words="%B1% %B2% %B3% Result2" 14 ) else if "%B3%" == "TRUE" ( 15 set words="%B1% %B2% %B3% Result3" 16 ) 17 ) else if "%B2%" == "TRUE" ( 18 if "%B3%" == "FALSE" ( 19 set words="%B1% %B2% %B3% Result4" 20 ) else if "%B3%" == "TRUE" ( 21 set words="%B1% %B2% %B3% Result5" 22 ) 23 ) 24) 25 26echo %words% 27 28endlocal 29exit /b 0 30
追記2
可読性を上げたので追記します。
Dos
1@echo off 2setlocal 3 4set B1=FALSE 5set B2=FALSE 6set B3=TRUE 7set words=%B1% %B2% %B3% 8 9if "%B1%" == "TRUE" ( 10 set words="%words% Result1" 11) else if "%words%" == "FALSE FALSE FALSE" ( 12 set words="%words% Result2" 13) else if "%words%" == "FALSE FALSE TRUE" ( 14 set words="%words% Result3" 15) else if "%words%" == "FALSE TRUE FALSE" ( 16 set words="%words% Result4" 17) else if "%words%" == "FALSE TRUE TRUE" ( 18 set words="%words% Result5" 19) 20 21echo %words% 22 23endlocal 24exit /b 0 25


回答2件
あなたの回答
tips
プレビュー