Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash script missing ']' error

Tags:

linux

bash

I have created a script to mount an usb device and cdrom drive automaticly when its plugged in. I had been trying to get this working with autofs (see https://superuser.com/questions/605896/automount-usb-pen-drive-and-cdrom-drive-on-plugin) but now trying to work with a script running at boot.

I think the logic is sound but I get line 13: [: missing ']'

The script is:

#!/bin/bash
while true
 do
    if [ -b /dev/sda1 ]
       then
       mkdir /media/usb
       mount /dev/sda1 /media/usb
       while [ -b /dev/sda1]; do
         sleep 2
       done
       umount /media/usb
       rmdir /media/usb
    fi
    if [ -b /dev/cdrom ]
       then
       mkdir /media/cdrom
       mount /dev/sda1 /media/cdrom
       while [ -b /dev/cdrom]; do
         sleep 2
       done
       umount /media/cdrom
       rmdir /media/cdrom
    fi

    sleep 5
 done

And then script is launched from rc.local using ./path/to/script&

like image 855
Zac Powell Avatar asked Dec 27 '22 02:12

Zac Powell


1 Answers

You need a space here:

while [ -b /dev/cdrom]; do
                     ^
_____________________|
like image 162
jaypal singh Avatar answered Jan 02 '23 14:01

jaypal singh