Am Fri, Jun 13, 2025 at 10:44:05PM -0500 schrieb Dale: > Howdy, > > As most know, I ordered some books about writing scripts. A couple > things are not making sense to me. Yet. This is the basics of a script > I want to write.
If you have a script with many sections that depend on one another, you can write it as one long script, or to make it more digestible when you read it a few years later, put blocks of code into functions and then call those functions from the main scope, like so: check_prereqs() { ... } do_stuff() { ... } cleanup() { ... } # main scope: call the above functions check_prereqs do_stuff cleanup To get flow control, it is common to have early return on error. That means: as soon as you encounter a problem that your script cannot handle, have the script exit. This is preferable over nested if-else-structures. You can take it to the extrem with the statement `set -e`. This causes the script to terminate as soon as any of its commands returns != 0 unhandled. That means if you want the script to continue, you need to “catch” any possible error case yourself with `if` or a || construct, as in: mountpoint /some/path || echo "/some/path is not a mountpoint." The disadvantage: the script becomes more verbose. The advantage: you might discover bugs that you never thought of and you make the script safer. > Section one. See if a encrypted partition is open or not. I use > cryptsetup to open/close. > If open, print that it is open then go to mount part of script in > section two. Pseudo code for the first section: if check_container_is_open then print "container is open" else open_container if not check_container_is_open then print "Failed to open container." exit fi print "container is now open" fi Note that I did not put the last print into an else. Because if the script encountered the problem case, it would have exited anyways. > If not open, ask for pass phrase and confirm it is open. Then go to > section two. > Section one done, or fi. > > Section two. Is unencrypted file system mounted. > If mounted, print that it is mounted and exit. > If not mounted, mount and confirm mount and then exit. If mount fails, > print error. > Section two done, or fi. > > Here's the thing I can't figure out. Let's say the I need section one > to go to section two at a certain point, maybe because the encrypted > file system is already open as shown above, how do I tell it to skip to > section two? As others mentioned, put the sections into functions, call the functions in sequence and let the functions return early if necessary. > Also, if something goes bad, how do I tell it to print out that > something went wrong and stop? I tend to define a function called die in all my scripts that require an early exit on error: die() { print "$@" > /dev/stderr exit 1 } Then, instead of a print, you simply put this into the script: die "LUKS container could not be opened." > The book I'm reading is Linux command: Advanced methods and strategies > to shell scripting by William Vance. I don't know who the guy is but it > is the one with the newest copyright date. 2020. It's basically the > same info as all the other books which still has me clueless on the > above info. In the old Basic days with Vic-20 puters, we had goto to > move from one part of a program to another. No idea on bash tho. > > Also, someone posted about mountpoint -q. That will help with section > two. Is there a way to find out if a file system is open using > cryptsetup or some other command that I can put in a script? lsblk comes to mind. I guess it just reads internal data from /sys or /dev and puts it into a nice readable form. frank@q ~ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS loop0 7:0 0 102,9M 1 loop /mnt/data/audio/sid/hvsc/C64Music nvme0n1 259:0 0 1,8T 0 disk ├─nvme0n1p1 259:1 0 300M 0 part /boot ├─nvme0n1p2 259:2 0 16M 0 part ├─nvme0n1p3 259:3 0 195,2G 0 part /mnt/windows └─nvme0n1p4 259:4 0 1,6T 0 part └─root 253:0 0 1,6T 0 crypt ├─vg-root 253:1 0 50G 0 lvm / ├─vg-home 253:2 0 200G 0 lvm /home ├─vg-gentoo 253:3 0 50G 0 lvm /mnt/gentoo └─vg-data 253:4 0 1,3T 0 lvm /mnt/data To make parsing easier, you can define the colums to display yourself. Then only output type and mointpoints, grep for lines beginning with crypt (to not catch lines that have “crypt” in the mountpoint) and lastly grep for the mountpoint, if needed: lsblk --option-for-your-columns | grep ^crypt | grep "$YOUR_MOUNTPOINT" -- Grüße | Greetings | Salut | Qapla’ Please do not share anything from, with or about me on any social network. If I wanted to sit in a Mercedes Benz, I’d call for a taxi.
signature.asc
Description: PGP signature