From 50f9cef354197101fdc754ee8de2b26f4178428d Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Tue, 21 Feb 2023 07:34:13 +0000 Subject: [PATCH] Autosave: 2023-02-21 07:34:13 --- .vscode/markdown-styles.css | 5 ++ .../Shell/Case_statements_in_Bash.md | 35 ++++++++++++ Programming_Languages/Shell/Conditionals.md | 53 +++++++++++++++++++ Programming_Languages/Shell/Control_flow.md | 13 ----- .../Shell/Lists_and_arrays.md | 13 +++++ ...arks_in_Bash.md => Quote_marks_in_Bash.md} | 0 6 files changed, 106 insertions(+), 13 deletions(-) create mode 100644 Programming_Languages/Shell/Case_statements_in_Bash.md create mode 100644 Programming_Languages/Shell/Conditionals.md delete mode 100644 Programming_Languages/Shell/Control_flow.md rename Programming_Languages/Shell/{_Quote_marks_in_Bash.md => Quote_marks_in_Bash.md} (100%) diff --git a/.vscode/markdown-styles.css b/.vscode/markdown-styles.css index a0dc585..90ad763 100644 --- a/.vscode/markdown-styles.css +++ b/.vscode/markdown-styles.css @@ -1,3 +1,8 @@ +code { + font-family: "IBM Plex Mono"; + font-size: 13px !important; +} + body { background: #000e07; } diff --git a/Programming_Languages/Shell/Case_statements_in_Bash.md b/Programming_Languages/Shell/Case_statements_in_Bash.md new file mode 100644 index 0000000..a1f7e45 --- /dev/null +++ b/Programming_Languages/Shell/Case_statements_in_Bash.md @@ -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) +``` diff --git a/Programming_Languages/Shell/Conditionals.md b/Programming_Languages/Shell/Conditionals.md new file mode 100644 index 0000000..7c15ab2 --- /dev/null +++ b/Programming_Languages/Shell/Conditionals.md @@ -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 +``` diff --git a/Programming_Languages/Shell/Control_flow.md b/Programming_Languages/Shell/Control_flow.md deleted file mode 100644 index 7198835..0000000 --- a/Programming_Languages/Shell/Control_flow.md +++ /dev/null @@ -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` diff --git a/Programming_Languages/Shell/Lists_and_arrays.md b/Programming_Languages/Shell/Lists_and_arrays.md index 63379f0..0b922e0 100644 --- a/Programming_Languages/Shell/Lists_and_arrays.md +++ b/Programming_Languages/Shell/Lists_and_arrays.md @@ -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]}"` diff --git a/Programming_Languages/Shell/_Quote_marks_in_Bash.md b/Programming_Languages/Shell/Quote_marks_in_Bash.md similarity index 100% rename from Programming_Languages/Shell/_Quote_marks_in_Bash.md rename to Programming_Languages/Shell/Quote_marks_in_Bash.md