# How to make a bash script wait till a pendrive is mounted?

I have a bash script which has a line cd /run/media/Username/121C-E137/ this script is triggered as soon as the pen-drive is recognized by the CPU but this line should be executed only after the mounting process is complete. As of now what happens is this line is executed before the pen-drive is mounted and returns an error the directory is invalid.

• Depends on your system. If you use systemd, you can write a udev rule using SYSTEMD_WANTS... It's documented in man systemd.device. – jasonwryan Apr 23 '14 at 8:14
• The simplest solution would be to let your script do the mounting. What is causing the mounting now? – Gilles Apr 23 '14 at 21:22

PENDRIVE='/run/media/Username/121C-E137'
while [ ! -d "$PENDRIVE" ]; do sleep 10 done cd$PENDRIVE