Posts

Showing posts with the label If Statement

Bash If Statement With Multiple Conditions Throws An Error

Answer : Use -a (for and) and -o (for or) operations. tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html Update Actually you could still use && and || with the -eq operation. So your script would be like this: my_error_flag=1 my_error_flag_o=1 if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ] || ([ $my_error_flag -eq 1 ] && [ $my_error_flag_o -eq 2 ]); then echo "$my_error_flag" else echo "no flag" fi Although in your case you can discard the last two expressions and just stick with one or operation like this: my_error_flag=1 my_error_flag_o=1 if [ $my_error_flag -eq 1 ] || [ $my_error_flag_o -eq 2 ]; then echo "$my_error_flag" else echo "no flag" fi You can use either [[ or (( keyword. When you use [[ keyword, you have to use string operators such as -eq , -lt . I think, (( is most preferred for arithmetic, because you can directly use operators such as == , < and ...

Batch - If, ElseIf, Else

Answer : @echo off title Test echo Select a language. (de/en) set /p language= IF /i "%language%"=="de" goto languageDE IF /i "%language%"=="en" goto languageEN echo Not found. goto commonexit :languageDE echo German goto commonexit :languageEN echo English goto commonexit :commonexit pause The point is that batch simply continues through instructions, line by line until it reaches a goto , exit or end-of-file. It has no concept of sections to control flow. Hence, entering de would jump to :languagede then simply continue executing instructions until the file ends, showing de then en then not found . @echo off set "language=de" IF "%language%" == "de" ( goto languageDE ) ELSE ( IF "%language%" == "en" ( goto languageEN ) ELSE ( echo Not found. ) ) :languageEN :languageDE echo %language% This works , but not sure how your language variable is de...