Case commands in shell in Linux (regular break, unconditional continuation, and conditional continuation)
In linux, the case name in shell is similar to the switch in C/C ++, but the case in shell is more powerful and complex.
1. The power mainly lies in the fact that the keywords in case in shell can be strings, and each item can contain regular expressions.
2. complexity is mainly reflected in the following three options after each item of case in shell: break (conventional break) and unconditional follow up (unconditional continuation) and conditional follow up (conditional continuation ).
This article focuses on the above 2nd points.
The unconditional continuation of case and conditional continuation are only available in bash 4.1.x and later versions.
Run the following command to view your current bash version.
echo $BASH_VERSION
The conventional break is added after each item ;;
Unconditional continuation is added after each item ;&
Conditional addition of each item ;;&
Test code
#!/bin/bashecho "Test No.1 ..."case "1" in 1) echo '1' ;;& 2) echo '2' ;; 3) echo '3' ;; ?) echo '?' ;; *) echo '*' ;;esacecho "Test No.2 ..."case "1" in 1) echo '1' ;; 2) echo '2' ;;& 3) echo '3' ;; ?) echo '?' ;; *) echo '*' ;;esacecho "Test No.3 ..."case "1" in 1) echo '1' ;& 2) echo '2' ;; 3) echo '3' ;; ?) echo '?' ;; *) echo '*' ;;esacecho "Test No.4 ..."case "1" in 1) echo '1' ;; 2) echo '2' ;& 3) echo '3' ;; ?) echo '?' ;; *) echo '*' ;;esac
Test results:
Test No.1 ...1?Test No.2 ...1Test No.3 ...12Test No.4 ...1