0

I want to create a bash script that will output the mount point from an inserted USB device. I have two commands (between "do" "and" done") that work separately but not together in a bash script. The script looks for a UUID file use the $UUID filename in the lsblk command to extract the mountpoint The mount point must be in a variable so that I can continue to use this in the same bash script. I've had this so far:

    #!/bin/bash 

EXCLUDE_DEVICE_1="5F92-0F71"
EXCLUDE_DEVICE_2="6fd9f710-f897-4b13-a521-70e184f669f3"

inotifywait -m --exclude "($EXCLUDE_DEVICE_1|$EXCLUDE_DEVICE_2)" -e create --format '%f' /dev/disk/by-uuid/ \
        | while read UUID
                do 
                        echo "new device found with uuid $UUID"
                        lsblk --noheadings --output MOUNTPOINT /dev/disk/by-uuid/$UUID
                done

The echo new device works and can see the $UUID but the lsblk command does nothing. When put the command lsblk --noheadings --output MOUNTPOINT /dev/disk/by-uuid/**realuuid** in the terminal it works. does anyone know what's wrong?

Coen17st
  • 147
  • 1
  • 9
  • Just a lame guess: Is `$UUID` already the full path? Can you post the output of `echo ...` and `printf %q lsblk ...` (mind the prepended `printf %q`)? – Socowi Jan 01 '21 at 19:58
  • No the $UUID is just the uuid of the device. this is the echo an printf %q output ```Setting up watches. Watches established. new device found with uuid 70DD-A531 lsblk--noheadings--outputMOUNTPOINT/dev/disk/by-uuid/70DD-A531``` – Coen17st Jan 02 '21 at 13:10
  • Hm, this is really strange. Could it be a timing issue? What happens if you put a `sleep 2` in front of `lsblk`? – Socowi Jan 02 '21 at 13:14
  • Hm you are right it is a timing issue with sleep 2 before lsblk it is working ```Setting up watches. Watches established. new device found with uuid 70DD-A531 /media/user/DRIVERS``` thanks for your help! – Coen17st Jan 02 '21 at 13:18
  • Glad I could help. You can try to bring that down by experimenting. `sleep` also accepts fractions of seconds, e.g. `sleep 0.1`. Feel free to write and accept your own answer to close this question. – Socowi Jan 02 '21 at 13:20

1 Answers1

0

The problem was a timing issue and can be solved by adding (for me) minimum sleep time of 0.2 sec between detecting and executing the command


EXCLUDE_DEVICE_1="5F92-0F71"
EXCLUDE_DEVICE_2="6fd9f710-f897-4b13-a521-70e184f669f3"
LOG_FILE=/home/user/Documents/log.txt

exec > >(tee -a $LOG_FILE)
exec 2>&1

inotifywait -m --exclude "($EXCLUDE_DEVICE_1|$EXCLUDE_DEVICE_2)" -e create --format '%f' /dev/disk/by-uuid/ \
        | while read UUID
                do 
                        echo "new device found with uuid: $UUID"
                        sleep 0.2
                        lsblk --noheadings --output MOUNTPOINT /dev/disk/by-uuid/$UUID
                done
Coen17st
  • 147
  • 1
  • 9
  • As an aside, [don't use upper case for your private variables.](/questions/673055/correct-bash-and-shell-script-variable-capitalization) – tripleee Jan 02 '21 at 13:52