Skip to content

Commit

Permalink
Update bash cheatsheet
Browse files Browse the repository at this point in the history
  • Loading branch information
abregman committed Nov 7, 2021
1 parent 505cd74 commit df738a8
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 6 deletions.
7 changes: 3 additions & 4 deletions resources/ansible.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
## Ansible

### Videos
### Learn Ansible

Name | Comments
:------ |:--------:
[Ansible 101 - Jeff Geerling](https://www.youtube.com/watch?v=goclfp6a2IQ&list=RDCMUCR-DXc1voovS8nhAvccRZhg&index=1) | Comprehensive practical way to learn Ansible
[What is Ansible? - TechWorld with Nana](https://www.youtube.com/watch?v=1id6ERvfozo) | High-level short overview of Ansible
[How to write your first Ansible playbook](https://www.youtube.com/watch?v=BeYUQaFS-vg) | Practical video on how to write a playbook in Ansible
[Learning Ansible basics - Red Hat](https://www.redhat.com/en/topics/automation/learning-ansible-tutorial) | Red Hat's guide on how to learn Ansible basics + links to the content itself
[Introduction to Ansible - 2021](https://medium.com/@bagusays/introduction-to-ansible-82f2bc12cd87) |

### Articles

Name | Comments
:------ |:--------:
[Learning Ansible basics - Red Hat](https://www.redhat.com/en/topics/automation/learning-ansible-tutorial) | Red Hat's guide on how to learn Ansible basics + links to the content itself
[Writing reliable Ansible Playbooks - 2021](https://dev.to/xlab_si/writing-reliable-ansible-playbooks-295i) |
[Introduction to Ansible - 2021](https://medium.com/@bagusays/introduction-to-ansible-82f2bc12cd87) |
[A CI/CD Pipeline Project for a Trunk-Based Development Strategy in a Kubernetes Environment](https://medium.com/swlh/a-ci-cd-pipeline-project-for-a-trunk-based-development-strategy-in-a-kubernetes-environment-c4ffea9700fe) |

### Books
Expand Down
8 changes: 7 additions & 1 deletion resources/aws.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# AWS

## Infrastructure

Name | Comments
:------|:------:
[infrastructure.aws](https://infrastructure.aws) | AWS Infrastructure Overview
[Regions and Availability Zones](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services) | See services list per region and AZ

## Tools and Projects

Name | Comments
Expand All @@ -23,7 +30,6 @@ Name | Comments
[terraformer](https://github.com/GoogleCloudPlatform/terraformer) | "A CLI tool that generates tf/json and tfstate files based on existing infrastructure (reverse Terraform)."
[terraforming](https://github.com/dtan4/terraforming) | "Export existing AWS resources to Terraform style (tf, tfstate)"


## Videos

Name | Comments
Expand Down
90 changes: 90 additions & 0 deletions resources/bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ x=$(date) -> Yes :D

* Set variable with default value (string): `x=${x:-'some_default'}`
* Set variable with default value (variable): `y=${y:-$z}`
* return value of a program: `$?`
* Check if variable is empty: `if [ -z "$var" ]; then`
* Variable length: `${#string}`

#### Arguments

Expand All @@ -129,6 +132,34 @@ if [ "$#" -lt 1 ]; then
fi
```

* Check if two arguments were passed

```
if [ "$#" -ne 2 ]; then
echo 'Please pass two arguments'
exit 1
fi
```

* Check if two arguments were passed and both are numbers

```
re='^[0-9]+$'
if ! [[ $1 =~ $re && $2 =~ $re ]]; then
echo "Oh no...I need two numbers"
exit 2
fi
```

* Check if arguments' strings length is equal

```
if [ ${#1} -ne ${#2} ]; then
echo 'Not equal`
exit 1
fi
```

#### Files

* check if file exists
Expand All @@ -141,8 +172,67 @@ fi
```

* check if directory exists

```
DIR=/some/dir
if [ -d "$DIR"]; then
echo "$DIR" exists"
fi
```

#### Loops

* Iterate over a string: `for i in $(seq 1 ${#1}); do`

#### Arithmetic Operations

* print the sum of two numbers: `echo $((20+17))`
* Check factor: `if [ $(($1 % 3)) -eq 0 ]; then`

#### Extract Patterns

* Extract date with sed: `echo $line | sed 's/.*\[//g;s/].*//g;s/:.*//g'`
* Extract first field (space separator) with awk: `echo $line | awk '{print $1}'`

#### Dictionary / Hash Table

* Define a dictionary: `declare -A somedict`
* Print one value based on given key: `echo ${somedict[some_key]}`
* Print all the keys of a dictionary: `echo ${!somedict[*]}`
* Check if key exists: `if [[ -v some_dict[$day] ]]; then`

* Update dict based values and generate top 10:

```
function update_dict() {
declare -A some_dict
while read line; do
day=$line
if [[ -v some_dict[$day] ]]; then
some_dict[$day]=$((some_dict[$day]+1))
else
some_dict[$day]=1
fi
done < $FILE
for day in ${!some_dict[@]}; do echo ${some_dict[$day]} $day; done | sort -rn | head -10
}
```

#### Common algorithms

* Hamming distance

```
distance=0
for i in $(seq 1 ${#1}); do
if [ ${1:$i-1:1} != ${2:$i-1:1} ]; then
distance=$((distance+1))
fi
done
echo $distance
```

#### Text Manipulation

* Take the first letter of every word in a line: `echo $line | sed 's/\(.\)[^ ]* */\1/g'`
8 changes: 7 additions & 1 deletion resources/kubernetes.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ Name | Comments
:------ |:--------:
[Kubernetes Networking](https://github.com/nleiva/kubernetes-networking-links) | Kubernetes Networking Resources
[Liveness and Readiness Probes](https://www.openshift.com/blog/liveness-and-readiness-probes) |
[Kubernetes Troubleshooting Visual Guide](https://learnk8s.io/troubleshooting-deployments?fbclid=IwAR2k6ziNfhBe--CKoYP6qh5_lHYM7_kruDjc1EcyrpgyV_tKJzQlwiuA_Jk) |

### Kubernetes - Troubleshooting

Name | Comments
:------ |:--------:
[troubleshoot.sh](https://troubleshoot.sh) | "A kubectl plugin providing diagnostic tools for Kubernetes applications"
[Kubernetes Troubleshooting Visual Guide](https://learnk8s.io/troubleshooting-deployments) |

### Kubernetes - Security

Expand Down
8 changes: 8 additions & 0 deletions resources/linux.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ sudo ausearch -m avc -m user_avc -m selinux_err -m user_selinux_err -i -ts today
* Reset "document text" value: `gsettings reset org.gnome.desktop.interface document-font-name`
* Reset "Legacy Window Titles": `gsettings reset org.gnome.desktop.wm.preferences titlebar-font`

#### Files

* Sort files by size: `ls -l | sort -nk5`

#### Misc

* Generate 8 digit random number: `shuf -i 9999999-99999999 -n 1`

## Checklist

Check your Linux educational progress with the following list:
Expand Down

0 comments on commit df738a8

Please sign in to comment.