Page 2 of 3 FirstFirst 123 LastLast
Results 11 to 20 of 25

Thread: Detecting mount containing running script

  1. #11
    Join Date
    Aug 2006
    Location
    White Rock, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 13.10 Saucy Salamander

    Thumbs up Re: Detecting mount containing running script

    Quote Originally Posted by schragge View Post
    +1 to what slavic and ofnuts said. Slavik's adivice was well-minded, but it might be misleading if you don't have package manpages-posix installed. The manual page he meant is exec(1posix).
    Hey schragge. Thanks for the heads up. I was actually missing that package.

    Quote Originally Posted by schragge View Post
    Since exec is impemented as bash builtin, you may also want to look it up in the bash info manual, or in the ABS.
    Got it, thanks =)

    Quote Originally Posted by schragge View Post
    Concerning your code, I'd probably rewrite this
    Code:
    AUTORUN_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
    as
    Code:
    AUTORUN_DIR=$(readlink -f "$0")
    Strictly speaking, even this is not needed as df is able to walk through symlinks, so
    Code:
    AUTORUN_DIR=$0
    should be enough.
    The problem with that is I need just the directory and not the directory and the file name. This could also work, I reckon:
    Code:
    AUTORUN_DIR="$(dirname -- "$(readlink -f -- "$0")")"
    Quote Originally Posted by schragge View Post
    BTW, better not name your script variables in all-caps as it may interfere with environment variables.
    Great piece of advice. Thanks a lot.

    Quote Originally Posted by schragge View Post
    Also, if the only purpose of finding the mount point is to check whether it's a removable media then better approach would be directly evaluating device name:
    Code:
    device=$(df "$0"|awk 'NR>1{print $1}')
    removable=$(lsblk -r -n -o RM $device)
    if ((removable))
    then echo Running off of removable media
    else echo Running off of fixed drive
    fi
    Just for fun, doing the above snippet in one pass:
    Code:
    ((`lsblk -rnorm $(df "$0"|sed -n '2s/ .*//p')`))&&media_type=removable||media_type=fixed
    echo Running off of $media_type media
    It's a good solution but one problem is if the filesystem is encrypted, say, e.g. /home/me/.Private, then that will show up as its device name even though it's not actually it. When you run lsblk on it, it will choke.

    It would be great if there was a way to call a whole bash subroutine through exec. Is there a way to do that? Then I could put all the ejection code in one function and call that via exec which would be a lot cleaner.

  2. #12
    Join Date
    Aug 2011
    Location
    47°9′S 126°43W
    Beans
    1,690
    Distro
    Kubuntu 12.10 Quantal Quetzal

    Re: Detecting mount containing running script

    Quote Originally Posted by kiplingw View Post
    It would be great if there was a way to call a whole bash subroutine through exec. Is there a way to do that? Then I could put all the ejection code in one function and call that via exec which would be a lot cleaner.
    If you call exec over a subroutine of your script, you defeat its very purpose. The important point here is that eject should somehow be called from outside your script (which is what 'exec' does) so that the file containing the script is closed when "eject' is executed.

    But nothing forbids your to have all that code in a funciton that retrieves the device/fileystem and calls 'exec'.

  3. #13
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Detecting mount containing running script

    Quote Originally Posted by kiplingw View Post
    It's a good solution but one problem is if the filesystem is encrypted, say, e.g. /home/me/.Private, then that will show up as its device name even though it's not actually it. When you run lsblk on it, it will choke.
    I have no experience with encrypted filesystems, but I guess that one additional step may help here: find the real device name with findmnt:
    Code:
    mountpoint=$(df "$0"|awk 'NR>1{print $NF}')
    device=$(findmnt -c -n -o SOURCE $mountpoint|cut -d[ -f1)
    removable=$(lsblk -r -n -o RM $device)
    ((removable)) && exec eject $device
    If you show how an encrypted filesystem shows up in the output of the following commands
    Code:
    findmnt -l -otarget,source,label,uuid
    findmnt -sl -otarget,source,label,uuid
    findmnt -ml -otarget,source,label,uuid
    sudo blkid -o list -c /dev/null
    then I believe we could come up with a way to extract the device name from one of them.

  4. #14
    Join Date
    Aug 2006
    Location
    White Rock, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 13.10 Saucy Salamander

    Lightbulb Re: Detecting mount containing running script

    Quote Originally Posted by ofnuts View Post
    If you call exec over a subroutine of your script, you defeat its very purpose. The important point here is that eject should somehow be called from outside your script (which is what 'exec' does) so that the file containing the script is closed when "eject' is executed.

    But nothing forbids your to have all that code in a funciton that retrieves the device/fileystem and calls 'exec'.
    I guess what I'm confused about is how exec works. As I understand it, it takes over execution of the current process with nothing further of the original script being executed after it. The reason for that is the file handle to the script will be closed immediately before the eject command is executed next. Is that correct?

  5. #15
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Detecting mount containing running script

    Exactly.

  6. #16
    Join Date
    Aug 2006
    Location
    White Rock, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 13.10 Saucy Salamander

    Re: Detecting mount containing running script

    Quote Originally Posted by schragge View Post
    Exactly.
    It would be nice if the script were not locking the device and control could return to the script after the eject command. But I guess that is not so straightforward.

  7. #17
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Detecting mount containing running script

    Well, you probably could use at for this:
    Code:
    echo Scheduling eject to be run in one minute
    echo eject $device|at now + 1 minute
    echo Continue script execution

  8. #18
    Join Date
    Aug 2006
    Location
    White Rock, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 13.10 Saucy Salamander

    Re: Detecting mount containing running script

    That's one approach, but I think a problem with it is it won't be very elegant if, say, the script doesn't need to wait that long, or the script ends up taking a little longer than that.

  9. #19
    Join Date
    Feb 2013
    Beans
    Hidden!

    Re: Detecting mount containing running script

    Sure, it is very inelegant. I'm just wondering why would you want control to be returned to the script after eject? To check whether eject failed? Then do it like this
    Code:
    echo Running eject
    if eject -n $device 2>/dev/null
    then exec eject $device
    else echo eject failed
    fi
    Last edited by schragge; April 7th, 2013 at 12:04 AM.

  10. #20
    Join Date
    Aug 2006
    Location
    White Rock, Canada
    Beans
    Hidden!
    Distro
    Xubuntu 13.10 Saucy Salamander

    Lightbulb Re: Detecting mount containing running script

    Quote Originally Posted by schragge View Post
    Sure, it is very inelegant. I'm just wondering why would you want control to be returned to the script after eject? To check whether eject failed? Then do it like this
    Code:
    echo Running eject
    if eject -n $device 2>/dev/null
    then exec eject $device
    else echo eject failed
    fi
    Hey Schragge. I had thought about that already and isn't the problem that control never "returns" from the conditional's evaluation in the then since the exec it calls replaces the process space?

Page 2 of 3 FirstFirst 123 LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •