From 48e218f23207dda49ee78991643b02e59dfd9414 Mon Sep 17 00:00:00 2001 From: thomasabishop Date: Tue, 7 Feb 2023 08:44:16 +0000 Subject: [PATCH] Autosave: 2023-02-07 08:44:16 --- .gitignore | 1 + .../Propositional_logic/Boolean_functions.md | 2 +- .../Read_input_file_line_by_line.md | 12 ++++++ .../Shell/Lists_and_arrays.md | 12 ++++++ Programming_Languages/Shell/Read.md | 37 +++++++++++++++++-- _meta/Topic_Log.md | 8 ++-- node-scratch.js | 7 ---- 7 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 Programming_Languages/Shell/Bash_snippets/Read_input_file_line_by_line.md delete mode 100644 node-scratch.js diff --git a/.gitignore b/.gitignore index e43b0f9..234a059 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .DS_Store +.obsidian/ diff --git a/Logic/Propositional_logic/Boolean_functions.md b/Logic/Propositional_logic/Boolean_functions.md index b7ee6f1..34c370b 100644 --- a/Logic/Propositional_logic/Boolean_functions.md +++ b/Logic/Propositional_logic/Boolean_functions.md @@ -19,7 +19,7 @@ Here is a work through where $f(1, 0, 1)$: - The second disjunction: $x \land y$ is false because $x$ is 1 and $y$ is 1 - The overall function returns false because the main connective is disjunction and both of its disjuncts are false -We can compute all possible outputs of the function by constructing a [truth table](/Logic/Propositional_logic/Truth-tables.md) with each possible variable as the truth conditions and the output of the function as the truth value: +We can compute all possible outputs of the function by constructing a [trkjuth table](/Logic/Propositional_logic/Truth-tables.md) with each possible variable as the truth conditions and the output of the function as the truth value: | $x$ | $y$ | $z$ | $f(x,y,z) = (x \land y) \lor (\lnot(x) \land z )$ | | --- | --- | --- | ------------------------------------------------- | diff --git a/Programming_Languages/Shell/Bash_snippets/Read_input_file_line_by_line.md b/Programming_Languages/Shell/Bash_snippets/Read_input_file_line_by_line.md new file mode 100644 index 0000000..16e585f --- /dev/null +++ b/Programming_Languages/Shell/Bash_snippets/Read_input_file_line_by_line.md @@ -0,0 +1,12 @@ +--- +categories: + - Programming Languages +tags: + - shell +--- + +# Read an input file line by line + +```bash +# Add snippet here +``` diff --git a/Programming_Languages/Shell/Lists_and_arrays.md b/Programming_Languages/Shell/Lists_and_arrays.md index e3d2675..63379f0 100644 --- a/Programming_Languages/Shell/Lists_and_arrays.md +++ b/Programming_Languages/Shell/Lists_and_arrays.md @@ -59,6 +59,18 @@ done Note that `@` here is a special symbol standing for all the members of the `words` array. +### Sorting arrays + +#### Sorting an integer array highests to lowest + +```bash +sorted_array=($(echo "${array[@]}" | tr " " "\n" | sort -nr)) +``` + +Where `array` is the name of the original array. The sorted array will be stored in the `sorted_array` array. + +The `sort` command sorts the input in reverse numerical order (`-n` for numerical sort and `-r` for reverse sort). The `tr` command is used to convert the spaces in the array to newline characters so that each element is sorted on a separate line. + ## Looping through file system The following script loops through all files in a directory that begin with `l` and which are of the bash file type (`.sh`) : diff --git a/Programming_Languages/Shell/Read.md b/Programming_Languages/Shell/Read.md index 7a5f8d4..fe23a6c 100644 --- a/Programming_Languages/Shell/Read.md +++ b/Programming_Languages/Shell/Read.md @@ -7,9 +7,11 @@ tags: # `read` -`read` is primarily used to capture `stdin` from the user and automatically parse it as variables. It has a secondary use case as a command that the `stdout` is piped to. This enables you to capture the output of a command as one or more variables which you can then execute subsequent operations on. +The primary use of `read` is to capture user input from `stdin`. It is also often used frequently to parse strings or files that are redirected to it (with `<` and `<<`) or piped to it. In each case, what is read is stored as a variable. -## `stdin` +`read` will parse line by line using a space (`\n`) as the default delimiter. You can use IFS to parse by other characters and/or [split the contents into an array](/Programming_Languages/Shell/Split_into_array.md). + +## Example of capturing user input ```bash $ read var1 var2 @@ -20,8 +22,37 @@ $ bishop > If you don't specify variables, `read` will automatically parse using whitespace -## `stdout` +## Example of piping to `read` + +This reads the files in a directory and passes the file names to `read`. ```bash find -type -f -not -path "./.git/" | read $fname ``` + +## Example of parsing a file + +We will typically read from a source and then do something with each variable that `read` returns, e.g: + +```bash +while read var; do + if [var == 'something']; then + # do something +done < './input-file.txt +``` + +## `$REPLY` + +If you do not assign a variable name to store the value that `read` reads a default (`$REPLY`) is applied. You can reference this value in your code. + +For example the following loop does something if `$REPLY` is equal to an empty string: + +```bash +while read; +do + ((count++)) + if [[ -z "$REPLY" ]]; then + echo "$count" + fi +done < "$input +``` diff --git a/_meta/Topic_Log.md b/_meta/Topic_Log.md index 17de264..9cd1f61 100644 --- a/_meta/Topic_Log.md +++ b/_meta/Topic_Log.md @@ -5,13 +5,15 @@ - Best way to run a command in a script - is it to `echo` it? - How to handle the return value of a command - If it returns multiple values, how to isolate and loop through them -- What the weird variable symbols mean like errors and stuff -- Read up properly about `find` and `read` +- ~~What the weird variable symbols mean like errors and stuff~~ +- ~~Read up properly about `find` and `read`~~ - `.list` file extension - Error handling -- Splitting strings +- ~~Splitting strings~~ - Awk - https://dane-bulat.medium.com/the-awk-programming-language-an-introduction-7035d343cd30 +- Why do we have to do `"$var"` instead of `$var` or `${var}` at times +- The `test` program (does it actually use the word 'test' or is this implicit?) and its use of `-z` and and `-e` flags ## Linux diff --git a/node-scratch.js b/node-scratch.js deleted file mode 100644 index f58382f..0000000 --- a/node-scratch.js +++ /dev/null @@ -1,7 +0,0 @@ -process.stdin.on("data", (data) => { - process.stdout.write(data.toString().trim()); - process.exit(); -}); - -process.stdout.write("\n What is your name? \n"); -process.stdout.write(` \n > `);