Hey Dale,
On Fri, 13 Jun 2025 22:44:05 -0500
Dale <[email protected]> wrote:
> 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.
>
>
> 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.
> 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?
Shell doesn't have 'goto's, but there are many control structures
available instead, the most important being ifs, loops (while, for),
and functions to avoid repeating common code. So try to structure
what you want in terms of those.
In this case, what you wrote above already translates pretty much
exactly to what you would need in a script. Something like:
if <already open>; then # but replace "<already open>", see below
# ...print already open...
else
# ...unlock the partition, check for error...
fi
if mountpoint -q /mnt/foo; then
# ...print already mounted...
else
# ...mount, check for error...
fi
The 'if's already include skipping parts of the code you don't want to
run; they just also enforce that execution continues downward :).
> Also, if something goes bad, how do I tell it to print out that
> something went wrong and stop?
echo "Error: Couldn't do X."
exit 1
Every program (e.g. your script) exits with a numeric code 0-255 for
the calling program (e.g. the shell) to inspect. Convention is that 0
means success and any non-zero number indicates some type of error
(which is up to the program itself to decide, so you can pick
whatever). So "exit [some nonzero number]" is how you stop and
indicate failure. "exit 0" would indicate success.
This error code is also how you can check for failure of cryptsetup
and mount. If they return a zero code, great! If not, then we should
abort.
There are a few ways to write this. After each command, the shell
sets a special variable "$?" to the command's exit code. So we could
be explicit about it:
cryptsetup open ...
if [[ $? -ne 0 ]]; then
echo "Error: Couldn't unlock disk."
exit 1
fi
However, we can also write this more directly, because what 'if'
*does* is check whether the command after the 'if' returns zero or not
(and the '!' inverts the check to mean 'run if the command failed',
rather than 'run if the command succeeded'):
if ! cryptsetup open ...; then
echo "Error: Couldn't unlock disk."
exit 1
fi
You can repeat the "is it open" or "is it mounted" checks after the
calls to cryptsetup or mount if you like, but typically one would just
rely on the exit status.
> 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?
cryptsetup creates a symlink under /dev/mapper with the target name of
the device you opened, so you could check for existence of that device
(e.g. /dev/mapper/sda3_crypt or whatever it's called for you).
if [[ -e /dev/mapper/sda3_crypt ]]; then
> Maybe this will get me started. Then I get to write a lot of scripts
> for each drive or drive set. o_O
>
> Thanks.
>
> Dale
>
> :-) :-)
Hope this helps,
Bryan