2

I wrote a script which formats USB pendrive and place media on it. It helps people in a library getting a bunch of HTML and media on it to browse historical data (official version).

All my script do the job well (formatting, mounting, copying, unmounting, say "goodbye"). But one point: how to know which device should I browse.

One only condition: there is still MAXimum 1 pendrive plugged in.

There is generally 3 people by day comming to plug their (USB!) stick in the machine (it looks like a buck-machine for credit card).

My Question Please help, I dont know what to do to say my script :

Hey, the only USB device is /dev/sdX1

Or

Hey, no USB pendrive in my I/O.

Thank you

1

Using find and awk

#!/bin/bash

# your code ...

# The name of your MAXimum 1 drive in /media/$USER
maximum1="MAXimum 1"

if [ -z "$(find /media/$USER/ -maxdepth 1 ! -path /media/$USER/ -type d |\
    awk '! /'"$maximum1"'/')" ]
then
    echo "Hey, the only USB device is $maximum1"
else
    echo "Hey, no USB pendrive in my I/O."
fi

# your code ...

Example

% ls -laog /media/$USER

total 24
drwxr-x---+ 3  4096 Aug 20 15:16 .
drwxr-xr-x  5  4096 Aug 18 08:43 ..
drwxr-xr-x  8 16384 Jan  1  1970 STORE N GO

% maximum1="STORE N GO"; if [ -z "$(find /media/$USER/ -maxdepth 1 ! -path /media/$USER/ -type d | awk '! /'"$maximum1"'/')" ]; then echo "Hey, the only USB device is $maximum1"; else echo "Hey, no USB pendrive in my I/O."; fi
Hey, the only USB device is STORE N GO

% maximum1="NOT STORE N GO"; if [ -z "$(find /media/$USER/ -maxdepth 1 ! -path /media/$USER/ -type d | awk '! /'"$maximum1"'/')" ]; then echo "Hey, the only USB device is $maximum1"; else echo "Hey, no USB pendrive in my I/O."; fi
Hey, no USB pendrive in my I/O.
1
  • And when is asap? =) – A.B. Aug 22 '15 at 18:31
1

Ubuntu has neat command udisksctl which allows looking up information with the info option on a block device specified with the -b flag.

Now, if we scan the output of udiskctl for each block device from a to z, and search for the 'usb' string in it, we can be sure it is a USB device.

Bellow is a script that does exactly that:

#!/bin/bash

FOUND=0
for var in {a..z}; do 

    udisksctl info -b /dev/sd$var 2>/dev/null | grep -q 'usb'; 
    if [ $(echo $?) -eq 0 ]; then 
        echo " FOUND USB DEVICE /dev/sd$var "; 
        FOUND=1
    fi 


done 

if [ $FOUND -eq 0 ]; then
    echo "NO USB DEVICES FOUND"
fi

There we have FOUND flag, that just serves the purpose of printing "NO USB DEVICES FOUND" if... well... if no USB devices are found! Otherwise, if we find at least one USB device , we set that flag to 1 and ignore it.

Here's sample output with two USBs plugged in and then removed:

CURRENT DIR:[/home/xieerqi]
$ ./bin/sh/findusb.sh                                                                                                                         
 FOUND USB DEVICE /dev/sdb 
 FOUND USB DEVICE /dev/sdc 

CURRENT DIR:[/home/xieerqi]
$ ./bin/sh/findusb.sh                                                                                                                         
NO USB DEVICES FOUND

Feel free to modify this script to suit your need.

2
  • Seems to be better than my answer =) – A.B. Aug 22 '15 at 18:30
  • @A.B. yours is fine as long as no-one mounts anything like a hard drive partition under /media/$USER/. Assuming that only admin can mount partitions there - I'd say it's alright. Also, if admin manually mounts a usb on another folder, say /mnt, then there's problem. – Sergiy Kolodyazhnyy Aug 22 '15 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.