mirror of
https://codeberg.org/Mo8it/How_To_Linux.git
synced 2024-11-22 07:28:03 +00:00
Explain redirection to stderr and shebang
This commit is contained in:
parent
0f0578ca26
commit
193eb13d05
3 changed files with 54 additions and 8 deletions
|
@ -1,5 +1,5 @@
|
||||||
# Day 1
|
# Day 1
|
||||||
|
|
||||||
In this chapter, you will learn the basics of Linux in the terminal.
|
In this day, you will learn the basics of Linux in the terminal.
|
||||||
|
|
||||||
But before we start with the basics, the first section discusses why you should learn Linux in the first place.
|
But before we start with the basics, the first section discusses why you should learn Linux in the first place.
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
# Day 2
|
# Day 2
|
||||||
|
|
||||||
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.
|
In this day, 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.
|
||||||
|
|
|
@ -4,9 +4,9 @@ Task automation requires multiple instructions that have to run on demand. To co
|
||||||
|
|
||||||
Since `bash` is the default shell on most Linux distributions, we learn bash scripting.
|
Since `bash` is the default shell on most Linux distributions, we learn bash scripting.
|
||||||
|
|
||||||
## Bash scripts
|
## First script
|
||||||
|
|
||||||
Lets write our first Bash script (also for some more demonstration of the two output forms):
|
Lets write our first Bash script (also for some more demonstration of the two output forms from the last chapter):
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
#!/usr/bin/bash
|
#!/usr/bin/bash
|
||||||
|
@ -23,17 +23,63 @@ if [ "$ANSWER" == "1" ]
|
||||||
then
|
then
|
||||||
echo "Good choice!"
|
echo "Good choice!"
|
||||||
else
|
else
|
||||||
echo "Nah, that can't be right! It must be an error!" > /dev/stderr
|
echo "Nah, that can't be right! It must be an error!" 1>&2
|
||||||
fi
|
fi
|
||||||
```
|
```
|
||||||
|
|
||||||
Copy this code into a file called `which-os.sh`.
|
Copy this code into a file called `which-os.sh`.
|
||||||
|
|
||||||
Now run `chmod u+x which-os.sh`. Then run `./which-os.sh`.
|
Now run `chmod u+x which-os.sh`. Then run `./which-os.sh`. Don't worry, everything will be explained afterwards.
|
||||||
|
|
||||||
<!-- TODO: Shebang -->
|
After running the script, you will see a prompt asking you to enter a number corresponding to an operating system. If you choose Linux, you get the output "Good choice". This output is in the standard output.
|
||||||
|
|
||||||
<!-- TODO: /usr/bin/python3 -->
|
If you don't choose Linux, you get the output "Nah, that can't be right! (...)". This output is redirected to the standard error.
|
||||||
|
|
||||||
|
We did learn that we can redirect to a file using `>`. The syntax `1>&2` redirects the standard output to the standard error. On the other hand. `2>&1` redirects the standard error to the standard output.
|
||||||
|
|
||||||
|
If you want to redirect both the standard output and error to a file, you can use this syntax: `COMMAND > FILE 2>&1`.
|
||||||
|
|
||||||
|
The redirection **order** is important in this case! `COMMAND 2>&1 > FILE` will redirect the standard output **only** to the file `FILE`!
|
||||||
|
|
||||||
|
`COMMAND > FILE 2>&1` has a useful shortcut: `COMMAND &> FILE`. You can also use `&>>` to append.
|
||||||
|
|
||||||
|
All other aspects of the script above will be explained in the next sections.
|
||||||
|
|
||||||
|
## Shebang
|
||||||
|
|
||||||
|
The first line of our first script starts with `#!` which is called the _shebang_. The shebang is followed by the program that runs the script. Since the script is a bash script, we use the program `bash` to run it. But writing `bash` after the shebang is not enough. We have to specify the path to the program. We can find out the path of the program by using the command `which`:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ which bash
|
||||||
|
/usr/bin/bash
|
||||||
|
```
|
||||||
|
|
||||||
|
You can also write a Python script and add a shebang at its beginning. We can find out the path to the Python program:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ which python3
|
||||||
|
/usr/bin/python3
|
||||||
|
```
|
||||||
|
|
||||||
|
This means that we can now write this script:
|
||||||
|
|
||||||
|
```python
|
||||||
|
#!/usr/bin/python3
|
||||||
|
|
||||||
|
print("Hello world!")
|
||||||
|
```
|
||||||
|
|
||||||
|
Lets save this tiny Python script as `hello_world.py`, make it executable (will be explained later) and then run it:
|
||||||
|
|
||||||
|
```console
|
||||||
|
$ chmod u+x hello_world.py
|
||||||
|
$ ./hello_world.py
|
||||||
|
Hello world!
|
||||||
|
```
|
||||||
|
|
||||||
|
We could have written the Python script without the shebang, but then we would have to run with `python3 hello_world.py`. Adding the shebang lets you see a script as a program and ignore what language it is written in when running it.
|
||||||
|
|
||||||
|
You can use the shebang with any program that runs a script (interpreter).
|
||||||
|
|
||||||
<!-- TODO: Variables -->
|
<!-- TODO: Variables -->
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue