public inbox for gentoo-user@lists.gentoo.org
 help / color / mirror / Atom feed
* [gentoo-user] Writing more complicated scripts than I'm used too.
@ 2025-06-14  3:44 Dale
  2025-06-14  8:42 ` Sascha Spreitzer
                   ` (4 more replies)
  0 siblings, 5 replies; 27+ messages in thread
From: Dale @ 2025-06-14  3:44 UTC (permalink / raw
  To: gentoo-user

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? 

Also, if something goes bad, how do I tell it to print out that
something went wrong and stop? 

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? 

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

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  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
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 27+ messages in thread
From: Sascha Spreitzer @ 2025-06-14  8:42 UTC (permalink / raw
  To: gentoo-user

Hi dale

In shell scripting nowadays we define and reuse functions. Sections are no longer possible.

I would recommend starting with beginners literature on «bash scripting» and to advance from there.
A quick search brought up: https://linuxhandbook.com/bash/

> On 14 Jun 2025, at 05:44, Dale <rdalek1967@gmail.com> wrote:
> 

[…]

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

[…]




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  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
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 27+ messages in thread
From: Bryan Gardiner @ 2025-06-14 17:18 UTC (permalink / raw
  To: gentoo-user

Hey Dale,

On Fri, 13 Jun 2025 22:44:05 -0500
Dale <rdalek1967@gmail.com> 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


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  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-09-18 15:58 ` Dale
  4 siblings, 0 replies; 27+ messages in thread
From: Frank Steinmetzger @ 2025-06-15 20:53 UTC (permalink / raw
  To: gentoo-user

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

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.

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

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-06-14  3:44 [gentoo-user] Writing more complicated scripts than I'm used too Dale
                   ` (2 preceding siblings ...)
  2025-06-15 20:53 ` Frank Steinmetzger
@ 2025-07-01 23:50 ` Dale
  2025-07-02  0:26   ` Frank Steinmetzger
  2025-07-02  1:10   ` [gentoo-user] " Eli Schwartz
  2025-09-18 15:58 ` Dale
  4 siblings, 2 replies; 27+ messages in thread
From: Dale @ 2025-07-01 23:50 UTC (permalink / raw
  To: gentoo-user

Dale 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. 
>
>
> <<<<SNIP>>>>
>
> 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
>
> :-)  :-) 
>


OK.  I got one wrote, sort of.  I'm trying to write these things so that
I can change the variables at the top to make them work on other drives
or drive sets.  Some I will add a section to do backups as well and
unmount and lock as well, maybe.  This one just unlocks a encrypted set
of drives and mounts it.  So far, when I run it, it tells me the drive
is open and mounted.  Since it is, that part works.  I haven't tested
the unlock and mount parts.  Given our power blinked some today during a
storm, I thought I was going to get a chance to test it.  o_O  The power
held up so no shutdown today. 

This is the script.  Tell me what I did wrong.  Please be kind.  This is
the first real script I've ever done. 


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

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
    exit 0
fi


It took me a while to figure out how important spaces are and where they
have to be for things to work around the ifs.  Put a space in the wrong
place, or leave one out where there should be one, it pukes on your
keyboard. 

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

For us older VHS types, be kind, rewind.  ROFL 

Dale

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-01 23:50 ` Dale
@ 2025-07-02  0:26   ` Frank Steinmetzger
  2025-07-02  0:47     ` Dale
  2025-07-02  1:10   ` [gentoo-user] " Eli Schwartz
  1 sibling, 1 reply; 27+ messages in thread
From: Frank Steinmetzger @ 2025-07-02  0:26 UTC (permalink / raw
  To: gentoo-user

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

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  0:26   ` Frank Steinmetzger
@ 2025-07-02  0:47     ` Dale
  2025-07-02  1:10       ` Eli Schwartz
  0 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-07-02  0:47 UTC (permalink / raw
  To: gentoo-user

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

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


[-- Attachment #2: Type: text/html, Size: 7413 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-01 23:50 ` Dale
  2025-07-02  0:26   ` Frank Steinmetzger
@ 2025-07-02  1:10   ` Eli Schwartz
  2025-07-02  1:33     ` Dale
  1 sibling, 1 reply; 27+ messages in thread
From: Eli Schwartz @ 2025-07-02  1:10 UTC (permalink / raw
  To: gentoo-user


[-- Attachment #1.1: Type: text/plain, Size: 946 bytes --]

On 7/1/25 7:50 PM, Dale wrote:
> It took me a while to figure out how important spaces are and where they
> have to be for things to work around the ifs.  Put a space in the wrong
> place, or leave one out where there should be one, it pukes on your
> keyboard. 


Spaces separate "words" in a command. And something that sometimes
surprises people is that in shell scripts, brackets, that is, [ and [[
are both "commands". They take a series of arguments.

For example,

if [ "$?" -eq "0" ] ; then

The "if" must be followed by a command to test its value. [ is a
command, and takes four arguments:

argument 1: $?
argument 2: -eq
argument 3: 0
argument 4: ]


The "]" argument tells it to stop reading arguments. :)

Using ; is equivalent to adding a newline, but prettier. It is legal to
do this:


if [ "$?" -eq "0" ];then

It just looks really ugly. :) :) :) Spaces help you read.



-- 
Eli Schwartz

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  0:47     ` Dale
@ 2025-07-02  1:10       ` Eli Schwartz
  2025-07-02  2:41         ` [gentoo-user] " Grant Edwards
  0 siblings, 1 reply; 27+ messages in thread
From: Eli Schwartz @ 2025-07-02  1:10 UTC (permalink / raw
  To: gentoo-user


[-- Attachment #1.1: Type: text/plain, Size: 1129 bytes --]

On 7/1/25 8:47 PM, Dale wrote:

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


"else" doesn't need to be on its own line but people almost universally
do so because it makes it easier to read.



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


This makes your eyes get drawn to the lines that are aligned left, which
have the "when shall I run it?" logic. You can easily tell that
"cryptsetup open" happens AFTER the "else".


-- 
Eli Schwartz

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  1:10   ` [gentoo-user] " Eli Schwartz
@ 2025-07-02  1:33     ` Dale
  2025-07-02  2:04       ` Javier Martinez
  0 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-07-02  1:33 UTC (permalink / raw
  To: gentoo-user

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

Eli Schwartz wrote:
> On 7/1/25 7:50 PM, Dale wrote:
>> It took me a while to figure out how important spaces are and where they
>> have to be for things to work around the ifs.  Put a space in the wrong
>> place, or leave one out where there should be one, it pukes on your
>> keyboard. 
> Spaces separate "words" in a command. And something that sometimes
> surprises people is that in shell scripts, brackets, that is, [ and [[
> are both "commands". They take a series of arguments.
>
> For example,
>
> if [ "$?" -eq "0" ] ; then
>
> The "if" must be followed by a command to test its value. [ is a
> command, and takes four arguments:
>
> argument 1: $?
> argument 2: -eq
> argument 3: 0
> argument 4: ]
>
>
> The "]" argument tells it to stop reading arguments. :)
>
> Using ; is equivalent to adding a newline, but prettier. It is legal to
> do this:
>
>
> if [ "$?" -eq "0" ];then
>
> It just looks really ugly. :) :) :) Spaces help you read.
>
>
>
> -- Eli Schwartz


Now I read somewhere that the [ and ] is a command but forgot that. 
That explains a lot.  I was looking at books and such and just sort of
did the same as they did for some of it.  The hard part was figuring out
if something was open or not and how to detect it and if something was
mounted or not.  Another thing I learned, what works on the command line
doesn't always work in a script.  Opposite is true too. 

I also moved the "else" over from your other post. 

Thanks. 

Dale

:-)  :-) 

P. S.  By the time I learn enough to be dangerous, I'll die of old age. 
:/ 

[-- Attachment #2: Type: text/html, Size: 2626 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  1:33     ` Dale
@ 2025-07-02  2:04       ` Javier Martinez
  2025-07-02  2:31         ` Dale
  0 siblings, 1 reply; 27+ messages in thread
From: Javier Martinez @ 2025-07-02  2:04 UTC (permalink / raw
  To: gentoo-user


[-- Attachment #1.1.1: Type: text/plain, Size: 2591 bytes --]


Software lies, /sys lies less (but rootkits could make it lie too).

Have you thought in parse /sys/block looking for dm* directories, and 
after that check if subdir slave is the one you want??

I want mean:

In /sys/block you have all block devices recognized in your system. You 
know that cryptsetup uses device mapper, so that if the volume is 
unlocked it has one dm device created (dm-0, dm-1)

you could check all dm devices there (they are "unlocked" even if they 
does not use dmcrypt...) and check if the 
/sys/block/dm-?/slaves/whateverpartition exists and whateverpartition is 
the one are you interested.

Probably your script will be a bit faster since you avoid exec commands, 
more trusted since you use kernel facility, and finally usually lsblk 
and friends (as ps or top with /proc) uses this information to their needs




El 2/7/25 a las 3:33, Dale escribió:
> Eli Schwartz wrote:
>> On 7/1/25 7:50 PM, Dale wrote:
>>> It took me a while to figure out how important spaces are and where they
>>> have to be for things to work around the ifs.  Put a space in the wrong
>>> place, or leave one out where there should be one, it pukes on your
>>> keyboard.
>> Spaces separate "words" in a command. And something that sometimes
>> surprises people is that in shell scripts, brackets, that is, [ and [[
>> are both "commands". They take a series of arguments.
>>
>> For example,
>>
>> if [ "$?" -eq "0" ] ; then
>>
>> The "if" must be followed by a command to test its value. [ is a
>> command, and takes four arguments:
>>
>> argument 1: $?
>> argument 2: -eq
>> argument 3: 0
>> argument 4: ]
>>
>>
>> The "]" argument tells it to stop reading arguments.:)
>>
>> Using ; is equivalent to adding a newline, but prettier. It is legal to
>> do this:
>>
>>
>> if [ "$?" -eq "0" ];then
>>
>> It just looks really ugly.:) :) :) Spaces help you read.
>>
>>
>>
>> -- Eli Schwartz
> 
> 
> Now I read somewhere that the [ and ] is a command but forgot that. That 
> explains a lot.  I was looking at books and such and just sort of did 
> the same as they did for some of it.  The hard part was figuring out if 
> something was open or not and how to detect it and if something was 
> mounted or not.  Another thing I learned, what works on the command line 
> doesn't always work in a script.  Opposite is true too.
> 
> I also moved the "else" over from your other post.
> 
> Thanks.
> 
> Dale
> 
> :-)  :-)
> 
> P. S.  By the time I learn enough to be dangerous, I'll die of old age.  :/


[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3145 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  2:04       ` Javier Martinez
@ 2025-07-02  2:31         ` Dale
  2025-07-02  2:44           ` Javier Juan Martinez Cabezon
  0 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-07-02  2:31 UTC (permalink / raw
  To: gentoo-user

Javier Martinez wrote:
>
> Software lies, /sys lies less (but rootkits could make it lie too).
>
> Have you thought in parse /sys/block looking for dm* directories, and
> after that check if subdir slave is the one you want??
>
> I want mean:
>
> In /sys/block you have all block devices recognized in your system.
> You know that cryptsetup uses device mapper, so that if the volume is
> unlocked it has one dm device created (dm-0, dm-1)
>
> you could check all dm devices there (they are "unlocked" even if they
> does not use dmcrypt...) and check if the
> /sys/block/dm-?/slaves/whateverpartition exists and whateverpartition
> is the one are you interested.
>
> Probably your script will be a bit faster since you avoid exec
> commands, more trusted since you use kernel facility, and finally
> usually lsblk and friends (as ps or top with /proc) uses this
> information to their needs
>
>


I tried to use commands that I use a lot already.  The mountpoint
command was a new one.  Someone posted it on this list somewhere. I just
had to get the commands to output less info instead of the usual bucket
full I like.  I have lsblk set to show a lot of info and that wouldn't
be good in this script.  I set it up as a alias.  I'm not sure if
scripts use the alias settings or not. 

I may rewrite a lot of this before it is over but so far, it works with
a open and mounted device at least.  If I could stop watching this TV
series, I could unmount, close and then test the other part of the
script.  I'm kinda hooked.  I like 'who done its'.  Anyway, I'm looking
into the [ and -f option.  That looks promising. 

Thanks to all for the tips so far.  I was expecting a lot more 'you did
this completely wrong' tho.  LOL 

Oh jeez!!  I just did a 'man ['.  Oh dear.  O_O 

Dale

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  2025-07-02  1:10       ` Eli Schwartz
@ 2025-07-02  2:41         ` Grant Edwards
  2025-07-02  3:18           ` Eli Schwartz
  0 siblings, 1 reply; 27+ messages in thread
From: Grant Edwards @ 2025-07-02  2:41 UTC (permalink / raw
  To: gentoo-user

On 2025-07-02, Eli Schwartz <eschwartz@gentoo.org> wrote:

> 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
>
> This makes your eyes get drawn to the lines that are aligned left, which
> have the "when shall I run it?" logic. You can easily tell that
> "cryptsetup open" happens AFTER the "else".

Maybe it's just me, but I've switched from using [ ] to [[ ]].  I find
it works right the first try way more often. It's a bash built-in, and
the quoting and whitespace handling seems to work more like my brain
expects.  After decades of using [ ], I was still regularly tripped up
in ways that I don't seem to be using [[ ]].  [And it's even support
by busybox's ash shell.]

https://mywiki.wooledge.org/BashFAQ/031

https://stackoverflow.com/questions/3427872/whats-the-difference-between-and-in-bash




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  2:31         ` Dale
@ 2025-07-02  2:44           ` Javier Juan Martinez Cabezon
  2025-07-02  4:07             ` Dale
  0 siblings, 1 reply; 27+ messages in thread
From: Javier Juan Martinez Cabezon @ 2025-07-02  2:44 UTC (permalink / raw
  To: gentoo-user


[-- Attachment #1.1.1: Type: text/plain, Size: 2184 bytes --]

El 2/7/25 a las 4:31, Dale escribió:
> Javier Martinez wrote:
>>
>> Software lies, /sys lies less (but rootkits could make it lie too).
>>
>> Have you thought in parse /sys/block looking for dm* directories, and
>> after that check if subdir slave is the one you want??
>>
>> I want mean:
>>
>> In /sys/block you have all block devices recognized in your system.
>> You know that cryptsetup uses device mapper, so that if the volume is
>> unlocked it has one dm device created (dm-0, dm-1)
>>
>> you could check all dm devices there (they are "unlocked" even if they
>> does not use dmcrypt...) and check if the
>> /sys/block/dm-?/slaves/whateverpartition exists and whateverpartition
>> is the one are you interested.
>>
>> Probably your script will be a bit faster since you avoid exec
>> commands, more trusted since you use kernel facility, and finally
>> usually lsblk and friends (as ps or top with /proc) uses this
>> information to their needs
>>
>>
> 
> 
> I tried to use commands that I use a lot already.  The mountpoint
> command was a new one.  Someone posted it on this list somewhere. I just
> had to get the commands to output less info instead of the usual bucket
> full I like.  I have lsblk set to show a lot of info and that wouldn't
> be good in this script.  I set it up as a alias.  I'm not sure if
> scripts use the alias settings or not.
> 
> I may rewrite a lot of this before it is over but so far, it works with
> a open and mounted device at least.  If I could stop watching this TV
> series, I could unmount, close and then test the other part of the
> script.  I'm kinda hooked.  I like 'who done its'.  Anyway, I'm looking
> into the [ and -f option.  That looks promising.
> 
> Thanks to all for the tips so far.  I was expecting a lot more 'you did
> this completely wrong' tho.  LOL
> 
> Oh jeez!!  I just did a 'man ['.  Oh dear.  O_O
> 
> Dale
> 
> :-)  :-)
> 
DEVICE=sda3
for dev in {0..15}; do
if [[ -d "/sys/block/dm-$dev/slaves/$DEVICE" ]]; then
echo "unlocked"
fi
done


This checks if sda3 is unlocked checking the first 16 device mapper 
devices (0,1....15)




[-- Attachment #1.1.2: OpenPGP public key --]
[-- Type: application/pgp-keys, Size: 3145 bytes --]

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 840 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  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 18:26             ` Grant Edwards
  0 siblings, 2 replies; 27+ messages in thread
From: Eli Schwartz @ 2025-07-02  3:18 UTC (permalink / raw
  To: gentoo-user


[-- Attachment #1.1: Type: text/plain, Size: 2300 bytes --]

On 7/1/25 10:41 PM, Grant Edwards wrote:
> On 2025-07-02, Eli Schwartz <eschwartz@gentoo.org> wrote:
> 
>> 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
>>
>> This makes your eyes get drawn to the lines that are aligned left, which
>> have the "when shall I run it?" logic. You can easily tell that
>> "cryptsetup open" happens AFTER the "else".
> 
> Maybe it's just me, but I've switched from using [ ] to [[ ]].  I find
> it works right the first try way more often. It's a bash built-in, and
> the quoting and whitespace handling seems to work more like my brain
> expects.  After decades of using [ ], I was still regularly tripped up
> in ways that I don't seem to be using [[ ]].


Yes, in general [[ is better than [

The practical difference is that it is parsed as a keyword, not as an
ordinary command, hence it knows what is inside of it without whitespace
handling. i.e.

[[ $FOO = bar ]]

$FOO does not need quotes because [[ runs *before* word splitting.

It also supports regex!


$ FOO="something"

$ [[ $FOO =~ ^s(.*)e(.*)n ]]

$ declare -p BASH_REMATCH
declare -a BASH_REMATCH=([0]="somethin" [1]="om" [2]="thi")


It's very powerful and purely additive over [

But it doesn't matter when pointing out how bash does whitespace
splitting of commands. :)


>  [And it's even support
> by busybox's ash shell.]



Beware that busybox ash supports [[ "partially".

From the busybox source code comments:



/* BASH_TEST2: [[ EXPR ]]
 * Status of [[ support:
 *   && and || work as they should
 *   = is glob match operator, not equality operator: STR = GLOB
 *   == same as =
 *   =~ is regex match operator: STR =~ REGEX
 * TODO:
 * singleword+noglob expansion:
 *   v='a b'; [[ $v = 'a b' ]]; echo 0:$?
 *   [[ /bin/n* ]]; echo 0:$?
 * quoting needs to be considered (-f is an operator, "-f" and ""-f are
not; etc)
 * ( ) < > should not have special meaning (IOW: should not require quoting)
 * in word = GLOB, quoting should be significant on char-by-char basis:
a*cd"*"
 */
#define    BASH_TEST2           (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST)



-- 
Eli Schwartz

[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 236 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-07-02  2:44           ` Javier Juan Martinez Cabezon
@ 2025-07-02  4:07             ` Dale
  0 siblings, 0 replies; 27+ messages in thread
From: Dale @ 2025-07-02  4:07 UTC (permalink / raw
  To: gentoo-user

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

Javier Juan Martinez Cabezon wrote:
> El 2/7/25 a las 4:31, Dale escribió:
>> Javier Martinez wrote:
>>>
>>> Software lies, /sys lies less (but rootkits could make it lie too).
>>>
>>> Have you thought in parse /sys/block looking for dm* directories, and
>>> after that check if subdir slave is the one you want??
>>>
>>> I want mean:
>>>
>>> In /sys/block you have all block devices recognized in your system.
>>> You know that cryptsetup uses device mapper, so that if the volume is
>>> unlocked it has one dm device created (dm-0, dm-1)
>>>
>>> you could check all dm devices there (they are "unlocked" even if they
>>> does not use dmcrypt...) and check if the
>>> /sys/block/dm-?/slaves/whateverpartition exists and whateverpartition
>>> is the one are you interested.
>>>
>>> Probably your script will be a bit faster since you avoid exec
>>> commands, more trusted since you use kernel facility, and finally
>>> usually lsblk and friends (as ps or top with /proc) uses this
>>> information to their needs
>>>
>>>
>>
>>
>> I tried to use commands that I use a lot already.  The mountpoint
>> command was a new one.  Someone posted it on this list somewhere. I just
>> had to get the commands to output less info instead of the usual bucket
>> full I like.  I have lsblk set to show a lot of info and that wouldn't
>> be good in this script.  I set it up as a alias.  I'm not sure if
>> scripts use the alias settings or not.
>>
>> I may rewrite a lot of this before it is over but so far, it works with
>> a open and mounted device at least.  If I could stop watching this TV
>> series, I could unmount, close and then test the other part of the
>> script.  I'm kinda hooked.  I like 'who done its'.  Anyway, I'm looking
>> into the [ and -f option.  That looks promising.
>>
>> Thanks to all for the tips so far.  I was expecting a lot more 'you did
>> this completely wrong' tho.  LOL
>>
>> Oh jeez!!  I just did a 'man ['.  Oh dear.  O_O
>>
>> Dale
>>
>> :-)  :-)
>>
> DEVICE=sda3
> for dev in {0..15}; do
> if [[ -d "/sys/block/dm-$dev/slaves/$DEVICE" ]]; then
> echo "unlocked"
> fi
> done
>
>
> This checks if sda3 is unlocked checking the first 16 device mapper
> devices (0,1....15)


Since most of my LVM drives are encrypted, to see if a encrypted drive
is open or not, I look for the device that is created when it is
unlocked.  The device sda3 for example may be available but it doesn't
mean it is open, just that the drive is connected.  I get the point
tho.  I could just make it check and see if the device that is created
when the drive/file system is unlocked exists or not.  Same thing just a
little more precise. 

As usual, there is a few ways to accomplish the same goal.  Yours may be
a little more precise if I see if what is created after the device is
open exists.  May look into that.  :-D

Dale

:-)  :-) 

[-- Attachment #2: Type: text/html, Size: 5150 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  2025-07-02  3:18           ` Eli Schwartz
@ 2025-07-02  4:22             ` Dale
  2025-07-02  5:21               ` Alexis
                                 ` (2 more replies)
  2025-07-02 18:26             ` Grant Edwards
  1 sibling, 3 replies; 27+ messages in thread
From: Dale @ 2025-07-02  4:22 UTC (permalink / raw
  To: gentoo-user

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

Eli Schwartz wrote:
> On 7/1/25 10:41 PM, Grant Edwards wrote:
>> On 2025-07-02, Eli Schwartz <eschwartz@gentoo.org> wrote:
>>
>>> 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
>>>
>>> This makes your eyes get drawn to the lines that are aligned left, which
>>> have the "when shall I run it?" logic. You can easily tell that
>>> "cryptsetup open" happens AFTER the "else".
>> Maybe it's just me, but I've switched from using [ ] to [[ ]].  I find
>> it works right the first try way more often. It's a bash built-in, and
>> the quoting and whitespace handling seems to work more like my brain
>> expects.  After decades of using [ ], I was still regularly tripped up
>> in ways that I don't seem to be using [[ ]].
> Yes, in general [[ is better than [
>
> The practical difference is that it is parsed as a keyword, not as an
> ordinary command, hence it knows what is inside of it without whitespace
> handling. i.e.
>
> [[ $FOO = bar ]]
>
> $FOO does not need quotes because [[ runs *before* word splitting.
>
> It also supports regex!
>
>
> $ FOO="something"
>
> $ [[ $FOO =~ ^s(.*)e(.*)n ]]
>
> $ declare -p BASH_REMATCH
> declare -a BASH_REMATCH=([0]="somethin" [1]="om" [2]="thi")
>
>
> It's very powerful and purely additive over [
>
> But it doesn't matter when pointing out how bash does whitespace
> splitting of commands. :)
>
>
>>  [And it's even support
>> by busybox's ash shell.]
>
> Beware that busybox ash supports [[ "partially".
>
> From the busybox source code comments:
>
>
>
> /* BASH_TEST2: [[ EXPR ]]
>  * Status of [[ support:
>  *   && and || work as they should
>  *   = is glob match operator, not equality operator: STR = GLOB
>  *   == same as =
>  *   =~ is regex match operator: STR =~ REGEX
>  * TODO:
>  * singleword+noglob expansion:
>  *   v='a b'; [[ $v = 'a b' ]]; echo 0:$?
>  *   [[ /bin/n* ]]; echo 0:$?
>  * quoting needs to be considered (-f is an operator, "-f" and ""-f are
> not; etc)
>  * ( ) < > should not have special meaning (IOW: should not require quoting)
>  * in word = GLOB, quoting should be significant on char-by-char basis:
> a*cd"*"
>  */
> #define    BASH_TEST2           (ENABLE_ASH_BASH_COMPAT * ENABLE_ASH_TEST)
>
>
>
> -- Eli Schwartz


Should I change mine from a single [ ] to a double [[ ]]?  For future
compatibility if nothing else? 

Dale

:-)  :-) 

[-- Attachment #2: Type: text/html, Size: 3759 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  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
  2 siblings, 0 replies; 27+ messages in thread
From: Alexis @ 2025-07-02  5:21 UTC (permalink / raw
  To: gentoo-user

Dale <rdalek1967@gmail.com> writes:

> Should I change mine from a single [ ] to a double [[ ]]?  For 
> future
> compatibility if nothing else? 

It depends what you mean by "compatibility". [[...]] is not POSIX, 
so is not supported by (for example) the dash shell. But if you're 
only ever going to be using this script with shells that support 
the '[[' construct, there's no reason to not use it.

('Compatibility' when it comes to shell scripting is a very messy 
topic. Over time i've gradually been documenting various 
differences between shells on the Gentoo wiki at 
https://wiki.gentoo.org/wiki/Shell/Scripting ).


Alexis.


^ permalink raw reply	[flat|nested] 27+ messages in thread

* [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  2025-07-02  3:18           ` Eli Schwartz
  2025-07-02  4:22             ` Dale
@ 2025-07-02 18:26             ` Grant Edwards
  1 sibling, 0 replies; 27+ messages in thread
From: Grant Edwards @ 2025-07-02 18:26 UTC (permalink / raw
  To: gentoo-user

On 2025-07-02, Eli Schwartz <eschwartz@gentoo.org> wrote:

> Yes, in general [[ is better than [
>
> The practical difference is that it is parsed as a keyword, not as an
> ordinary command, hence it knows what is inside of it without whitespace
> handling. i.e.

>>  [And it's even support
>> by busybox's ash shell.]
>
>
>
> Beware that busybox ash supports [[ "partially".
>
> From the busybox source code comments:

Ah, thanks for pointing that out.  I apparently haven't run into those
incompibilities yet, but I'll have to keep them in mind.  When writing
scripts of an embedded system, I tend to write/test scripts in bash,
then test them with ash on the target system.  What I _should_ do is
install busybox on my development host and do the development using
busybox ash as well.

--
Grant




^ permalink raw reply	[flat|nested] 27+ messages in thread

* [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  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
  2 siblings, 0 replies; 27+ messages in thread
From: Grant Edwards @ 2025-07-02 18:27 UTC (permalink / raw
  To: gentoo-user

On 2025-07-02, Dale <rdalek1967@gmail.com> wrote:

> Should I change mine from a single [ ] to a double [[ ]]? For future
> compatibility if nothing else?

The single-bracket version will be around forever. You only need to
switch if you want the easier (IMO) sematics/syntax.

--
Grant




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  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
  2 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-07-03 18:47 UTC (permalink / raw
  To: gentoo-user

Dale wrote:
>
> Should I change mine from a single [ ] to a double [[ ]]?  For future
> compatibility if nothing else? 
>
> Dale
>
> :-)  :-) 


Well, today is my usual go to town day.  It was a longer day today tho. 
I had a 6 month checkup with one Doctor and my regular weekly Doctor
visit with another one.  Plus I did my usual shopping for groceries
etc.  When I leave, I close my video player and lock the drives.  When I
return, I get to unlock them again. 

I ran the script to see if it would detect the drive is already open and
mounted or not but today, I got to test the part where it is not open or
mounted.  I ran the script, it asked for my password and unlocked like a
champ and then mounted my drives.  No boo boos or anything.  I'm still
tweaking a bit. 

I still may improve some things like Javier mentioned but at least for
now, it works.  It gives me a starting point for other scripts.  Then I
can add the backup section to the scripts that do backups, and then
unmount them as well when done.  :-D 

I may start getting lazy.  ROFL 

Dale

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  2025-07-03 18:47               ` Dale
@ 2025-07-03 21:56                 ` Arve Barsnes
  2025-07-04  0:19                   ` Dale
  0 siblings, 1 reply; 27+ messages in thread
From: Arve Barsnes @ 2025-07-03 21:56 UTC (permalink / raw
  To: gentoo-user

On Thu, 3 Jul 2025 at 20:48, Dale <rdalek1967@gmail.com> wrote:
> Well, today is my usual go to town day.  It was a longer day today tho.
> I had a 6 month checkup with one Doctor and my regular weekly Doctor
> visit with another one.  Plus I did my usual shopping for groceries
> etc.  When I leave, I close my video player and lock the drives.  When I
> return, I get to unlock them again.
>
> I ran the script to see if it would detect the drive is already open and
> mounted or not but today, I got to test the part where it is not open or
> mounted.  I ran the script, it asked for my password and unlocked like a
> champ and then mounted my drives.  No boo boos or anything.  I'm still
> tweaking a bit.
>
> I still may improve some things like Javier mentioned but at least for
> now, it works.  It gives me a starting point for other scripts.  Then I
> can add the backup section to the scripts that do backups, and then
> unmount them as well when done.  :-D
>
> I may start getting lazy.  ROFL

Congrats Dale! The satisfaction of a home-made script that does what
you want it to do is fantastic :)

Regards,
Arve


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Re: Writing more complicated scripts than I'm used too.
  2025-07-03 21:56                 ` Arve Barsnes
@ 2025-07-04  0:19                   ` Dale
  0 siblings, 0 replies; 27+ messages in thread
From: Dale @ 2025-07-04  0:19 UTC (permalink / raw
  To: gentoo-user

Arve Barsnes wrote:
> On Thu, 3 Jul 2025 at 20:48, Dale <rdalek1967@gmail.com> wrote:
>> Well, today is my usual go to town day.  It was a longer day today tho.
>> I had a 6 month checkup with one Doctor and my regular weekly Doctor
>> visit with another one.  Plus I did my usual shopping for groceries
>> etc.  When I leave, I close my video player and lock the drives.  When I
>> return, I get to unlock them again.
>>
>> I ran the script to see if it would detect the drive is already open and
>> mounted or not but today, I got to test the part where it is not open or
>> mounted.  I ran the script, it asked for my password and unlocked like a
>> champ and then mounted my drives.  No boo boos or anything.  I'm still
>> tweaking a bit.
>>
>> I still may improve some things like Javier mentioned but at least for
>> now, it works.  It gives me a starting point for other scripts.  Then I
>> can add the backup section to the scripts that do backups, and then
>> unmount them as well when done.  :-D
>>
>> I may start getting lazy.  ROFL
> Congrats Dale! The satisfaction of a home-made script that does what
> you want it to do is fantastic :)
>
> Regards,
> Arve


Now I'm working on a script to unmount and lock a drive or drive set.  I
got the part that tests to see if a file is open done.  I used lsof to
do that.  The exit code is a little backward for what I'm doing but I
got it figured out. 

Once I get that done, I just copy the script and change the variables
for other drives or drive sets.  Then for my backup drives, I add the
part to update the backups and the variables for that.  Then I can copy
those and change the variables for all my backup scripts.   

I think I'm turning into a monster.  ROFLMBO

Dale

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-06-14  3:44 [gentoo-user] Writing more complicated scripts than I'm used too Dale
                   ` (3 preceding siblings ...)
  2025-07-01 23:50 ` Dale
@ 2025-09-18 15:58 ` Dale
  2025-09-18 17:37   ` Michael
  4 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-09-18 15:58 UTC (permalink / raw
  To: gentoo-user

Dale wrote:
> Howdy,
>
> <<< SNIP >>>
>
> Dale
>
> :-)  :-) 
>


Well, my little scripts have been working pretty good but every once in
a while I run into a small problem.  I have in my script a section that
looks to see if any files are open before trying to unmount the file
system.  Sometimes I may have a video left open or a Konsole tab that I
have in the path of what I want to umount and close.  Instead of getting
the job only half done and errorring out, I check first and stop if
something is open.  My problem is this.  I use Dolphin as my file
manager.  I usually have it set to show thumbnails/previews.  Sometimes
I can see in a preview if I'm looking at the video I want or not.  As
most know, I have a rather large collection of videos.  Generating all
those thumbnails/previews is a task that takes a LOT of time.  When I
first mount the drive, it can take a hour or more sometimes.  Thing is,
if it is generating those previews when I want to unmount the file
system, it stops it from unmounting because kioworker is accessing files
to generate the previews. 

Is their a way to stop kioworker, or kill it completely, so that I can
unmount a file system?  I tried putting pkill right before the lsof and
unmount part of my script but when it kills kioworker, it just restarts,
very quickly I might add.  This morning when I ran into this, I just
went to Dolphin and turned off previews.  Thing is, I have a lot of tabs
open at times.  It takes a bit to turn all of them off.  I also have
more than one instance of Dolphin open as well. 

One of my scripts is below.  Be gentle, I'm not a scripting guru by any
means.  :-D 

Dale

:-)  :-) 


#!/bin/bash
RED='\033[0;31m'
NC='\033[0m' # No Color
###printf "I ${RED}love${NC} Stack Overflow\n"

#echo -e "${RED}Starting 8TB backup${NC}"
echo ""

# Define the mount point, LVM device and LVM name.
mount_point="/home/dale/Desktop/Crypt"
lvm_dev="/dev/vg.crypt/crypt"
lvm_name=crypt
#pkill kioworker
lsof $mount_point >/dev/null 2>&1
if [ "$?" -eq "0" ] ; then
    echo -e "$RED File system has open files $NC"
    exit
else umount $mount_point
    cryptsetup close $lvm_name
    cryptsetup status $lvm_name
    echo "crypt now closed"


fi




^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-09-18 15:58 ` Dale
@ 2025-09-18 17:37   ` Michael
  2025-09-18 21:51     ` Dale
  0 siblings, 1 reply; 27+ messages in thread
From: Michael @ 2025-09-18 17:37 UTC (permalink / raw
  To: gentoo-user

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

On Thursday, 18 September 2025 16:58:35 British Summer Time Dale wrote:
> Dale wrote:
> > Howdy,
> > 
> > <<< SNIP >>>
> > 
> > Dale
> > 
> > :-)  :-) 
> 
> Well, my little scripts have been working pretty good but every once in
> a while I run into a small problem.  I have in my script a section that
> looks to see if any files are open before trying to unmount the file
> system.  Sometimes I may have a video left open or a Konsole tab that I
> have in the path of what I want to umount and close.  Instead of getting
> the job only half done and errorring out, I check first and stop if
> something is open.  My problem is this.  I use Dolphin as my file
> manager.  I usually have it set to show thumbnails/previews.  Sometimes
> I can see in a preview if I'm looking at the video I want or not.  As
> most know, I have a rather large collection of videos.  Generating all
> those thumbnails/previews is a task that takes a LOT of time.  When I
> first mount the drive, it can take a hour or more sometimes.  Thing is,
> if it is generating those previews when I want to unmount the file
> system, it stops it from unmounting because kioworker is accessing files
> to generate the previews. 
> 
> Is their a way to stop kioworker, or kill it completely, so that I can
> unmount a file system?  I tried putting pkill right before the lsof and
> unmount part of my script but when it kills kioworker, it just restarts,
> very quickly I might add.  This morning when I ran into this, I just
> went to Dolphin and turned off previews.  Thing is, I have a lot of tabs
> open at times.  It takes a bit to turn all of them off.  I also have
> more than one instance of Dolphin open as well. 
> 
> One of my scripts is below.  Be gentle, I'm not a scripting guru by any
> means.  :-D 
> 
> Dale
> 
> :-)  :-) 
> 
> #!/bin/bash
> RED='\033[0;31m'
> NC='\033[0m' # No Color
> ###printf "I ${RED}love${NC} Stack Overflow\n"
> 
> #echo -e "${RED}Starting 8TB backup${NC}"
> echo ""
> 
> # Define the mount point, LVM device and LVM name.
> mount_point="/home/dale/Desktop/Crypt"
> lvm_dev="/dev/vg.crypt/crypt"
> lvm_name=crypt
> #pkill kioworker
> lsof $mount_point >/dev/null 2>&1
> if [ "$?" -eq "0" ] ; then
>     echo -e "$RED File system has open files $NC"
>     exit
> else umount $mount_point
>     cryptsetup close $lvm_name
>     cryptsetup status $lvm_name
>     echo "crypt now closed"
> 
> 
> fi

Did you try closing Dolphin?  I'm thinking if Dolphin is the process that 
spawns kio, you'll need to kill that instead to stop respawning.

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-09-18 17:37   ` Michael
@ 2025-09-18 21:51     ` Dale
  2025-09-19  8:33       ` Michael
  0 siblings, 1 reply; 27+ messages in thread
From: Dale @ 2025-09-18 21:51 UTC (permalink / raw
  To: gentoo-user

Michael wrote:
> On Thursday, 18 September 2025 16:58:35 British Summer Time Dale wrote:
>> Dale wrote:
>>> Howdy,
>>>
>>> <<< SNIP >>>
>>>
>>> Dale
>>>
>>> :-)  :-) 
>> Well, my little scripts have been working pretty good but every once in
>> a while I run into a small problem.  I have in my script a section that
>> looks to see if any files are open before trying to unmount the file
>> system.  Sometimes I may have a video left open or a Konsole tab that I
>> have in the path of what I want to umount and close.  Instead of getting
>> the job only half done and errorring out, I check first and stop if
>> something is open.  My problem is this.  I use Dolphin as my file
>> manager.  I usually have it set to show thumbnails/previews.  Sometimes
>> I can see in a preview if I'm looking at the video I want or not.  As
>> most know, I have a rather large collection of videos.  Generating all
>> those thumbnails/previews is a task that takes a LOT of time.  When I
>> first mount the drive, it can take a hour or more sometimes.  Thing is,
>> if it is generating those previews when I want to unmount the file
>> system, it stops it from unmounting because kioworker is accessing files
>> to generate the previews. 
>>
>> Is their a way to stop kioworker, or kill it completely, so that I can
>> unmount a file system?  I tried putting pkill right before the lsof and
>> unmount part of my script but when it kills kioworker, it just restarts,
>> very quickly I might add.  This morning when I ran into this, I just
>> went to Dolphin and turned off previews.  Thing is, I have a lot of tabs
>> open at times.  It takes a bit to turn all of them off.  I also have
>> more than one instance of Dolphin open as well. 
>>
>> One of my scripts is below.  Be gentle, I'm not a scripting guru by any
>> means.  :-D 
>>
>> Dale
>>
>> :-)  :-) 
>>
>> #!/bin/bash
>> RED='\033[0;31m'
>> NC='\033[0m' # No Color
>> ###printf "I ${RED}love${NC} Stack Overflow\n"
>>
>> #echo -e "${RED}Starting 8TB backup${NC}"
>> echo ""
>>
>> # Define the mount point, LVM device and LVM name.
>> mount_point="/home/dale/Desktop/Crypt"
>> lvm_dev="/dev/vg.crypt/crypt"
>> lvm_name=crypt
>> #pkill kioworker
>> lsof $mount_point >/dev/null 2>&1
>> if [ "$?" -eq "0" ] ; then
>>     echo -e "$RED File system has open files $NC"
>>     exit
>> else umount $mount_point
>>     cryptsetup close $lvm_name
>>     cryptsetup status $lvm_name
>>     echo "crypt now closed"
>>
>>
>> fi
> Did you try closing Dolphin?  I'm thinking if Dolphin is the process that 
> spawns kio, you'll need to kill that instead to stop respawning.


Well, I try to keep it open so that when I open and mount the encrypted
file system again, I can refresh and be back where I was.  Most of the
time when I get new videos, I have to process them by renaming the files
to make sure they in order and all that.  If I close Dolphin, I lose
that.  At times, I may have half a dozen tabs open that I need to
process.  I also use one, or two, tabs for what I'm currently watching. 

I usually have four instances of Dolphin open.  One for documents, one
for videos that are on one set of drives, one that is for videos in the
process of being downloaded and then one that has multiple tabs for
either what I'm watching, processing or otherwise ready for something. 
It's a odd system to describe but I've done it this way for years and it
works well.  Pretty efficient too.  As long as I don't have to close
Dolphin, everything gets taken care of.  I might add, I have to be at a
good spot where everything on the encrypted drives is processed before I
update my backups.  That way the backups just add new stuff. 

Closing Dolphin would be good if I didn't mind losing my spot.  I may
have to come up with a way to "store" or "save" my spot and then restore
when I remount the file system.  I'm not sure how or if that can be done
tho.  I looked under File and don't see a way to save or restore.  You
know, that would be nice to have tho.  Sort of a option like Firefox
when you have more than one profile.  You can select what you want to
restore from or start a new one.  I wonder if the devs would think that
was a good idea and add it? 

Right now, I'm lucky that when I logout and back in, it does restore my
previous tabs and such on all four instances.  Every once in a while it
resets and loses the info after a major update tho.  Then I have to go
looking through all directories, over 600 of them, to see if I have
files I haven't processed yet.  It takes a while.

I really wish the pkill kioworker would work, at least long enough for
it to unmount the file system.  I just can't think of a way to kill it
and not let it restart, very fast I might add. 

I hope I don't have to close Dolphin.  I also wish it managed previews
better, instead of regenerating them each time.  From the properties
info.   62,199 files, 1,315 sub-folders.  Try to keep up with all that
by just remembering.  Heck, I forget what I go to the kitchen for to
often.  ROFL 

Dale

:-)  :-) 


^ permalink raw reply	[flat|nested] 27+ messages in thread

* Re: [gentoo-user] Writing more complicated scripts than I'm used too.
  2025-09-18 21:51     ` Dale
@ 2025-09-19  8:33       ` Michael
  0 siblings, 0 replies; 27+ messages in thread
From: Michael @ 2025-09-19  8:33 UTC (permalink / raw
  To: gentoo-user

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

On Thursday, 18 September 2025 22:51:48 British Summer Time Dale wrote:

> Right now, I'm lucky that when I logout and back in, it does restore my
> previous tabs and such on all four instances.  Every once in a while it
> resets and loses the info after a major update tho.  Then I have to go
> looking through all directories, over 600 of them, to see if I have
> files I haven't processed yet.  It takes a while.

You could use find with xargs or similar to list any files changed recently, 
so you don't have to dig manually using the GUI.  There's also search/kfind > 
Properties tab where you can set the creation/modification time for file 
search. 


> I really wish the pkill kioworker would work, at least long enough for
> it to unmount the file system.  I just can't think of a way to kill it
> and not let it restart, very fast I might add. 

Run 'ps axf' to see if a kioworker is showing being spawn from some subprocess 
different to the main Dolphin, in which case kill that in hope it doesn't 
respawn.



[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

^ permalink raw reply	[flat|nested] 27+ messages in thread

end of thread, other threads:[~2025-09-19  8:44 UTC | newest]

Thread overview: 27+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox