Mini completion of the bash tutorial

This commit is contained in:
waldek 2022-05-03 15:36:09 +02:00
parent 61a9aed48b
commit 856bab763d
1 changed files with 137 additions and 0 deletions

View File

@ -293,6 +293,143 @@ exit
waldek@helloworld:~$
```
### Using variables to store the output of command
Bash only really knows *characters*, both for sending and receiving.
We can temporarily store the output of a command using variables.
The syntax is a bit tricky at first but quickly becomes quite natural.
We can try this out on the command line.
Next we'll write a small script to leverage the power of variables and pipes.
```
waldek@metal:~$ grep $USER /etc/passwd
waldek:x:1000:1000:waldek,,,:/home/local/waldek:/bin/zsh
waldek@metal:~$ my_name=$(grep $USER /etc/passwd)
waldek@metal:~$ echo $my_name
waldek:x:1000:1000:waldek,,,:/home/local/waldek:/bin/zsh
waldek@metal:~$
```
A little but more complicated.
```
waldek@metal:~$ count=$(grep "/home" /etc/passwd | wc -l)
waldek@metal:~$ msg="there are $count users on this machine"
waldek@metal:~$ echo $msg
there are 4 users on this machine
waldek@metal:~$
```
Now a small script.
```bash
#!/bin/bash
FULLNAME="wouter gordts"
CITY="Brussels"
echo "this script was written by $FULLNAME in $CITY"
IP=$(ip a | grep -v "127.0.0.1" | grep -o -E "[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\/[[:digit:]]{1,3}")
TIME=$(date +%X)
DAY=$(date +%A)
YEAR=$(date +%Y)
echo "this computer has $IP as IP address"
echo "it is $TIME and we are a $DAY in $YEAR"
```
Which if we run it gives us the following output.
```
waldek@metal:~$ bash test.sh
this script was written by wouter gordts in Brussels
this computer has 10.1.12.53/24 as IP address
it is 02:17:02 PM and we are a Tuesday in 2022
waldek@metal:~$
```
### Getting input into the script
#### With `read`
Observe the output of the following *program*.
It's not really complicated but it will demonstrate we can do arithmetic in bash scripts as well!
```
waldek@metal:~$ bash test.sh
In which year where you born?
1986
your are probably around 36...
waldek@metal:~$
```
The output above was generated with the following code.
The **two** things to notice are the `read year` line and the `$(( $this_year - $year ))`.
The former offers the possibility to **prompt** the user for input, the latter performs a mathematical calculation with two numbers.
```bash
#!/bin/bash
echo "In which year where you born?"
read year
this_year=$(date +%Y)
echo "your are probably around $(( $this_year - $year ))..."
waldek@metal:~$
```
#### With command line arguments
We can create a similar behaviour but with command line arguments.
By doing so we don't have to answer any questions the script poses at runtime.
If we create a script that will run for a long time, it doesn't require any interaction mid way!
All necessary information is supplied by at execution time.
```
waldek@metal:~$ bash test.sh 1986
your are probably around 36...
waldek@metal:~$
```
```bash
#!/bin/bash
year=$1
this_year=$(date +%Y)
echo "your are probably around $(( $this_year - $year ))..."
```
The magic behind is the `$1` variable.
This variable represents the *first* argument on the command line.
Knowing this, what would `$4` mean?
Indeed, the *fourth* argument...
# Coding Challenge - output the exact output below
```
waldek@metal:~$ bash test.sh hello world 1986 35 foo bar linux rulez...
hello waldek, my name is test.sh
you supplied 8 arguments on the command line...
here are all of them on one line: hello world 1986 35 foo bar linux rulez...
waldek@metal:~$
```
<details>
<summary>Spoiler warning!</summary>
```bash
#!/bin/bash
echo "hello $USER, my name is $0"
echo "you supplied $# arguments on the command line..."
echo "here are all of them on one line: $@"
```
</details>
### What is a Bash Script - What are they, how do they work and how to run them.
[Ryan's tutorials](https://ryanstutorials.net/bash-scripting-tutorial/bash-script.php)