What's new

Custom script to determine if a USB HDD is mounted or not

  • SNBForums Code of Conduct

    SNBForums is a community for everyone, no matter what their level of experience.

    Please be tolerant and patient of others, especially newcomers. We are all here to share and learn!

    The rules are simple: Be patient, be nice, be helpful or be gone!

algorab

Occasional Visitor
Hello,

This is a kind request addressed to those who are good with linux shell scripting. Unfortunately, I am not good with this, so I need help.

I want to create a custom script which will do the following:

- mount > mount.txt
- blkid > blkid.txt
- read the content of the mount.txt and isolate all those lines which show mounts for /dev/sd* (sda1, sda2, sdb1, sdb2, and so on)
- read the content of the blkid.txt file and match the elements from mount.txt to the content of the blkid.txt in order to retrieve the UUID
- read the /etc/fstab and match the UUIDs retrieved in the previous step with the declared mount points (identified by UUID) and determine if what is in fstab is actually mounted.

Based on the above, the script will continue with a decision point.

If all declared mount points are actualy mounted, then the script will exit.

If, however, the mount configuration is not the expected one, then the script will perform USB unbind and then USB bind to force the router to re-read all attached USB devices.

Then, it will execute mount -a and verify again if the mount points are the ones from the fstab file.

The USB unbind / bind I already figured out. My problem is that I suppose I will have to make use of awk and / or sed in order to process the 3 files and match the data, and I do not know how to do it.

So, if anyone can lend a hand with this, I will very much appreciate it.

Thank you.

Cheers,
algorab
 
Last edited:
OK Here's the basics of something to get you started :)
Code:
#!/bin/sh

grep ^UUID /etc/fstab | awk '{ print $2 }' > /tmp/fstab.txt
mount | grep ^\/dev\/sd | awk '{ print $3 }' > /tmp/mtab.txt

grep -Fxvq -f /tmp/mtab.txt /tmp/fstab.txt
if [ $? -eq 0 ]
then
    echo "Error"
fi
 
Last edited:
Well, I've had it! There is no way the router can provide a stable environment when a USB drive is attached to it. And I have given up on trying to figure out a solution to make it work.

So, from this point of view, I could say that I had unrealistic expectations but also that the usefulness of the USB ports is asymptotically approaching 0.

Consequently, the existing drive is now attached to the media player and I will acquire a NAS device and call it quits.
 
Similar threads
Thread starter Title Forum Replies Date
C Script to notify unknown device connects to LAN ASUS Wi-Fi 3

Similar threads

Latest threads

Support SNBForums w/ Amazon

If you'd like to support SNBForums, just use this link and buy anything on Amazon. Thanks!

Sign Up For SNBForums Daily Digest

Get an update of what's new every day delivered to your mailbox. Sign up here!

Staff online

Top