Autosave: 2023-02-21 07:34:13
This commit is contained in:
parent
b8ad24af99
commit
50f9cef354
6 changed files with 106 additions and 13 deletions
5
.vscode/markdown-styles.css
vendored
5
.vscode/markdown-styles.css
vendored
|
@ -1,3 +1,8 @@
|
|||
code {
|
||||
font-family: "IBM Plex Mono";
|
||||
font-size: 13px !important;
|
||||
}
|
||||
|
||||
body {
|
||||
background: #000e07;
|
||||
}
|
||||
|
|
35
Programming_Languages/Shell/Case_statements_in_Bash.md
Normal file
35
Programming_Languages/Shell/Case_statements_in_Bash.md
Normal file
|
@ -0,0 +1,35 @@
|
|||
---
|
||||
categories:
|
||||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
---
|
||||
|
||||
# Case statements in Bash
|
||||
|
||||
```bash
|
||||
function convertCharToInt {
|
||||
case $1 in
|
||||
A | X )
|
||||
echo 1
|
||||
;;
|
||||
|
||||
B | Y )
|
||||
echo 2
|
||||
;;
|
||||
|
||||
C | Z )
|
||||
echo 3
|
||||
;;
|
||||
*)
|
||||
echo 0
|
||||
;;
|
||||
esac
|
||||
}
|
||||
```
|
||||
|
||||
Usage:
|
||||
|
||||
```bash
|
||||
declare -i intValue = $(convertCharToInt B)
|
||||
```
|
53
Programming_Languages/Shell/Conditionals.md
Normal file
53
Programming_Languages/Shell/Conditionals.md
Normal file
|
@ -0,0 +1,53 @@
|
|||
---
|
||||
categories:
|
||||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
---
|
||||
|
||||
# Conditionals in Bash
|
||||
|
||||
## If statements
|
||||
|
||||
- Conditional blocks start with `if` and end with the inversion `fi` (this is a common syntactic pattern in bash)
|
||||
- The conditional expression must be placed in square brackets with spaces either side. The spaces matter: if you omit them, the code will not run
|
||||
- We designate the code to run when the conditional is met with `then`
|
||||
- We can incorporate else if logic with `elif`
|
||||
|
||||
## Basic example
|
||||
|
||||
```bash
|
||||
if [ -e $var ]; then
|
||||
# Do something
|
||||
else
|
||||
# Do something else
|
||||
fi
|
||||
```
|
||||
|
||||
## If, else
|
||||
|
||||
```bash
|
||||
if [ "$myMove" -eq "$opponentMove" ]; then
|
||||
(( totalScore+=myMove+3 ))
|
||||
elif [ $absDiff -eq 2 ] && [ "$myMove" -gt "$opponentMove" ]; then
|
||||
(( totalScore+=myMove))
|
||||
elif [ $absDiff -eq 2 ] && [ "$opponentMove" -gt "$myMove" ]; then
|
||||
(( totalScore+=myMove+6))
|
||||
elif [ $absDiff -eq 1 ] && [ "$opponentMove" -gt "$myMove" ]; then
|
||||
(( totalScore+=myMove))
|
||||
elif [ $absDiff -eq 1 ] && [ "$myMove" -gt "$opponentMove" ]; then
|
||||
(( totalScore+=myMove+6))
|
||||
fi
|
||||
```
|
||||
|
||||
## Nested conditionals
|
||||
|
||||
```bash
|
||||
if [[ "$line" =~ ^$ ]]; then
|
||||
if [[ "$runningTotal" -gt "$highest" ]]; then
|
||||
(( highest=runningTotal ))
|
||||
fi
|
||||
# Reset running sum
|
||||
(( runningTotal=0 ))
|
||||
fi
|
||||
```
|
|
@ -1,13 +0,0 @@
|
|||
---
|
||||
categories:
|
||||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
---
|
||||
|
||||
## If statements
|
||||
|
||||
- Conditional blocks start with `if` and end with the inversion `fi` (this is a common syntactic pattern in bash)
|
||||
- The conditional expression must be placed in square brackets with spaces either side. The spaces matter: if you omit them, the code will not run
|
||||
- We designate the code to run when the conditional is met with `then`
|
||||
- We can incorporate else if logic with `elif`
|
|
@ -3,8 +3,11 @@ categories:
|
|||
- Programming Languages
|
||||
tags:
|
||||
- shell
|
||||
- data-structures
|
||||
---
|
||||
|
||||
# Lists and arrays in Bash
|
||||
|
||||
## List variables
|
||||
|
||||
When we use the term **list** in bash, we are not actually referring to a specific type of data structure. Instead a **list variable** is really just a normal variable wrapped in quote marks that has strings separated by spaces. Despite the fact that this is not an actual iterative data structure, we are still able to loop through variables of this type.
|
||||
|
@ -81,3 +84,13 @@ for x in ./l*.sh; do
|
|||
done
|
||||
echo
|
||||
```
|
||||
|
||||
## Associational arrays / maps
|
||||
|
||||
With Bash 4 we gained an additional array-like data structure that is key-value based and similar to maps in other languages.
|
||||
|
||||
```bash
|
||||
declare -A rock=(["win"]="scissors" ["lose"]="paper")
|
||||
```
|
||||
|
||||
We would then individuate a value with `"${rock[win]}"`
|
||||
|
|
Loading…
Add table
Reference in a new issue