Autosave: 2023-03-16 06:58:39

This commit is contained in:
thomasabishop 2023-03-16 06:58:39 +00:00
parent c9bdffc204
commit ea6507a47e
13 changed files with 239 additions and 37 deletions

View file

@ -4,7 +4,7 @@ categories:
tags: [python]
---
# Functions
# Functions in Python
- Convention is to leave a double line-break after a function definition (but not with nested functions - here, a single linebreak is sufficient)
- Scope within functions is demarcated by indents, as everything in Python

View file

@ -91,7 +91,7 @@ print(a_list)
We distinguish `del` from `remove` when removing elements from lists:
- `del` requires an index value
- `remove` requires a value reference (i.e. the mame of the element rather than its index)
- `remove` requires a value reference (i.e. the name of the element rather than its index)
`del` is simple deletion whereas `remove` searches the list. Therefore `del` is more efficient.

View file

@ -26,6 +26,22 @@ x = list(map(addOne, data))
# necessary to add `list` to get some output
```
### Map returns an object
When you run `map` against a list it returns a map object not a list. This can present problems if you are not aware of it since it is not an entity that you can loop through.
The following map removes whitespaces from elements in a list:
```py
lines = map(lambda x: x.strip(), lines)
```
In order to be able to run list-like operations against the resulting `lines` object we would need to convert it to a list:
```py
lines = list(map(lambda x: x.strip(), lines))
```
## Filter
```py

View file

@ -7,23 +7,30 @@ tags:
# Case statements in Bash
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:
```bash
function convertCharToInt {
case $1 in
A|X )
echo 1
;;
echo 1 ;;
B|Y )
echo 2
;;
echo 2 ;;
C|Z )
echo 3
;;
echo 3 ;;
*)
echo 0
;;
echo ;;
esac
}
```

View file

@ -0,0 +1,57 @@
---
categories:
- Programming Languages
tags:
- shell
---
# Formatting output text in Bash
We can use the `-e` flag with `echo` to have greater control over the output of text. `-e` allows us to use linebreaks, tabs and other formattings within a string.
## Output text in columns
```bash
echo -e "Name\t\tNumber"; echo -e "Thomas\t\t123";
Name Number
Thomas 123
```
## Break text over several lines
```bash
echo -e "This text\nbreaks over\nthree lines"
This text
breaks over
three lines
```
## Colour outputs
![](/_img/terminal_colours.png)
```bash
echo -e "\033[31;40mColoured Text\033[0m"
```
The coloured section is prepended by `\033[` and ended with `[0m`. The foreground colour is given first, and then the background colour.
We can also change the text style. We do this by adding an extra value after the first square bracket:
```bash
echo -e "\033[4;31;40mColoured Text\033[0m"
```
This underlines the output.
We can create a script that simplifies the construction of colour-formatted text, e.g
```bash
ulinered="\033[4;31;40m"
red="\033[31;40m"
none="\033[0m"
echo -e $ulinered"ERROR:"$none$red" Something went wrong."$none
```

View file

@ -7,7 +7,7 @@ tags:
# Functions in Bash
- We don't name function parameters in the function declaration. Instead we have an implied index of arguments: `$1, $2, $3,...`. When the function is called, the first value after the function name becomes `$1` by default.
We don't name function parameters in the function declaration. Instead we have an implied index of arguments: `$1, $2, $3,...`. When the function is called, the first value after the function name becomes `$1` by default, then the subsequent arguments.
```bash
function expandRange() {
@ -24,3 +24,38 @@ expandedRange=$(expandRange 1 4)
echo $expandedRange
# 1 2 3 4
```
## Get all arguments as an array
We can access all the arguments passed to a function using the `$@` syntax we encountered before when [passing arguments to scripts](/Programming_Languages/Shell/Passing_arguments_to_scripts.md). (Here a function is a kind of script in miniature so the process is the same.)
```sh
function numberThings() {
i=1
for f in "$@"; do
echo $i: "$f"
(( i++ ))
done
}
```
## Local variables
```sh
var1="I'm variable 1"
function myfunction() {
var2="I'm variable 2"
local var3="I'm variable 3"
}
myfunction
echo $var1
echo $var2
echo $var3
# I'm variable 1
# I'm variable 2
```
> The convention is to put functions at the top of the script, after the shebang and after the global variables

View file

@ -5,6 +5,8 @@ tags:
- shell
---
# Grep
## Purpose of `grep`
`grep` stands for “global regular expression print”. It allows you to search plain text data sets for strings which match a regular expression or pattern.

View file

@ -5,18 +5,9 @@ tags:
- shell
---
# Loops in bash
# Loops in Bash
## Loop through an array
```bash
for element in "${arr[@]}"
do
echo "$element"
done
```
## Traditional for loop with upper and lower increment range
## Traditional for loop
```bash
for (( i=0; i<=5;i++ )); do
@ -25,3 +16,55 @@ done
# 1 2 3 4 5
```
## `for..in`: loop through an array
```bash
for element in "${arr[@]}"
do
echo "$element"
done
```
## While loop
> `while` loops execute while a condition is true (0)
We can use a `while` loop as a condition in two senses:
- execute while a given condition obtains
- expand and execute a given command as long as the final command in the `while' for the command has an exit status of zero (i.e. truthy)
Here is an exampe of using `while` in the former case:
```sh
declare -i n=0
while (( n<10 ))
do
echo "n:$n"
(( n++ ))
done
```
Here is an example of using `while` in the latter case:
```sh
while read line;
do
# something
done < "$file_to_read"
```
## Until loop
> `until` loops execute until a condition is false (1)
```sh
declare -i m=0
until (( m==10 )): do
echo "m:$m"
(( m++ ))
done
```
This gives us the same output as `n` with the while loop but here it runs so long as `m==10` is false. As soon as `m` is equal to 100, the condition becomes true and hence the loop stops.

View file

@ -9,13 +9,15 @@ tags:
## Relation between commands and programs
Whenever we issue a command in bash we are really running an executable program that is associated with the command. This is why when we create our own bash scripts we must run `chmod` to make them executables. When we issue a command like `./file.sh` we are running an executable program.
Whenever we issue a command in bash we are really running an executable program that is associated with the command. This is why when we create our own bash scripts we must run `chmod` to make them executable. When we issue a command like `./file.sh` we are running an executable program.
How come, however, that when we use a program like `cd` or `npm` we dont have to type `./cd.sh` or `./npm.sh` ? Remember from our discussion of the `PATH` environment variable that whenever we use inbuilt commands like `ls` and `cd` we are automatically sourcing them from the binary directory because we have these directories in our `PATH` . Hence the shell knows in advance what these commands mean. In the case of custom scripts, these arent typically added to the `PATH` so we have to source them in order to run them.
When we run a program like `cd` or `npm` we dont have to type `./cd.sh` or `./npm.sh`. This is because a reference to the program file is already in our `$PATH`.
In the case of `cd`, this is an in-built program and as such it will be sourced from a binary and we have a reference to the binary in path. In the case of `npm`, this is not an in-built program however in order to run it we must already have it in our `PATH`.
## Passing arguments
If you think about it, a script is really just a function that runs when you source it. As such there needs to be a way for you to pass data to the function so that it can actually act like a function and take arguments. When we use for example `cd ./Desktop` we are passing a directory name as an argument to the `cd` program. We can do the same thing with our custom bash scripts.
A script is really just a function that runs when you source it. As such there needs to be a way for you to pass data to the function so that it can actually act like a function and take arguments. When we use for example `cd ./Desktop` we are passing a directory name as an argument to the `cd` program. We can do the same thing with our custom bash scripts.
To pass an argument we simply add the values after the script in the command. For example:
@ -23,7 +25,7 @@ To pass an argument we simply add the values after the script in the command. Fo
./arguments.sh Thomas 33
```
The script is as follows:
We can use built-in variables to return information about the invocation of this script:
```bash
#!/bin/bash
@ -45,9 +47,8 @@ The second argument is 33
Your name is Thomas and you are 33 years old
```
Some points to note on syntax. The `$` is used to individuate the script itself and its arguments.
Key points:
- Each argument passed is accessible from an index starting at `1` (`$1`)
- The script itself occupies the `0` position, hence we are able to log the name of the script at line 1 `$0` )
- To log the arguments as a group (for instance to later loop through them) we use `$@` .
- To get the number of arguments use `$#`
- `$0` designates the script or function that is being executed
- `$@` designates a list of all the arguments that are passed to the script
- `$1...` designates each individual argument

View file

@ -0,0 +1,20 @@
---
categories:
- Programming Languages
tags:
- shell
---
# Proper shebang syntax
This is a more portable way of writing the shebang because the location of Bash on different systems can vary.
```bash
#!/usr/bin/env Bash
```
Rather than:
```bash
#!/bin/bash
```

View file

@ -3,6 +3,7 @@ categories:
- Programming Languages
tags:
- shell
- regex
---
# Test values in Bash
@ -22,7 +23,7 @@ echo $?
[ -d /bin/zsh ] # is this binary a directory?
echo $
01 # no
1 # no
```
## Test structures
@ -61,6 +62,14 @@ We can negate a test condition with `!`:
0
```
## Running process if test succeeds or fails
We can use the following structure to run a process if a test condition obtains:
```bash
[[ -d ~ ]] && echo "/home is a directory"
```
## Extended test: `[[...]]`
When we use **double brackets** we are using _extended_ `test`.
@ -72,6 +81,16 @@ The extended test supports the standard `test` comparisons and adds other featur
[[ -d ~ || -a /bin/mash ]]; echo $?
```
### Using regular expressions
Extended test also allows us to use regular expressions as part of our test conditions. In order to test against a regular expression we use `=~` as the comparison operator.
```bash
[[ "thomas" =~ t.* ]]; echo $?
```
Here the test succeeds because "thomas" begins with "t" followed by any other character.
## Further examples
**Test if a character exists in two strings**

View file

@ -5,6 +5,8 @@ tags:
- shell
---
# Text manipulation
## Sorting strings: `sort`
If you have a `.txt` file containing text strings, each on a new line you can use the sort function to quickly put them in alphabetical order:

BIN
_img/terminal_colours.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 81 KiB