public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
From: Frank Steinmetzger <Warp_7@gmx.de>
To: gentoo-user@lists.gentoo.org
Subject: Re: [gentoo-user] Writing more complicated scripts than I'm used too.
Date: Wed, 2 Jul 2025 02:26:29 +0200	[thread overview]
Message-ID: <aGR8tR-OBVR4zVtT@q> (raw)
In-Reply-To: <c42d73bb-61ee-bb79-d11a-d3b9f44a0767@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 2160 bytes --]

Am Tue, Jul 01, 2025 at 06:50:40PM -0500 schrieb Dale:

> OK.  I got one wrote, sort of.
> […]
> This is the script.  Tell me what I did wrong.  Please be kind.  This is
> the first real script I've ever done. 

I’ll share some of my bash wisdom. Some may be nitpicks, others a matter of 
personal taste.

> #/bin/bash
> 
> 
> # Define the mount point
> MOUNT_POINT="/home/dale/Desktop/Crypt"
> LVM_DEV="/dev/vg.crypt/crypt"
> 
> lsblk -o NAME -o LABEL | grep ^crypt-open > /dev/null
-------------------------------------------^^^^^^^^^^^^
grep has a -q option to suppress output, no redirection necessary.

> 
> if [ "$?" -eq "0" ] ; then
The `if $?` part is actually kinda nicely readable, but you could of course 
also do a direct `if lsblk | grep; then`.

>     echo "Crypt file system is open"
> 
>     else echo "Crypt is not open.  Please enter passphrase."
------^^^^^^^^^
Lol, it never occured to me that `else` may be followed by a command without 
semicolon, because I’ve always written it on its own line. It is however 
common practice to put the else on the same level of indentation as its 
respective `if` and `fi`.

>     cryptsetup open $LVM_DEV crypt
> fi


> 
> # Check if the disk is already mounted
> if mountpoint -q "$MOUNT_POINT"; then
>     echo "The disk is already mounted at $MOUNT_POINT."
>     # mount if not mounted
>     else mount $MOUNT_POINT
>     exit 0
> fi

Same thing here with the `else` indentation. It is not visible on first 
sight that there is an if/else/fi structure here. And it’s not immediately 
clear that the exit also belongs into the else block.


> This is simple still but hey, I'm new at this.  It does work on one part
> of it at least.  ROFL 

Tool tip: shellcheck. It will give all sorts of feedback on common pitfalls 
and give suggestions on improvements. For instance, it showed me something I 
overlooked (you wrote #/bin/bash instead of #!/bin/bash).

-- 
Grüße | Greetings | Salut | Qapla’
Please do not share anything from, with or about me on any social network.

Electronically yours

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2025-07-02  0:27 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-14  3:44 [gentoo-user] Writing more complicated scripts than I'm used too Dale
2025-06-14  8:42 ` Sascha Spreitzer
2025-06-14 17:18 ` Bryan Gardiner
2025-06-15 20:53 ` Frank Steinmetzger
2025-07-01 23:50 ` Dale
2025-07-02  0:26   ` Frank Steinmetzger [this message]
2025-07-02  0:47     ` Dale
2025-07-02  1:10       ` Eli Schwartz
2025-07-02  2:41         ` [gentoo-user] " Grant Edwards
2025-07-02  3:18           ` Eli Schwartz
2025-07-02  4:22             ` Dale
2025-07-02  5:21               ` Alexis
2025-07-02 18:27               ` Grant Edwards
2025-07-03 18:47               ` Dale
2025-07-03 21:56                 ` Arve Barsnes
2025-07-04  0:19                   ` Dale
2025-07-02 18:26             ` Grant Edwards
2025-07-02  1:10   ` [gentoo-user] " Eli Schwartz
2025-07-02  1:33     ` Dale
2025-07-02  2:04       ` Javier Martinez
2025-07-02  2:31         ` Dale
2025-07-02  2:44           ` Javier Juan Martinez Cabezon
2025-07-02  4:07             ` Dale
2025-09-18 15:58 ` Dale
2025-09-18 17:37   ` Michael
2025-09-18 21:51     ` Dale
2025-09-19  8:33       ` Michael

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=aGR8tR-OBVR4zVtT@q \
    --to=warp_7@gmx.de \
    --cc=gentoo-user@lists.gentoo.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox