Rescan USB bus

Code:
#!/bin/sh
cd /sys/bus/usb/drivers/usb-storage
for drv_id in *:* ; do
  [ ! -h "$drv_id" ] && continue
  for device in /sys/block/sd*/device; do
    if echo $(readlink -f $device)|egrep -q "$drv_id"; then
      dev_id=`echo $device | cut -f4 -d/`
      mount=`grep "^/dev/$dev_id" /proc/mounts|cut -d\  -f2`
      echo -n "$drv_id is bound to $dev_id"
      if [ -z "$mount" ]; then
        echo " (Not mounted)"
        echo -n "$drv_id" >unbind
        echo -n "$drv_id" >bind
      else
        echo " (Mounted at $mount)"
      fi
    fi
  done
done
 
Last edited:
Yep, that'll work. Monty should now update his script with the new improved version.
Edit: Just tried it and it worked fine without the quotes but no harm in putting them in.
 
I copied the new script into the file, replacing the old one, and reset permissions. I then ejected the USB drive with Webif and ran usbscan. I got the following message:
Code:
1-2:1.0 is bound to sda (not mounted)
A few seconds later, the drive remounted as 'usb-drive1'. I did the same on a different machine and got a similar message but the drive did not mount. I ran usbscan again and got the following message:
Code:
egrep: bad regex '*:*': Invalid preceding regular expression
This HDR-FOX is currently recording. Is that the problem?
 
First box is OK. If you run the script again on that box it should then tell you that the USB drive is mounted.
Recording status shouldn't make any difference. Try installing a fresh copy of the script on the second box.
 
I ran usbscan again and got the following message:
Code:
egrep: bad regex '*:*': Invalid preceding regular expression
This HDR-FOX is currently recording. Is that the problem?
No, it's an artefact of how the shell's globbing works. I've modified the script...
 
Yep, that'll work. Monty should now update his script with the new improved version.
Edit: Just tried it and it worked fine without the quotes but no harm in putting them in.
Interesting - must be an ash/busybox extension to the test command.

Code:
#!/bin/sh

mount=

if [ -z $mount ]; then
  echo empty
else
  echo not empty
fi

On the Humax:

Code:
humax# ./s
empty

whereas with a real bourne shell:

Code:
nero% ./s
./s: test: argument expected
zsh: exit 1  ./s
 
At the risk of getting back on topic, I've added something to the web interface that will appear in the next version. Thanks due to prpr and raydon for putting the pieces together.

Screenshot%202015-01-22%2023.01.32.png
 
No, it's an artefact of how the shell's globbing works. I've modified the script...
Under what circumstances will the wildcards not be expanded properly when globbing folder contents ?
 
Last edited:
When there is nothing to match the pattern - it doesn't expand to nothing, it just stays as is, so you get one trip round the loop with the pattern as the argument.
 
When there is nothing to match the pattern - it doesn't expand to nothing, it just stays as is, so you get one trip round the loop with the pattern as the argument.
Thanks for that. Before I posted last night I had tried the script with no USB drives attached and did not get any error. Don't like inconsistencies so had another look at the script this morning and I realized that I had earlier replaced the egrep command with a simple grep, since the command argument did not require extended pattern matching. grep does not generate the error, but egrep, or grep -E, does. Mystery solved, but good practice to have the extra test at the start of the loop anyway.
 
Last edited:
Back
Top