On every reboot the USB port assignments of an attached scanner are incorrect. My goal is to create a script that runs on reboot eliminating my interaction.

Here's what I do manually to correct the port assignments.

    1) lsusb -d 04f9:0272  #the output identifies the correct ports of the scanner       
    2) sudo chmod a+w /dev/bus/usb/001/002 #scanner now works

The following script creates the variables but chmod fails reporting "no such file or directory".

    buss=$(lsusb -d 04f9:0272 |awk '{print $2}')
    devis=$(lsusb -d 04f9:0272 |awk '{print $4}')
    sudo chmod a+w /dev/bus/usb/$buss/$devis

The correct values are displayed when I echo $buss or $devis. I know I will need to do more to automate this process.

  • 2
    What is output of ls -l /dev/bus/usb/001/002 (per your example)? You may find that the easiest solution is to put the set of users who should have access to the scanner in a particular group such as scanner. Then no need for fiddling around with a script. – roaima Jul 23 '17 at 22:19
  • crw-rw-r-- 1 root lp 189, 1 Jul 24 18:48 /dev/bus/usb/001/002 – Guy D'Amico Jul 24 '17 at 22:59
  • This sounds like you should be reading up on the use of udev vs trying to handle it in multi user mode – jas- Jul 25 '17 at 1:45

The problem is probably the specific formatting of the lsusb output:

% usbid=1d6b:0001
% lsusb -d $usbid | awk '{ print "/dev/bus/usb/" $2 "/" $4 }'

Notice the extra colon at the end. Fortunately you can easily remove it, either in awk or in the shell, here's an awk example:

% usbid=1d6b:0001
% lsusb -d $usbid | awk '{ a = $4; sub(/:$/, "", a); print "/dev/bus/usb/" $2 "/" a }'
  • What is the purpose of the "%" preceding the first two lines? Also, If possible would you explain what's happening in this string: "'{ a = $4; sub(/:$/, "", a); print "/dev/bus/usb/" $2 "/" a }'" – Guy D'Amico Jul 23 '17 at 20:36
  • Oh, the % is simply a shell prompt sign, indicating that that line is typed; the line without it is the output of the command. – Josip Rodin Jul 23 '17 at 22:10
  • The fourth column is assigned into the temporary variable a, which is in turn processed with a sub() replacement to omit the final colon at the end of the line according the regular expression between the slashes. Finally, the output string is printed, consisting of pre-defined parts, the second column, and the modified fourth column. You can read more about these elements of awk syntax in its awk(1) manual page. – Josip Rodin Jul 23 '17 at 22:12

That looks like it should work, except that the output of lsusb contains a colon after the device number:

$ lsusb -d 1d6b:0002
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

So the devis variable would contain that, and the end result would be that you'd try to access /dev/bus/usb/001/001:, with the colon.

You can get rid of the trailing colon with the suffix-deleting expansion, like this:


Another way to do that in just the shell (without awk) would be to just read the output of lsusb. This only takes the first line lsusb outputs, there might be more if there are more than one device that match.

In shells with process substitution:

read x buss x devis x < <(lsusb -d 04f9:0272)
chmod +w "/dev/bus/usb/$buss/$devis"

Or with a pipe:

lsusb -d 04f9:0272 | {
   read x buss x devis x
   chmod +w "/dev/bus/usb/$buss/$devis"
  • I couldn't figure out where the trailing colon was coming from and there it was staring at me in the lsusb output. ;-( Are you able to tell me what to do with the file I call "scan.sh" so that it is always read once the OS is rebooted or must I manually execute it in the terminal? – Guy D'Amico Jul 23 '17 at 20:29

In the comments you provide the output of ls -l for your device:

crw-rw-r-- 1 root lp 189, 1 Jul 24 18:48 /dev/bus/usb/001/002

You can see that the user and group already have read/write access. So if you add your users to the group lp none of this script stuff remains necessary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.