Skip to content

Commit 9a8020d

Browse files
committed
content: improved merging and scripting, added processes bonus
1 parent 2a2b75b commit 9a8020d

File tree

3 files changed

+256
-28
lines changed

3 files changed

+256
-28
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
---
2+
title: 'Managing processes and system resources'
3+
description: 'Process management, special variables, and even more Unix & Bash features'
4+
hasSlides: false
5+
links: {
6+
}
7+
---
8+
9+
## Searching the command history
10+
11+
In interactive mode, you can use `CTRL+R` to search through
12+
the history of previously executed commands.
13+
14+
## System status
15+
16+
This section will explore a couple of the most popular commands for
17+
reviewing important system resources:
18+
19+
- memory
20+
- disk usage
21+
- network address
22+
- running processes
23+
24+
### Memory
25+
26+
Use the `free` command to examine the how your RAM is used:
27+
28+
```bash
29+
free -h
30+
# prints:
31+
#
32+
# total used free shared cache available
33+
# Mem: 31Gi 6.8Gi 19Gi 2.0Gi 6.8Gi 24Gi
34+
```
35+
36+
### Disk usage
37+
38+
The `du` command can tell you the size of a file.
39+
The `df` command tells you how much space is left on your hard drive:
40+
41+
```bash
42+
df -h
43+
# prints
44+
# Filesystem Size Used Avail Use% Mounted on
45+
# /dev/nvme0n1p5 324G 26G 298G 8% /
46+
# /dev/nvme0n1p1 256M 256M 0 100% /boot
47+
```
48+
49+
## Network address
50+
51+
To see your network address use the `ip addr` command:
52+
53+
```bash
54+
ip addr
55+
# prints information about all network interfaces
56+
57+
# -j makes ip print its output as JSON
58+
# jq is a json query command line tool
59+
ip -j -4 addr | jq '.[].addr_info[].local'
60+
# prints only a list of all local ip4 addresses
61+
```
62+
63+
### Processes
64+
65+
The `ps` command returns a list of running proceses:
66+
67+
```bash
68+
# returns only processese in the current shell
69+
ps
70+
71+
# returns all processes, including system servies
72+
# and the owning user
73+
ps aux
74+
```
75+
76+
### top
77+
78+
`top` is popular utility that combines a lot of system information
79+
and provides ways to filter and interact with it.
80+
81+
### Special variables
82+
83+
```bash
84+
# the process id of the current command
85+
echo "$$"
86+
87+
# the process id of the last command
88+
echo "$!"
89+
90+
# re-execute the last command
91+
!!
92+
```
93+
94+
## Managing processes
95+
96+
### Terminating processes
97+
98+
```bash
99+
# kill a process by its process id
100+
kill 1234
101+
102+
# kill all processes by command name
103+
killall firefox
104+
```
105+
106+
### Background jobs
107+
108+
Use the `&` operator at the end of a command to keep that command running in the background:
109+
110+
```bash
111+
# run a job in the background
112+
long-running-task &
113+
114+
# list background jobs
115+
jobs
116+
117+
# bring jobs to the foreground
118+
fg
119+
```
120+
121+
Use `CTRL+Z` to suspend a foreground command (pause its execution).
122+
123+
```bash
124+
# send the most recent suspended job the background
125+
bg
126+
127+
# kill the background job with id 1
128+
kill %1
129+
```
130+
131+
### Difference between jobs and processes
132+
133+
A process is any running program with its own address space, memory, and a unique process ID. It is a fundamental concept managed by the operating system.
134+
135+
In contrast, a job is a concept used by the shell, specifically referring to any program started interactively that does not detach from the terminal.

src/content/lessons/bash-scripting.md

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 'Scripting with bash'
33
description: 'Diving deeper into bash features, writing scripts'
44
order: 8
5-
state: 'draft'
5+
state: 'upcoming'
66
tags: ['bash', 'unix']
77
links: {
88
'shellcheck': 'https://github.com/koalaman/shellcheck',
@@ -80,7 +80,7 @@ such as JavaScript or Python
8080

8181
<bonus-content>
8282

83-
### A note on portablity
83+
### A note on portability
8484

8585
In the above example, we are assuming that the `/bin/bash` file exists -
8686
this might not be the case.
@@ -178,8 +178,7 @@ str1 > str2
178178
### Test: file properties
179179

180180
```bash
181-
-a file # file exists
182-
-e file # file exists; same -a
181+
-e file # file exists
183182
-d file # directory exists
184183
-f file # file exists and is a regular file
185184
# (not a directory or other special type of file)
@@ -193,6 +192,18 @@ file1 -ot file2 # file1 is older than file2
193192

194193
---
195194

195+
### Test: variables
196+
197+
```bash
198+
-v var # variable is set
199+
200+
-n $var # variable value is NOT empty
201+
202+
-z $var # variable is unset or value is empty
203+
```
204+
205+
---
206+
196207
### Test: logical operators
197208

198209
```bash
@@ -201,6 +212,9 @@ file1 -ot file2 # file1 is older than file2
201212

202213
# cond1 OR cond2
203214
[ condition1 -o condition2 ]
215+
216+
# NOT condition
217+
[ ! condition ]
204218
```
205219

206220
---
@@ -217,8 +231,18 @@ To use bash-specific features use double square brackets:
217231

218232
---
219233

220-
Some useful extensions are:
221-
<!-- TODO: write the list -->
234+
Some useful extensions only available in bash are:
235+
236+
```bash
237+
# using globs in tests
238+
[[ "hello.txt" == *.txt ]]
239+
240+
# use character classes in tests
241+
[[ "$input" == [0-9]* ]]
242+
243+
# use regular expressions in tests
244+
[[ "$version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]
245+
```
222246

223247
---
224248

@@ -258,6 +282,8 @@ fi
258282

259283
### case
260284

285+
Case patterns can contain globs like `*`, `?`, `[a-z]`:
286+
261287
```bash
262288
case expression in
263289
pattern1 )
@@ -271,6 +297,8 @@ esac
271297

272298
## Loops
273299

300+
Loops can use globs as well:
301+
274302
```bash
275303
for f in ~/mydir/*.txt; do
276304
echo $f
@@ -310,6 +338,8 @@ echo ${#myarray[@]}
310338

311339
## Handling inputs
312340

341+
There are special variable that allow you to handle script arguments.
342+
313343
```bash
314344
# print number of passed-in arguments
315345
echo "$#"
@@ -355,6 +385,8 @@ trap "rm -f $tempfile" EXIT
355385

356386
## Functions
357387

388+
Functions can be used to group logical units and repetitive actions.
389+
358390
```bash
359391
# declare a function
360392
my_func() { echo "Hello $1"; }
@@ -363,10 +395,17 @@ my_func() { echo "Hello $1"; }
363395
function myfunc() {
364396
echo "same as above"
365397
}
398+
399+
# calling a function is like calling a command
400+
my_func "friend"
366401
```
367402

403+
> In bash functions do NOT return values.
404+
368405
---
369406

407+
Use the `declare` built-in command to examine functions defined in your environment:
408+
370409
```bash
371410
# Show function body
372411
declare -f my_func
@@ -377,24 +416,38 @@ declare -F
377416

378417
---
379418

380-
<!-- TODO: explain difference to script arguments -->
419+
The meaning of certain special variable changes inside a function:
381420

382421
```bash
383-
# number of arguments
384-
myfunc() { echo $# }
422+
# number of arguments to the function
423+
myfunc() { echo "$#"; }`
385424
386-
# all arguments
425+
# all arguments to the function
387426
function myfunc() {
388-
echo $@
427+
echo "$@"
389428
}
390429
391-
# arguments by order
392-
myfunc() { echo $1 $2 $3 }
393-
394-
# calling a function with arguments
395-
myfunc hello world
430+
# function arguments by order
431+
myfunc() { echo "$1 $2 $3"; }
396432
```
397433

434+
<bonus-content>
435+
436+
<home-work>
437+
438+
### Script vs function arguments
439+
440+
- Create a script that takes 2 arguments
441+
- Print the string 'From script:'
442+
- Print the values of both arguments
443+
- Add a function definition to the script that also takes 2 arguments
444+
- The function should print 'From function:' and then print its arguments
445+
- Call the function inside the script passing in the script arguments **in reverse**
446+
447+
</home-work>
448+
449+
</bonus-content>
450+
398451
---
399452

400453
## Linting with shellcheck

0 commit comments

Comments
 (0)