Autosave: 2023-02-07 08:44:16

This commit is contained in:
thomasabishop 2023-02-07 08:44:16 +00:00
parent f475ad9be2
commit 48e218f232
7 changed files with 65 additions and 14 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
.DS_Store
.obsidian/

View file

@ -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 )$ |
| --- | --- | --- | ------------------------------------------------- |

View file

@ -0,0 +1,12 @@
---
categories:
- Programming Languages
tags:
- shell
---
# Read an input file line by line
```bash
# Add snippet here
```

View file

@ -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`) :

View file

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

View file

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

View file

@ -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 > `);