Frank Steinmetzger wrote:
> 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.

I forgot about that.  I added the -q option and removed the null bit. 


>> 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`.

I couldn't get lsblk to behave like I wanted.  I tried it but it never
worked right. 


>>     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`.

Well, this part of the script may not have been executed yet.  Keep in
mind, the device is open and mounted so it only runs the first bit, I
guess it ignores the rest.  I may be wrong tho.  Does it need to be on
its own line?  I'm learning, trying to anyway.  May as well learn it
right.  LOL 


>>     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.

I was actually wondering about the exit bit.  I'm concerned when I test
it on a unopen and unmounted device, it may fail. 


>
>> 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


Actually, I did a copy and paste.  I typed that wrong.  Hmmmm, I think I
copied that from one of my not so much a script scripts.  I may need to
check around.  That may be wrong in a few places.  While at it.  Given
it has a # in front, why is it not seen as a comment? 

I been reading books and some stuff others linked too.  It got me this
far at least.  I'm thinking about buying Linux Command Line and Shell
Scripting Bible as well.  It is a large book.  There is a new version
not yet in print but listed.  Costs more but figure it will be more up
to date with new stuff.  Both the new and old are large books.  Well
over 600 pages.  That should confuse me really well.  o_O 

New script below. 

Dale

:-)  :-) 



#!/bin/bash


# Define the mount point
MOUNT_POINT="/home/dale/Desktop/Crypt"
LVM_DEV="/dev/vg.crypt/crypt"

lsblk -o NAME -o LABEL | grep -q ^crypt-open

if [ "$?" -eq "0" ] ; then
    echo "Crypt file system is open"

    else echo "Crypt is not open.  Please enter passphrase."
    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
fi

Reply via email to