--Summary of how to write case statements in ShellScript bash
--The following is a case statement that performs a specific process when a variable contains a specific value or character string. --It is assumed that any value is stored in the variable.
```bash
#!/bin/bash
case ${Variable A} in
"String B")
What you want to do when the character string B is stored in the variable A;; "String C") What you want to do when the character string C is stored in the variable A;; "String D") What you want to do when the character string D is stored in the variable A;; *) Processing to be performed when variables other than character string B, character string C, and character string D are stored in variable A;; esac ```
--Describe the process to output different contents depending on the character string stored in the variable number
.
--It is assumed that the variable number
contains a character string of arbitrary two-digit numbers.
--When 00 is stored in the variable number
, the message" Executes the process when 00 is stored in the variable number "is output.
--When 20 is stored in the variable number
," Process when 20 is stored in the variable number is executed. "Is output.
--When 30 is stored in the variable number
, the message" Executes the process when 30 is stored in the variable number "is output.
--When none of 10, 20, and 30 are stored in the variable number
," 00, 20, 30 are not stored in the variable number. "Is output.
--The case statements that meet the above conditions are described below.
```bash
#!/bin/bash
case ${number} in
"00")
echo "Executes the process when 00 is stored in the variable number.";;
"20")
echo "Executes the process when 00 is stored in the variable number.";;
"30")
echo "Executes the process when 00 is stored in the variable number.";;
*)
echo "The variable number does not contain any of 00, 20, or 30."
esac
```
Recommended Posts