topics/shell/README.md
| Name | Topic | Objective & Instructions | Solution | Comments |
|---|---|---|---|---|
| Hello World | Variables | Exercise | Solution | Basic |
| Basic date | Variables | Exercise | Solution | Basic |
| Great Day | Variables | Exercise | Solution | Basic |
| Factors | Arithmetic | Exercise | Solution | Basic |
| Argument Check | Conditionals | Exercise | Solution | Basic |
| Files Size | For Loops | Exercise | Solution | Basic |
| Count Chars | Input + While Loops | Exercise | Solution | Basic |
| Sum | Functions | Exercise | Solution | Basic |
| Number of Arguments | Case Statement | Exercise | Solution | Basic |
| Empty Files | Misc | Exercise | Solution | Basic |
| Directories Comparison | Misc | Exercise | Solution | Basic |
| It's alive! | Misc | Exercise | Solution | Intermediate |
#!/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> <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> <b>Few example:
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> <b>HW="Hello World
</b></details>
DATE=$(date)
</b></details>
echo $1
</b></details>
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
</b></details>
<details> <summary>What is <code>$@</code>?</summary> <b> </b></details> <details> <summary>What is difference between <code>$@</code> and <code>$*</code>?</summary> <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>
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> <b>if [ ${#1} -ne ${#2} ]; then
...
</b></details>
regex='^[0-9]+$'
if [[ ${var//*.} =~ $regex ]]; then
...
</b></details>
One way: $(( 1 + 2 ))
Another way: expr 1 + 2
</b></details>
if [ $(($1 % 4)) -eq 0 ]; then
</b></details>
Answer depends on the language you are using for writing your scripts. If Bash is used for example then:
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
Should be x=2
</b></details>
${var//*.}
</b></details>
${var%.*} </b></details>
shuf -i 9999999-99999999 -n 1 </b></details>
<details> <summary>Can you give an example to some Bash best practices?</summary> <b> </b></details> <details> <summary>What is the ternary operator? How do you use it in bash?</summary> <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>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> <b>bats </b></details>