Sh script not moving file after it is created "${path}/${file}"

My sh script for copying a created file onto a mounted floppy doesn't move the file after it is created.

The file "diskplayer.contents" is written to /home/pi/diskplayer/tmp and then this script is supposed to move it to "/media/pi/Floppy Disk/"

Watches for diskplayer.contents file and then moves it to mounted floppy

#!/bin/sh
# /etc/init.d/copyfiles.sh
### BEGIN INIT INFO
# Provides:          copyfiles.sh
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Copies file to the floppy
# Description:       Copies specific file from tmp folder to the mounted floppy once created
### END INIT INFO

inotifywait -m /home/pi/diskplayer/tmp -e create @__init__.py |
    while read path action file; do
        if [ "$file" = "diskplayer.contents" ]; then
            sudo chown root:root "${path}/${file}"
            sudo chmod 777 "${path}/${file}"
            sudo mv "${path}/${file}" /media/pi/Floppy Disk/diskplayer.contents
        fi
    done

I've tried sudo mv "${path}/${file}" /media/floppy/diskplayer.contents just to see if it would move to that folder even though the floppy isn't mounted there and it still doesn't work


Solution 1:

sudo mv "${path}/${file}" /media/pi/Floppy Disk/diskplayer.contents

Shells split command arguments by whitespace. So this becomes the equivalent of:

sudo mv "${path}/${file}" "/media/pi/Floppy" "Disk/diskplayer.contents"

Move "${path}/${file}" and "/media/pi/Floppy" to "Disk/diskplayer.contents"

This is possibly generating some sort of error. Does your init script log somewhere?

#!/bin/sh

I strongly recommend you invoke noninteractive shells with -e so that error status codes terminate the script. There's rarely any point in continuing to process a script on error, and having the whole script fail immediately is the simplest way to do it in shell.

inotifywait -m /home/pi/diskplayer/tmp -e create @__init__.py ...

I would personally reach to inotifyd for this - independent system daemon that can invoke your script when file or path is modified. It would simplify your shell script, and make it easier to test your shell script separately from the init side of the equation.