3

Just learning bash and trying to implement a function in a script.

The script below runs thru ShellCheck fine, but I get a syntax error running bash from the command line

It must be the way I've defined my function, but I can't figure out the right way to do it - especially if it passes ShellCheck

error is on line 24
syntax error near unexpected token `${\r''
`autounload() {

Script:

#!/bin/bash
# /home/pi/scripts/usb-unloader.sh
#
# Called from {SCRIPT_DIR}/usb-initloader.sh
# make sure to chmod 0755 on file
#
# UnMounts usb device on /media/<device>
# Logs changes to /var/log/syslog and local log folder
# use tail /var/log/syslog to look at latest events in log
#
# CONFIGURATION
#
LOG_FILE="$1"
MOUNT_DIR="$2"
DEVICE="$3"  # USB device name (from kernel parameter passed from rule)
#
# check for defined log file
if [ -z "$LOG_FILE" ]; then
    exit 1
fi
#
# autounload function to unmount USB device and remove mount folder
#
autounload() {
    if [ -z "$MOUNT_DIR" ]; then
        exit 1
    fi
    if [ -z "$DEVICE" ]; then
        exit 1
    fi

    dt=$(date '+%Y-%m-%d %H:%M:%S')
    echo "--- USB Auto UnLoader --- $dt"

    sudo umount "/dev/$DEVICE"
    sudo rmdir "$MOUNT_DIR/$DEVICE"

    # test that this device isn't already mounted
    device_mounted=$(grep "$DEVICE" /etc/mtab)

    if ! "$device_mounted"; then
        echo "/dev/$DEVICE successfully Un-Mounted"
        exit 1
    fi
}

autounload >> "$LOG_FILE" 2>&1

This is the part of the code where I start defining the function

#
autounload() {
    if [ -z "$MOUNT_DIR" ]; then
        exit 1
    fi

I've tried to move function above and below where it gets called but it seems to makes no difference.

Wyetro
  • 8,439
  • 9
  • 46
  • 64
dbmitch
  • 5,361
  • 4
  • 24
  • 38
  • 4
    you probably have a DOS file running in UNIX. Try to run `dos2unix` to "clean" it – fedorqui Jul 06 '16 at 22:24
  • I'm editing on Windows using Textpad then ftp over. So if I just use sudo nano that should clean it up? – dbmitch Jul 06 '16 at 22:29
  • Not really, since Windows has different line endings. Just use the tool `dos2unix` (or others as seen in [Windows command to convert Unix line endings?](http://stackoverflow.com/q/17579553/1983854)) to replace those. – fedorqui Jul 06 '16 at 22:38
  • 1
    Thanks! - Damn - should have asked an hour ago! - Put that in an answer and I'll mark it. This has been a steep learning curve for me. – dbmitch Jul 06 '16 at 22:39
  • Haha nice to read that this was useful. Happy coding!! – fedorqui Jul 06 '16 at 23:18
  • 2
    This is covered in the [`bash` tag info](http://stackoverflow.com/tags/bash/info). – chepner Jul 07 '16 at 01:18
  • Wow - that looks like a great resource - I don't know if I would have found it looking for syntax errors in the toc - but there's a few other great hints I can use already. Next up - trying to find out why my udev rules aren't working any more – dbmitch Jul 07 '16 at 02:07
  • @chepner that is indeed a great resource. I somehow think hardly anybody knows about it, so it makes me think that having good canonical Q&A may be best on the long term. – fedorqui Jul 07 '16 at 06:55

1 Answers1

2

As discovered in comments, you have a Windows (DOS) file that then runs on a UNIX machine. This has the problem that the line endings in DOS are \r\n while in UNIX are \n, so there is a superfluous \r in every line.

To get rid of those you can use either of these:

Community
  • 1
  • 1
fedorqui
  • 275,237
  • 103
  • 548
  • 598