Bash script to mount and unmount a BBC Micro:bit

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_manage.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 udiksctl dump
RESULTS=$(udisksctl dump | grep IdLabel | grep -c -i $MICRO)

case "$RESULTS" in

0 )     echo "no $MICRO found in 'udkisksctl 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

2 thoughts on “Bash script to mount and unmount a BBC Micro:bit”

  1. I was not able to run your code in my PC (KDE Neon 5.15).

    Actually, my PC is not detecting microbit at all. Do you have any suggestion on what to do? Microbit’s website just assumes it will work seamlessly on Linux as any other USB device…

    1. I’ve updated the code in my blog to the latest I am using. I hope that this helps.

      After connecting your microbit, try:
      gvfs-mount –list

      This should display the following line somewhere in the output:

      Volume(0) : MICROBIT

Leave a Reply

Your e-mail address will not be published. Required fields are marked *