Add questions and exercises
In addition, move shell scripting questions into a separate file.
This commit is contained in:
268
README.md
268
README.md
@ -2,7 +2,7 @@
|
||||
|
||||
:information_source: This repo contains questions and exercises on various technical topics, sometimes related to DevOps and SRE :)
|
||||
|
||||
:bar_chart: There are currently **1962** questions
|
||||
:bar_chart: There are currently **1999** questions
|
||||
|
||||
:books: To learn more about DevOps and SRE, check the resources in [devops-resources](https://github.com/bregman-arie/devops-resources) repository
|
||||
|
||||
@ -32,7 +32,7 @@
|
||||
<td align="center"><a href="#programming"><img src="images/programming.png" width="75px;" height="75px;" alt="programming"/><br /><b>Programming</b></a></td>
|
||||
<td align="center"><a href="#python"><img src="images/python.png" width="80px;" height="75px;" alt="Python"/><br /><b>Python</b></a></td>
|
||||
<td align="center"><a href="#go"><img src="images/Go.png" width="75px;" height="75px;" alt="go"/><br /><b>Go</b></a></td>
|
||||
<td align="center"><a href="#shell-scripting"><img src="images/bash.png" width="70px;" height="75px;" alt="Bash"/><br /><b>Shell Scripting</b></a></td>
|
||||
<td align="center"><a href="exercises/shell/README.md"><img src="images/bash.png" width="70px;" height="75px;" alt="Bash"/><br /><b>Shell Scripting</b></a></td>
|
||||
<td align="center"><a href="#kubernetes"><img src="images/kubernetes.png" width="75px;" height="75px;" alt="kubernetes"/><br /><b>Kubernetes</b></a></td>
|
||||
<td align="center"><a href="#prometheus"><img src="images/prometheus.png" width="75px;" height="75px;" alt="Prometheus"/><br /><b>Prometheus</b></a></td>
|
||||
</tr>
|
||||
@ -5313,270 +5313,6 @@ If more pods are running than needed -> it deletes some of them<br>
|
||||
If not enough pods are running -> it creates more
|
||||
</b></details>
|
||||
|
||||
## Shell Scripting
|
||||
|
||||
### Shell Scripting Exercises
|
||||
|
||||
|Name|Topic|Objective & Instructions|Solution|Comments|
|
||||
|--------|--------|------|----|----|
|
||||
|Hello World|Variables|[Exercise](exercises/shell/hello_world.md)|[Solution](exercises/shell/solutions/hello_world.md) | Basic
|
||||
|Basic date|Variables|[Exercise](exercises/shell/basic_date.md)|[Solution](exercises/shell/solutions/basic_date.md) | Basic
|
||||
|Great Day|Variables|[Exercise](exercises/shell/great_day.md)|[Solution](exercises/shell/solutions/great_day.md) | Basic
|
||||
|Factors|Arithmetic|[Exercise](exercises/shell/factors.md)|[Solution](exercises/shell/solutions/factors.md) | Basic
|
||||
|Argument Check|Conditionals|[Exercise](exercises/shell/argument_check.md)|[Solution](exercises/shell/solutions/argument_check.md) | Basic
|
||||
|Files Size|For Loops|[Exercise](exercises/shell/files_size.md)|[Solution](exercises/shell/solutions/files_size.md) | Basic
|
||||
|Count Chars|Input + While Loops|[Exercise](exercises/shell/count_chars.md)|[Solution](exercises/shell/solutions/count_chars.md) | Basic
|
||||
|Sum|Functions|[Exercise](exercises/shell/sum.md)|[Solution](exercises/shell/solutions/sum.md) | Basic
|
||||
|Number of Arguments|Case Statement|[Exercise](exercises/shell/num_of_args.md)|[Solution](exercises/shell/solutions/num_of_args.md) | Basic
|
||||
|Empty Files|Misc|[Exercise](exercises/shell/empty_files.md)|[Solution](exercises/shell/solutions/empty_files.md) | Basic
|
||||
|Directories Comparison|Misc|[Exercise](exercises/shell/directories_comparison.md)| :( | Basic
|
||||
|It's alive!|Misc|[Exercise](exercises/shell/host_status.md)|[Solution](exercises/shell/solutions/host_status.md) | Intermediate
|
||||
|
||||
## Shell Scripting - Self Assessment
|
||||
|
||||
<details>
|
||||
<summary>What does this line in shell scripts means?: <code>#!/bin/bash</code></summary><br><b>
|
||||
|
||||
|
||||
`#!/bin/bash` is She-bang
|
||||
|
||||
/bin/bash is the most common shell used as default shell for user login of the linux system. The shell’s name is an acronym for Bourne-again shell. Bash can execute the vast majority of scripts and thus is widely used because it has more features, is well developed and better syntax.
|
||||
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>True or False? When a certain command/line fails in a shell script, the shell script, by default, will exit and stop running</summary><br><b>
|
||||
|
||||
Depends on the language and settings used.
|
||||
If the script is a bash script then this statement is true. When a script written in Bash fails to run a certain command it will keep running and will execute all other commands mentioned after the command which failed.
|
||||
|
||||
Most of the time we might actually want the opposite to happen. In order to make Bash exist when a specific command fails, use 'set -e' in your script.
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What do you tend to include in every script you write?</summary><br><b>
|
||||
|
||||
Few example:
|
||||
|
||||
* Comments on how to run it and/or what it does
|
||||
* If a shell script, adding "set -e" since I want the script to exit if a certain command failed
|
||||
|
||||
You can have an entirely different answer. It's based only on your experience and preferences.
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Today we have tools and technologies like Ansible, Puppet, Chef, ... Why would someone still use shell scripting?</summary><br><b>
|
||||
|
||||
* Speed
|
||||
* Flexibility
|
||||
* The module we need doesn't exist (perhaps a weak point because most CM technologies allow to use what is known as "shell" module)
|
||||
* We are delivering the scripts to customers who don't have access to the public network and don't necessarily have Ansible installed on their systems.
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Variables
|
||||
|
||||
<details>
|
||||
<summary>How to define a variable with the value "Hello World"?</summary><br><b>
|
||||
|
||||
`HW="Hello World`
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to define a variable with the value of the current date?</summary><br><b>
|
||||
|
||||
`DATE=$(date)`
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to print the first argument passed to a script?</summary><br><b>
|
||||
|
||||
`echo $1`
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Write a script to print "yay" unless an argument was passed and then print that argument</summary><br><b>
|
||||
|
||||
```
|
||||
echo "${1:-yay}"
|
||||
```
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What would be the output of the following script?
|
||||
|
||||
```
|
||||
#!/usr/bin/env bash
|
||||
NINJA_TURTLE=Donatello
|
||||
function the_best_ninja_turtle {
|
||||
local NINJA_TURTLE=Michelangelo
|
||||
echo $NINJA_TURTLE
|
||||
}
|
||||
NINJA_TURTLE=Raphael
|
||||
the_best_ninja_turtle
|
||||
```
|
||||
</summary><br><b>
|
||||
Michelangelo
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Explain what would be the result of each command:
|
||||
|
||||
* <code>echo $0</code>
|
||||
* <code>echo $?</code>
|
||||
* <code>echo $$</code>
|
||||
* <code>echo $#</code></summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What is <code>$@</code>?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What is difference between <code>$@</code> and <code>$*</code>?</summary><br><b>
|
||||
|
||||
`$@` is an array of all the arguments passed to the script
|
||||
`$*` is a single string of all the arguments passed to the script
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How do you get input from the user in shell scripts?</summary><br><b>
|
||||
|
||||
Using the keyword <code>read</code> so for example <code>read x</code> will wait for user input and will store it in the variable x.
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to compare variables length?</summary><br><b>
|
||||
|
||||
```
|
||||
if [ ${#1} -ne ${#2} ]; then
|
||||
...
|
||||
```
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Conditionals
|
||||
|
||||
<details>
|
||||
<summary>Explain conditionals and demonstrate how to use them</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>In shell scripting, how to negate a conditional?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>In shell scripting, how to check if a given argument is a number?</summary><br><b>
|
||||
|
||||
```
|
||||
regex='^[0-9]+$'
|
||||
if [[ ${var//*.} =~ $regex ]]; then
|
||||
...
|
||||
```
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Arithmetic Operations
|
||||
|
||||
<details>
|
||||
<summary>How to perform arithmetic operations on numbers?</summary><br><b>
|
||||
|
||||
One way: `$(( 1 + 2 ))`
|
||||
Another way: `expr 1 + 2`
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to perform arithmetic operations on numbers?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to check if a given number has 4 as a factor?</summary><br><b>
|
||||
|
||||
`if [ $(($1 % 4)) -eq 0 ]; then`
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Loops
|
||||
|
||||
<details>
|
||||
<summary>What is a loop? What types of loops are you familiar with?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Demonstrate how to use loops</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Troubleshooting
|
||||
|
||||
<details>
|
||||
<summary>How do you debug shell scripts?</summary><br><b>
|
||||
|
||||
Answer depends on the language you are using for writing your scripts. If Bash is used for example then:
|
||||
|
||||
* Adding -x to the script I'm running in Bash
|
||||
* Old good way of adding echo statements
|
||||
|
||||
If Python, then using pdb is very useful.
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Running the following bash script, we don't get 2 as a result, why?
|
||||
|
||||
```
|
||||
x = 2
|
||||
echo $x
|
||||
```
|
||||
</summary><br><b>
|
||||
|
||||
Should be `x=2`
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Substring
|
||||
|
||||
<details>
|
||||
<summary>How to extract everything after the last dot in a string?</summary><br><b>
|
||||
|
||||
`${var//*.}`
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>How to extract everything before the last dot in a string?</summary><br><b>
|
||||
|
||||
${var%.*}
|
||||
</b></details>
|
||||
|
||||
#### Shell Scripting - Misc
|
||||
|
||||
<details>
|
||||
<summary>Generate 8 digit random number</summary><br><b>
|
||||
|
||||
shuf -i 9999999-99999999 -n 1
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>Can you give an example to some Bash best practices?</summary><br><b>
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What is the ternary operator? How do you use it in bash?</summary><br><b>
|
||||
|
||||
A short way of using if/else. An example:
|
||||
|
||||
[[ $a = 1 ]] && b="yes, equal" || b="nope"
|
||||
</b></details>
|
||||
|
||||
<details>
|
||||
<summary>What does the following code do and when would you use it?
|
||||
|
||||
<code>diff <(ls /tmp) <(ls /var/tmp)</code>
|
||||
|
||||
</summary><br>
|
||||
It is called 'process substitution'. It provides a way to pass the output of a command to another command when using a pipe <code>|</code> is not possible. It can be used when a command does not support <code>STDIN</code> or you need the output of multiple commands.
|
||||
https://superuser.com/a/1060002/167769
|
||||
</details>
|
||||
|
||||
<details>
|
||||
<summary>What are you using for testing shell scripts?</summary><br><b>
|
||||
|
||||
bats
|
||||
</b></details>
|
||||
|
||||
## SQL
|
||||
|
||||
### SQL Exercises
|
||||
|
Reference in New Issue
Block a user