Video: https://youtu.be/U2J9V9l8EjU
Written version: https://cloudexplainers.com/linux-bash-scripting-full-tutorial-for-beginners/
Everything below ran for real in the video. Run it in a throwaway machine or container, never on a computer you care about.
The machine the video used, as files you can build it from:
lab/Dockerfilelab/capture.pylab/home/readings/garden.csv- [
lab/home/readings/north field.csv](lab/home/readings/north field.csv) lab/home/readings/roof.csvlab/home/sensors.txtlab/home/style.csslab/s1/report.shlab/s10/report.shlab/s3/count.shlab/s3b/count.shlab/s4/count.shlab/s5/list.shlab/s5b/all.shlab/s6/report.shlab/s8a/backup.shlab/s8b/backup.shlab/s8c/backup.shlab/s8d/backup.shlab/s9/list.sh
Build and open it (Docker):
docker build -t lab lab
docker run -it --rm lab bash# ana@station:~$
./backup.sh
ls /home/ana/backups# ana@station:~$
cat report.sh
ls
./report.sh
ls -l report.sh
chmod +x report.sh
ls -l report.sh
./report.sh
report.sh
echo $PATH# ana@station:~$
sensor = roof
sensor=roof
echo "Sensor: $sensor"
today=$(date +%F)
echo "Report for $today"
count=$(wc -l < readings/roof.csv)
echo "$count readings, $((24 - count)) missing"
file="north field.csv"
wc -l readings/$file
wc -l "readings/$file"# ana@station:~$
cat count.sh
chmod +x count.sh
./count.sh roof
./count.sh
ls readings/roof.csv
echo $?
ls readings/cellar.csv
echo $?
head -n 6 count.sh
./count.sh
echo $?# ana@station:~$
[[ -f readings/roof.csv ]] && echo "the file is there"
[[ -f readings/cellar.csv ]] && echo "the file is there"
echo $?
name=""
[ $name = roof ] && echo yes
[[ $name = roof ]] && echo yes
tail -n 10 count.sh
./count.sh cellar
echo $?
./count.sh "north field"
echo $?# ana@station:~$
for f in readings/*.csv; do echo "$f"; done
cat list.sh
./list.sh
cat sensors.txt
cat all.sh
./all.sh# ana@station:~$
echo $((11.4 + 1))
sort -t, -k2 -n readings/roof.csv | tail -n 1
sort -t, -k2 -n readings/roof.csv | tail -n 1 | cut -d, -f2
head -n 14 report.sh
tail -n 3 report.sh
./report.sh# ana@station:~$
./all.sh > report.txt
cat report.txt
./all.sh > report.txt 2> errors.log
cat errors.log
./all.sh >> report.txt 2>> errors.log
wc -l report.txt errors.log
./count.sh cellar > /dev/null 2>&1
echo $?# ana@station:~$
cat backup.sh
ls -d /home/ana/*/
echo $?
ls *.gz
head -n 4 backup.sh
./backup.sh
echo $?
sed -n 3,5p backup.sh
./backup.sh
ls *.gz
sed -n 3p backup.sh
./backup.sh
echo $?
cat readings/cellar.csv | wc -l
echo $?
set -o pipefail
cat readings/cellar.csv | wc -l
echo $?# ana@station:~$
bash -x count.sh "north field"
shellcheck list.sh
cat list.sh
shellcheck list.sh
echo $?
./list.sh# ana@station:~$
head -n 11 report.sh
sed -n 13,26p report.sh
tail -n 14 report.sh
shellcheck report.sh
./report.sh
echo $?
ls /home/ana/backups
tar -tzf /home/ana/backups/readings-*.tar.gzFrom Cloud Explainers. Lab files are MIT licensed (see the repository's LICENSE).