So I want to mount a usb device using a script if it's plugged in at boot. The usb device is not plugged in at every boot but others may be so I need to check if the label is correct and then mount it. So far I have the following line:

lsblk -rnpo label,name | awk '$1=="USB_FOO"{mount $2 /path/to/mount}'

but executing it gives me the following error:

awk: cmd. line:1: (FILENAME=- FNR=6) fatal: division by zero attempted

What would be a working example?

  • I figured it out using variables in the script, still I am interested if (and how) this can be solved in a one-liner. Jul 18, 2017 at 19:12

1 Answer 1


You can do that using /etc/fstab. Just append the following line into the file:

/dev/sdX   /path/to/mount   auto   defaults,nofail   0   0
  • To be sure it's the correct labeled device, use LABEL=<label> instead of /dev/sdX in the answer. But if this is a homework question to use a script, you're on your own - nobody would write a script fo that, because it is an inherent feature of linux mount.
    – ridgy
    Jul 18, 2017 at 21:26
  • You can also add user to the options, so that a regular user can mount a device. Jul 18, 2017 at 21:39
  • It's no homework question I already figured out a way. I only read one config file from the device if it's plugged in and then it should be possible to remove it without causing any "damage". So using fstab and letting it mount it automatically would keep the device mounted even after the config file was read. I could use the script to unmount it but it's easier to have the mount, config file reading and the unmount process in one script. Jul 19, 2017 at 12:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .