Shell condition judgment
1. if condition judgment: if [condition]; then do something fi multiple conditions: & represents AND | represents OR multiple judgments: if [condition]; then do something elif [condition 2]; then // do something else // do someshing fi
#!/bin/bashread -p "Please input (Y/N) : " ynif [ "$yn" == "Y" ] || [ "$yn" == "y" ]; then echo "Yes!"elif [ "$yn" == "N" ] || [ "$yn" == "n" ]; then echo "No!"else echo "Input Error"fiexit 0
View running results
[work@www sh]$ sh hello.sh Please input (Y/N) : yYes![work@www sh]$ sh hello.sh Please input (Y/N) : lInput Error[work@www sh]$
2. case judgment if there are many judgment levels in if, it is recommended to use case, otherwise there are too many nesting, and the code is prone to deterioration. Case $ variable name in "variable 1") do something; each end uses two semicolons (variable 2) do something ;;*) the last * indicates other values do something; esac:
#!/bin/bashcase $1 in "one") echo "One" ;; "two") echo "Two" ;; *) echo "Usage $0 (one|two)" ;;esacexit 0
Running result
[work@ww sh]$ sh hello.sh Usage hello.sh (one|two)[work@www sh]$ sh hello.sh oneOne[work@www sh]$
Address: http://blog.csdn.net/yonggang7/article/details/40480695