1
0
Fork 0
mirror of https://codeberg.org/Mo8it/How_To_Linux.git synced 2024-11-21 19:18:02 +00:00

Improve and plan rest of content of day 2

This commit is contained in:
Mo 2022-09-23 19:48:13 +02:00
parent d92f748fe7
commit 0f0578ca26
4 changed files with 94 additions and 40 deletions

View file

@ -9,6 +9,7 @@
- [Tasks](day_1/tasks.md)
- [Day 2](day_2/README.md)
- [Shell glue](day_2/glue.md)
- [Shell scripting](day_2/shell_scripting.md)
- [Tasks](day_2/tasks.md)
- [Day 3](day_3/README.md)
- [Notes](day_3/notes.md)

View file

@ -1,3 +1,3 @@
# Day 2
Coming soon...
In this chapter, we will learn how to glue commands together to benefit from their modularity. Later, we will write our own shell scripts to achieve some automation.

View file

@ -1,8 +1,8 @@
# Shell glue
When you run something in the terminal, then you are interacting with the so called `shell`.
When you run something in the terminal, then you are interacting with the so called _shell_.
The default shell on almost all Linux systems is `bash`. (We will learn about the `fish` shell later)
The default shell on almost all Linux systems is `bash`. (We will learn about the `fish` shell later 🐠)
The shell has the power to glue commands together to make the impossible possible! Lets use some gluing magic!
@ -28,18 +28,18 @@ Lets count the number of lines of the html file of the homepage of this book:
```console
$ curl -s https://how-to-linux.mo8it.xyz | wc -l
201
220
```
The option `-s` tells `curl` to be silent and not show progress information.
You can see that `wc` did count the number of lines. We did just combine two completely different tools with some pipes glue!
How about counting the number times the word "Linux" was mentioned on the homepage?
How about counting the number of lines that contain the word "Linux" on the homepage?
To do so, we will add a new pipe inbetween!
`grep` is a command that searches for matches of a specified pattern. Each match is printed in a new line.
`grep` is a command that searches for matches of a specified pattern. Each line with a match is printed in a new line.
To demonstrate `grep`, here is an usage example:
@ -55,14 +55,14 @@ Back to the main example:
```console
$ curl -s https://how-to-linux.mo8it.xyz | grep "Linux" | wc -l
6
7
```
You can see that you can use multiple pipes. This allows for almost infinite combinations!
Being able to combine commands is the reason why many commands are simple. They do one thing and do it well! To do more, combine them!
This is much more flexible and powerful that a program that tries to do a lot of things.
This is much more flexible and powerful than a program that tries to do a lot of things.
## Input, output
@ -74,24 +74,24 @@ The standard output has the number 1 while the standard error has the number 2.
Normal output is sent to the standard output. Errors (and sometimes output that is not very important) are sent to the standard error.
You can redirect the standard output or the standard error to a _stream_. Normally you will redirect the output to a file. Other stream forms are not relevant for now.
You can redirect the standard output or the standard error to a file!
## Redirections
Lets see how you can redirect the output of commands to a file.
If you just run `curl -s https://how-to-linux.mo8it.xyz`, you will the html file printed in the terminal. Lets redirect the output to a html file on your disk:
If you just run `curl -s https://how-to-linux.mo8it.xyz`, you will see the html file printed to the terminal. Lets redirect the output to a html file on your disk:
```console
$ curl -s https://how-to-linux.mo8it.xyz > how-to-linux.html
```
Now view the content of the new file how-to-linux.html. You will be able to see the same output from the terminal without redirection.
Now view the content of the new file `how-to-linux.html`. You will see the same output from the terminal without redirection.
Now try this command:
```console
$ curl https://not-existent-site.mo8it.xyz > test.html
$ curl https://non-existent-site.mo8it.xyz > test.html
curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html
@ -106,7 +106,7 @@ You will see that the file is empty since `curl` did not find a page to show as
If you are using this command in a script, then it might be wise to redirect the error to a log file:
```console
$ curl https://not-existent-site.mo8it.xyz 2> curl.log
$ curl https://non-existent-site.mo8it.xyz 2> curl.log
$ cat curl.log
curl: (60) SSL certificate problem: self-signed certificate
More details here: https://curl.se/docs/sslcerts.html
@ -123,30 +123,3 @@ Did you notice the number 2 before the redirection symbol `2>`?
The last section did mention that the number of the standard error is 2. Therefore, 2 has to be specified to redirect the errors.
If you don't specify a number, then it is equivalent to 1 which stands for the standard output. This means that `>` is equivalent to `1>`.
## Bash scripts
Lets write our first Bash script (also for some more demonstration of the two outputs):
```bash
#!/usr/bin/bash
echo "What is your favorite operating system after reading this book?"
echo "1. Linux"
echo "2. Windows"
echo "3. Mac"
echo -n "Enter a number: "
read ANSWER
if [ "$ANSWER" == "1" ]
then
echo "Good choice!"
else
echo "Nah, that can't be right! It must be an error!" > /dev/stderr
fi
```
Copy this code into a file called `which-os.sh`.
Now run `chmod +x which-os.sh`. Then run `./which-os.sh`.

View file

@ -0,0 +1,80 @@
# Shell scripting
Task automation requires multiple instructions that have to run on demand. To combine multiple instructions, we need to write a shell script.
Since `bash` is the default shell on most Linux distributions, we learn bash scripting.
## Bash scripts
Lets write our first Bash script (also for some more demonstration of the two output forms):
```bash
#!/usr/bin/bash
echo "What is your favorite operating system after reading this book?"
echo "1. Linux"
echo "2. Windows"
echo "3. Mac"
echo -n "Enter a number: "
read ANSWER
if [ "$ANSWER" == "1" ]
then
echo "Good choice!"
else
echo "Nah, that can't be right! It must be an error!" > /dev/stderr
fi
```
Copy this code into a file called `which-os.sh`.
Now run `chmod u+x which-os.sh`. Then run `./which-os.sh`.
<!-- TODO: Shebang -->
<!-- TODO: /usr/bin/python3 -->
<!-- TODO: Variables -->
<!-- TODO: $ -->
<!-- TODO: Escaping '$' -->
<!-- TODO: ${} -->
<!-- TODO: $() -->
<!-- TODO: read -->
<!-- TODO: if -->
<!-- TODO: test -->
<!-- TODO: if [ ! -f ] -->
<!-- TODO: else -->
<!-- TODO: else if -->
<!-- TODO: case -->
<!-- TODO: for -->
<!-- TODO: while -->
<!-- TODO: command | while read -->
<!-- TODO: math (( )) -->
<!-- TODO: Long command on multiple lines \ -->
<!-- TODO: Permissions -->
<!-- TODO: r,w,x -->
<!-- TODO: u,g,o -->
<!-- TODO: chmod codes -->
<!-- TODO: https://chmodcommand.com -->