[LINUX] How to write a ShellScript bash case statement

Purpose

--Summary of how to write case statements in ShellScript bash

Example of writing

--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 ```

More specific example of writing

--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

How to write a ShellScript bash case statement
How to write a ShellScript Bash for statement
How to write a Python class
Qiita (1) How to write a code name
How to write a GUI using the maya command
How to write a list / dictionary type of Python3
[Python] How to write a docstring that conforms to PEP8
XPath Basics (2) -How to write XPath
How to call a function
How to hack a terminal
[Python] How to write an if statement in one sentence.
How to define multiple variables in a python for statement
How to write a test for processing that uses BigQuery
I'll never forget how to write a shell script, don't forget! !!
How to write a metaclass that supports both python2 and python3
How to make a Japanese-English translation
How to put a symbolic link
How to write soberly in pandas
Flask reuse How to write html
How to make a slack bot
Write standard output to a file
How to make a recursive function
How to create a virtual bridge
How to create a Dockerfile (basic)
[Blender] How to make a Blender plugin
How to delete a Docker container
How to write Docker base image
How to write Django1.9 environment-independent wsgi.py
How to make a crawler --Basic
Notes on how to write requirements.txt
How to create a config file
[Introduction to Python] How to output a character string in a Print statement
[Introduction to Python] How to use the in operator in a for statement?
Difference in how to write if statement between ruby ​​and python
How to write a docstring to create a named tuple document with sphinx
How to determine if a shell script was started in bash
How to create a clone from Github
How to split and save a DataFrame
How to build a sphinx translation environment
Think about how to write a filter with the Shotgun API-Contact Versions
How to set optuna (how to write search space)
How to add a package with PyCharm
[Python] How to make a class iterable
How to draw a graph using Matplotlib
[Python] How to convert a 2D list to a 1D list
How can I write a good program?
How to write Python document comments (Docstrings)
[Colab] How to copy a huge dataset
[Python] How to invert a character string
How to install a package using a repository
[Ubuntu] How to execute a shell script
How to get a stacktrace in python
A memorandum of how to write pandas that I tend to forget personally
How to write a string when there are multiple lines in python
[Introduction to Python] How to write a character string with the format function
How to create a repository from media
How to make a Backtrader custom indicator
How to choose a Seaborn color palette
How to test on a Django-authenticated page
How to make a Pelican site map
[Bash] Command to write a file line by line to a serial number file [One liner (?)]