Skip to content

Instantly share code, notes, and snippets.

@oznakn
Last active July 4, 2019 21:34
Show Gist options
  • Save oznakn/855e21bd0cc6f169b39aaef95ad836f1 to your computer and use it in GitHub Desktop.
Save oznakn/855e21bd0cc6f169b39aaef95ad836f1 to your computer and use it in GitHub Desktop.
Mount a device automatically, experimental
#!/usr/bin/env sh
already_did=$(sudo mount | grep "$1" | wc -l)
if [[ "$already_did" == 0 ]]; then
blkid_output=$(sudo blkid)
result=$(echo "$blkid_output" | grep "$1" | grep -Po ' UUID="(.*?)"' | awk '{print substr($1, 7, length($1) - 7)}')
if [[ ${#result} != 0 ]]; then
label=$(echo "$blkid_output" | grep "$1" | grep -Po ' LABEL="(.*?)"' | awk '{print substr($1, 8, length($1) - 8)}')
if [[ ${#label} == 0 ]]; then
label=$(echo "$blkid_output" | grep "$1" | grep -Po ' PARTLABEL="(.*?)"' | awk '{print substr($1, 12, length($1) - 12)}')
fi
if [[ ${#label} == 0 ]]; then
label="$result"
fi
device_type_output=$(echo "$blkid_output" | grep "$1")
device_type=""
if [[ $(echo "$device_type_output" | grep 'TYPE="ntfs"' | wc -l) == 1 ]]; then
device_type="ntfs-3g"
elif [[ $(echo "$device_type_output" | grep 'TYPE="vfat"' | wc -l) == 1 ]]; then
device_type="vfat"
elif [[ $(echo "$device_type_output" | grep 'TYPE="ext4"' | wc -l) == 1 ]]; then
device_type="ext4"
fi
if [[ "$device_type" == "" ]]; then
echo "device type not found"
else
sudo mkdir -p "/mnt/$label"
sudo mount -t "$device_type" -o user,users,rw,uid=oznakn,gid=users,dmask=0022,fmask=0022,utf8 "$1" "/mnt/$label"
echo "/mnt/$label"
fi
else
echo "device not found"
fi
else
echo "already mounted"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment