eolas/zk/Case_statements_in_Bash.md

41 lines
507 B
Markdown
Raw Permalink Normal View History

2023-02-21 07:34:13 +00:00
---
tags:
- shell
---
# Case statements in Bash
2023-03-16 06:58:39 +00:00
Usage against a variable
```sh
animal="dog"
case $animal in
bird ) echo "Avian";;
dog|puppy ) echo "Canine";;
* ) echo "No match";;
esac
```
Usage as part of a function:
2023-02-21 07:34:13 +00:00
```bash
function convertCharToInt {
case $1 in
2023-03-16 06:58:39 +00:00
A|X )
echo 1 ;;
B|Y )
echo 2 ;;
C|Z )
echo 3 ;;
2023-02-21 07:34:13 +00:00
*)
2023-03-16 06:58:39 +00:00
echo ;;
2023-02-21 07:34:13 +00:00
esac
}
```
Usage:
```bash
declare -i intValue = $(convertCharToInt B)
```