0

I am not a coder or a programmer, I just try to learn but I have some limitations...

I'd like to mount a webdav resource when I logging into the session (virtual console), however I'd like to check if the folder is empty to determine if the resource must be mounted or not.

This is the script that I tried to figure out:

if status is-interactive
    # Commands to run in interactive sessions can go here
    #
    #
    # Start WebDav
    #
    # check if the folder is empty
    function tst
         command find /mnt/drive -maxdepth 0 -empty
    end
    
    # Compare the variable and mount the resource
    function drive
        set mnt '/mnt/drive'
        if test (tst) = $mnt
            command rclone mount drive:/ /mnt/drive/ --vfs-cache-mode writes --daemon
            else 
            # Actually I would use a "do nothing"
            echo "Resource already mounted"
        end
    end
     
    drive &
    # end WebDav   
end

Unfortunately it doesn't work, the login session get stuck hence I have to open another virtual console getting this error:

= /mnt/drive
             ^
~/.config/fish/config.fish (line 12): 
        if test (tst) = $mnt
           ^
in function 'drive'
    called on line 19 of file ~/.config/fish/config.fish
from sourcing file ~/.config/fish/config.fish
    called during startup
Resource already mounted

I don't understand what I am doing wrong... 😩

Now it works thanks!!!

if status is-interactive
    # Commands to run in interactive sessions can go here
    #
    
    # Start WebDav
    function drive
        set tst (find /mnt/drive -maxdepth 0 -empty)
        set mnt '/mnt/drive'
        if test "$tst" = "$mnt"
            command rclone mount drive:/ /mnt/drive/ --vfs-cache-mode writes --daemon &
        else 
            true
        end
    end
    
    drive & 
    # end WebDav   
end
1
  • in Stack Exchange, the right place to put the solution is in an Answer -- if the existing one solves it, you can accept it; or if there's something else that had to happen, you can self-answer. Thank you!
    – Jeff Schaller
    Oct 1, 2021 at 15:27

1 Answer 1

2

It looks to me like you're bashing into problems with test, which is one of the worst pieces of POSIX command-line scripts and one that fish (for better or worse) copies.

In particular, if test (tst) = $mnt will not behave as you expect if the output of tst is empty.

Instead, use:

set mnt '/mnt/drive'
set tst (find /mnt/drive -maxdepth 0 -empty)
if "$tst" = "$mnt"

The hang in the login session suggests to me that rclone isn't going into the background properly, or that logging in takes a long time. You could put it into the background with rclone ... &, but you probably want to disown it as well - otherwise when you exit the first session the mount will be killed.

However - the shell is probably the wrong layer to be doing this. If you start multiple shells in quick succession you may run into race conditions or other unexpected behaviour. Depending on your operating system, a systemd user service sounds like the most sensible thing to me. There is a systemd machine service configuration at https://www.jamescoyle.net/how-to/3116-rclone-systemd-startup-mount-script which might provide a good base for a user service.

1
  • Thank you very much, eventually reading again the documentation I figured out the same fixing. About your suggestion regarding rclone maybe I could use rc.local since I am using FreeBSD.
    – TGL
    Oct 1, 2021 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.