Linux
HOGENT toegepaste informatica
Thomas Parmentier, Andy Van Maele, Bert Van Vreckem
2024-2025
Informatie uitwisselen tussen script en omgeving:
stdin
, stdout
,
stderr
$1
, $2
, enz.Een functie gedraagt zich als een commando!
functie_naam arg1 arg2 arg3
${1}
, ${2}
,
enz.return STATUS
ipv exit
Hoe kan je een functie een “waarde” laten teruggeven? Via
stdout
!
stdout
van command
wordt opgevangen en
opgeslagen in variabele ${output}
.
Wat is de uitvoer van dit script?
Wat is de uitvoer van dit script?
# Usage: copy_iso_to_usb ISO_FILE DEVICE
# Copy an ISO file to a USB device, showing progress with pv (pipe viewer)
# e.g. copy_iso_to_usb FedoraWorkstation.iso /dev/sdc
copy_iso_to_usb() {
local iso="${1}"
local destination="${2}"
local iso_size
iso_size=$(stat -c '%s' "${iso}")
printf "Copying %s (%'dB) to %s\n" \
"${iso}" "${iso_size}" "${destination}"
dd if="${iso}" \
| pv --size "${iso_size}" \
| sudo dd of="${destination}"
}
Zie Parameter Substitution in de Advanced Bash-Scripting Guide.
Verwijder patroon van begin/einde van een string:
PATROON
in globbing syntax!
(man 7 glob
)
\
op het einde van
een regel)Probeer dit:
$ sleep 60
Ctrl+Z
[1]+ Stopped sleep 60
$ bg
[1]+ sleep 60 &
$ find / -type f > all-files.txt 2>&1 &
[2] 4321
Ctrl+Z
zet de uitvoer van een proces stil (nog niet
afgesloten!)bg
start het proces terug op, maar in de
achtergrond&
op het einde van een regel start proces op de
achtergrond = combinatie van Ctrl+Z
en bg
Commando | Betekenis |
---|---|
jobs |
Lijst van achtergrondprocessen |
jobs -l |
Idem, toon ook Process ID (PID) |
fg NUM |
Breng proces op voorgrond |
bg NUM |
Herstart stilgelegd proces op achtergrond |
Probeer dit:
$ at now + 2 minutes
warning: commands will be executed using /bin/sh
at Mon Nov 15 15:47:00 2021
at> date > /tmp/date.txt
at> <Ctrl+D>
job 9 at Mon Nov 15 15:47:00 2021
$ watch cat /tmp/date.txt
at
zal binnen 2 minuten het opgegeven commando
uitvoerenwatch
herbekijken we elke 2s de inhoud van het
doelbestandNog at
voorbeelden:
at 3:03 AM
at midnight
at 1am tomorrow
at now + 3 weeks
-…Commando | Betekenis |
---|---|
at |
Voer commando’s uit op specifiek tijdstip |
atq |
Geeft lijst van geplande taken |
atrm NUM |
Verwijder taak met id NUM |
batch |
Voer taak uit wanneer systeem minder belast is |
/etc/crontab
crontab -l
crontab -e
Veld | Beschrijving | Waarden |
---|---|---|
MIN | Minuten | 0-59 |
HOUR | Uren | 0-23 |
DOM | Dag van de maand | 1-31 |
MON | Maand | 1-12 |
DOW | Dag van de week | 0-7 |
CMD | Commando |
Dag van de week: zo = 0/7, ma = 1, di = 2, …
# use /bin/sh to run commands, no matter what /etc/passwd says
SHELL=/bin/sh
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
# Set time zone
CRON_TZ=Japan
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"