Bash script to mount and unmount a BBC Micro:bit
Last updated: Jan 24, 2023
I develop using the BBC Micro:bit (which I’ll call a microbit from now on) using Linux. To get a new hex file on to the microbit, the microbit has to be mounted on to the file system. Which may seem obvious. But the microbit has a habit of unmounting itself after being programmed. Or not mounting when it is plugged in. So I wrote a script to make things easier. Because I can. To be more accurate, I copied a script from Stackoverflow and made a few minor modifications. Don’t pretend this isn’t how you have written a lot of your scripts. Find the code at the bottom of this post. Copy and paste it to a file called microbit_manage.sh. Make the file executable (chmod +x ./microbit_manage.sh) and run using either ‘mount’ or ‘unmount’ as an argument. I’ve got these aliased to ‘mm’ and ‘md’ in my .bashrc.
Example usage:
bill@bill ~ $ microbit\_manage.sh mount
found one MICROBIT, device: /dev/sdb
MICROBIT was unmounted
Mounted /dev/sdb at /media/bill/MICROBIT.
bill@bill ~ $ microbit\_manage.sh unmount
found one MICROBIT, device: /dev/sdb
MICROBIT was mounted
Unmounted /dev/sdb.
Comments and improvements welcome. The script is:
#!/bin/bash
# microbit_mount.sh
# mount and unmount microbit
# modified from https://askubuntu.com/questions/342188/how-to-auto-mount-from-command-line
# v1.0 matt oppenheim October 2017
BASEPATH="/media/$(whoami)/"
MICRO="MICROBIT"
if [ $# -eq 0 ]
then
echo "no argument supplied, use 'mount' or 'unmount'"
exit 1
fi
if [ $1 == "--help" ]
then
echo "mounts or unmounts a BBC micro:bit"
echo "args: mount - mount the microbit, unmout - unmount the microbit"
fi
# how many MICRO found in udisksctl dump
RESULTS=$(udisksctl dump | grep IdLabel | grep -c -i $MICRO)
case "$RESULTS" in
0 ) echo "no $MICRO found in 'udisksctl dump'"
exit 0
;;
1 ) DEVICELABEL=$(udisksctl dump | grep IdLabel | grep -i $MICRO | cut -d ":" -f 2 | sed 's/^[ \t]*//')
DEVICE=$(udisksctl dump | grep -i "IdLabel: \+$DEVICELABEL" -B 12 | grep " Device:" | cut -d ":" -f 2 | sed 's/^[ \t]*//')
DEVICEPATH="$BASEPATH""$DEVICELABEL"
echo "found one $MICRO, device: $DEVICE"
if [[ -z $(mount | grep "$DEVICE") ]]
then
echo "$DEVICELABEL was unmounted"
if [ $1 == "mount" ]
then
udisksctl mount -b "$DEVICE"
exit 0
fi
else
echo "$DEVICELABEL was mounted"
if [ $1 == "unmount" ]
then
udisksctl unmount -b "$DEVICE"
exit 0
fi
fi
;;
* ) echo "more than one $MICRO found"
;;
esac
echo "exiting without doing anything"`
Prior to mounting the microbit, running the command:
gvfs-mount –list
Should return a bunch of stuff, with a block like this in it:
Drive(1): MBED DAPLINK VFS
Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
Volume(0): MICROBIT
Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
This shows that the microbit is connected, but not mounted or usable by the file system.
After mounting the microbit using the above script, running:
gvfs-mount --list
Should return something like:
Drive(1): MBED DAPLINK VFS
Type: GProxyDrive (GProxyVolumeMonitorUDisks2)
Volume(0): MICROBIT
Type: GProxyVolume (GProxyVolumeMonitorUDisks2)
Mount(0): MICROBIT -> file:///media/bill/MICROBIT
Type: GProxyMount (GProxyVolumeMonitorUDisks2)
Showing that the microbit is now mounted