[SOLVED] Refer to ANY USB flash drive in bash script

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

[SOLVED] Refer to ANY USB flash drive in bash script

Postby pmilne » Wed Nov 20, 2013 1:19 pm

We have a script to display slideshows in LibreOffice using Raspberry pi's running raspbian - the script refers to a specific file located on the SD card. It loads the file into LO and displays it. How would I refer to the file if it were located on ANY USB flash drive attached to the pi. There's lots of advice on referring to specific drives using UDEV rules, is there a way to detect and refer to any flash drive that was plugged in? I would know the filename and it's location on the drive but not the drive details in advance.

Here's the project https://github.com/milnepe/slideshow

Thanks in advance, Pete
Last edited by pmilne on Wed Nov 20, 2013 10:57 pm, edited 1 time in total.
User avatar
pmilne
 
Posts: 18
Joined: Fri Jul 22, 2005 8:03 pm

Postby nelz » Wed Nov 20, 2013 1:57 pm

udev rules can run a script, s you could have a scriot that is run when a device meets the basic requirements, USB and removable, that mounts each partition and checks for the specific file. Then it either passes the file to LO or unmounts the device.

Alternatively, if you hav control over the USB flash drives used, you can give a specific label to any filesystem containing the file you need and have udev check for that label.

Either way, be aware that udev blocks while it is running a script, so your script should detach.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8496
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK

Postby pmilne » Wed Nov 20, 2013 3:55 pm

Thanks for the sound advice nelz - I've been meaning to get to grips with udev rules for a while! I know you've done articles on this before which I find very useful to help me on the learning curve.

I was going to run a script which checks for the file at /mnt/flashdrive/slideshow and then loads that. The path being generated by the rule, rather than the udev rule running the script but that's got me thinking.
User avatar
pmilne
 
Posts: 18
Joined: Fri Jul 22, 2005 8:03 pm

Postby pmilne » Wed Nov 20, 2013 10:54 pm

I created the following udev rule which seems to work for my fat formatted sticks:
KERNEL=="sd*", ATTR{removable}=="1", DRIVERS=="usb-storage", SYMLINK+="usbstick"

And made an entry in fstab:
/dev/usbstick /media/usbstick vfat defaults 0 0

This will mount the usb stick on rebooting the pi so my script can check for the presence of a known filename in /media.usbstick/slideshow.odp and read the file.
User avatar
pmilne
 
Posts: 18
Joined: Fri Jul 22, 2005 8:03 pm

Postby nelz » Wed Nov 20, 2013 11:46 pm

That will work fine as long as you only ever connect one USB storage device at a time, and that has the file you need on the first partition, and that partition contains a FAT filesystem.
"Insanity: doing the same thing over and over again and expecting different results." (Albert Einstein)
User avatar
nelz
Site admin
 
Posts: 8496
Joined: Mon Apr 04, 2005 11:52 am
Location: Warrington, UK


Return to Programming

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest