adds text manipulation exercises

This commit is contained in:
waldek 2022-06-28 20:22:42 +02:00
parent fe76dac62a
commit 688bcab1b7
5 changed files with 131 additions and 6 deletions

View File

@ -0,0 +1,35 @@
# Encapsulation preface
TODO - outline users permissions
# `chroot`
TODO - ultra basic chroot creation
## With a custom script
# `debootstrap`
TODO - practical root build
# Using `chroot` to fix a system
## `schroot`
# `systemd-nspawn`
## Foreign images
## Creating your own images with `debos`
# links
* [hub](https://hub.nspawn.org/images/)
* [tutorial](https://blog.selectel.com/systemd-containers-introduction-systemd-nspawn/)
* [docker nspawn](https://seanmcgary.com/posts/run-docker-containers-with-systemd-nspawn)
* [debian](https://wiki.debian.org/nspawn)
* [arch](https://wiki.archlinux.org/title/systemd-nspawn#Use_a_%22macvlan%22_or_%22ipvlan%22_interface)
* [systemd-sysext](https://0pointer.net/blog/testing-my-system-code-in-usr-without-modifying-usr.html)
* [schroot](https://wiki.debian.org/RichardDarst/Schroot)

View File

@ -4,6 +4,7 @@ What happens when we log into a server or when we open up a terminal with `bash`
As always the manual can tell us quite a bit. As always the manual can tell us quite a bit.
Quite often you'll find a list of configuration files used by a program near the end of the manual. Quite often you'll find a list of configuration files used by a program near the end of the manual.
Sometimes not, your millage may vary but here is the files section of the `bash` manual. Sometimes not, your millage may vary but here is the files section of the `bash` manual.
If you can't find a list of files the program you want to investigate searches for, there are other ways of digging deep into what a program is *doing* but that is for a later date.
``` ```
FILES FILES
@ -44,6 +45,7 @@ waldek@tester:~$
Let's have a look at them one by one. Let's have a look at them one by one.
First the `.bash_history`. First the `.bash_history`.
This contains a history of all the commands I ran on this computer! This contains a history of all the commands I ran on this computer!
The arrow keys navigate his file when you search through your history.
Quite handy for when we forget how to do something but we know we've done it before. Quite handy for when we forget how to do something but we know we've done it before.
You can use `grep` to search the file but there is also a shortcut you can use, `ctrl-r`. You can use `grep` to search the file but there is also a shortcut you can use, `ctrl-r`.
@ -97,6 +99,7 @@ waldek@tester:~$
Aha! Aha!
The comment on the first line mentions *non-login shells* so there must be a difference! The comment on the first line mentions *non-login shells* so there must be a difference!
This is a very important configuration file and I urge you to read it, especially the comments. This is a very important configuration file and I urge you to read it, especially the comments.
Notice how the length of your history file is set in here?
What kind of content is in this file? What kind of content is in this file?
Last but not least `.profile`. Last but not least `.profile`.
@ -200,7 +203,6 @@ A quick look at the `man su` tells us that.
o changes to the target user's home directory o changes to the target user's home directory
o sets argv[0] of the shell to '-' in order to make the shell a login shell o sets argv[0] of the shell to '-' in order to make the shell a login shell
``` ```
So we can do the following. So we can do the following.
@ -249,7 +251,7 @@ waldek@tester:~$
``` ```
What about `ssh` connections? What about `ssh` connections?
I'm running the server on port 2222 because it's a contained with host networking. I'm running the server on port 2222 because it's a container with host networking.
It's a bit too early to go into too much detail but by then end of the course you'll fully understand what's happening! It's a bit too early to go into too much detail but by then end of the course you'll fully understand what's happening!
``` ```
@ -304,15 +306,15 @@ waldek@tester:~$ cat .bashrc .bash_logout .profile
waldek@tester:~$ waldek@tester:~$
``` ```
We don't see any differences but our shell is *less* powerful. We don't see any big differences but our shell is *less* powerful.
For example, autocomplete does not work anymore on things like `systemctl`. For example, autocomplete does not work anymore on things like `systemctl` or `apt`.
We now have a `bash` shell that has only sourced actual content from the following files. We now have a `bash` shell that has only sourced actual content from the following files.
* `/etc/profile` * `/etc/profile`
* `/etc/bash.bashrc` * `/etc/bash.bashrc`
* `/etc/bash.bash.logout` if it exists * `/etc/bash.bash.logout` if it exists
As these are system files we can only edit them as `root`. As these are system files we can only edit them as `root` or via `sudo`.
I went a head and added a comment `#` in front of every line. I went a head and added a comment `#` in front of every line.
When I now log in on a tty I get the following. When I now log in on a tty I get the following.
@ -367,7 +369,7 @@ $PS1 $PS2 $PS4
``` ```
Your prompt is defined by the `$PS1` variable. Your prompt is defined by the `$PS1` variable.
By changing this we can change it's behaviour. By setting this we can change it's behaviour.
``` ```
-bash-5.1$ PS1="helloworld" -bash-5.1$ PS1="helloworld"
@ -485,6 +487,44 @@ it is 17:06:58
waldek@tester:~ -> waldek@tester:~ ->
``` ```
## Saving our changes
When you exit your shell, and log back in, you'll notice the prompt is back to normal.
If we want to *save* the one we have we need to place it in one of the files that get *sourced* when our session starts.
I would recommend you make your changes to the `.bashrc` file and have a `.profile` that sources the `.bashrc` if we run an interactive `bash` shell.
You can find some interesting notes [here](https://superuser.com/questions/789448/choosing-between-bashrc-profile-bash-profile-etc).
So let's get started!
```
waldek@tester:~ -> tail -n +1 .profile .bashrc # a nice trick to show file content with header ;)
==> .profile <==
if [ "$BASH" ]; then
. ~/.bashrc
fi
==> .bashrc <==
PS1="\u@\H:\w -> "
echo "sourced"
waldek@tester:~ ->
```
I'm writing this documentation on a laptop so it would be nice to see my battery status on the command line.
By now you probably guessed it, but the sourced files are scripts so we can use every trick we've learned during our `bash` scripting classes.
I'll make a function that prints the status of the battery and will include this function in the prompt.
```bash
function get_battery_status () {
status=$(cat /sys/class/power_supply/BAT0/status)
echo -e $status # -e for no newline
}
PS1="\u@\H:\w [\$(get_battery_status)] -> " # need to escape the $ sign or use single quotes!
#PS1='\u@\H:\w [$(get_battery_status)] -> ' # this would work as well.. ah, bash...
echo "sourced"
```
## Decoding the *base* prompt ## Decoding the *base* prompt
On a modern install your `$PS1` will probably be as one of the two below. On a modern install your `$PS1` will probably be as one of the two below.

View File

@ -0,0 +1,36 @@
market Salad
mircocity case
diy plantboxes 4
market Soup
books shell scripting bible
books advanced python
market Dressings 2
mircocity monitor 4
diy bulbs 10
market Oil 2
market Pasta 4
diy nails 50
market Cheese
market Dairy
market Condiments
diy hose
mircocity hdd 6
mircocity motherboard
mircocity ssd
diy hammer
mircocity cpu
market Sauces 6
mircocity tablet
market Bread
diy bucket
market Rice
diy drill
market Seafood
market Meat
market Eggs 12
diy soil
market Cereal
mircocity keyboard
mircocity ram 4
mircocity graphics card
diy screws 75

View File

@ -1808,3 +1808,15 @@ Most of these pipes are not actually *useful* but I hope they illustrate the fle
* find valid IP addresses * find valid IP addresses
* find all ugly filenames and replace with better names (replace with \_) * find all ugly filenames and replace with better names (replace with \_)
* regex crossword puzzle * regex crossword puzzle
# Exercise
Download [this](../assets/shopping_random.list) shopping list and extract me the following.
1. different shops I need to go to, sorted by amount of items to get at each shop
2. list of item of one shop but, ordered by how many times I need the item
3. list of only the items I need multiples of
4. think of some additional creative was of extracting information or maybe make a few lists yourself

View File

@ -28,6 +28,8 @@
* big [list](https://training-course-material.com/training/Debian_exercises) of course with exercises * big [list](https://training-course-material.com/training/Debian_exercises) of course with exercises
* [systemd-boot](https://blobfolio.com/2018/replace-grub2-with-systemd-boot-on-ubuntu-18-04/) tutorial * [systemd-boot](https://blobfolio.com/2018/replace-grub2-with-systemd-boot-on-ubuntu-18-04/) tutorial
* goaccess script for log [interpretation](https://github.com/stockrt/nginx2goaccess/blob/master/nginx2goaccess.sh) * goaccess script for log [interpretation](https://github.com/stockrt/nginx2goaccess/blob/master/nginx2goaccess.sh)
* advanced [htop](https://peteris.rocks/blog/htop/)
* systemd [tips and tricks](https://www.freedesktop.org/wiki/Software/systemd/TipsAndTricks/)
## started ## started