BashN00b BashN00b - 1 month ago 11
Bash Question

Issue in file creation (when whitespaces are present)

I wrote couple scripts to generate a QR code every time a .cia file is uploaded on my home ftp, to watch the folder I used inotify. The problem occurs when I have a file with whitespaces, having "one two three.cia" it will generate 4 QR codes: one.png,two.png,three.png and one_two_three.png (which is the only i want).

EDIT: If i run ./despacer "file name one.cia" and then ./rename i have the desired result. I guess this narrow down the issue to qr.sh

Ideas? Thanks a lot!

qr.sh

#!/usr/bin/env bash

dir=/ftproot
while true
do

inotifywait -m -q --format '%w%f' -e create,moved_to $dir | while read FILE; do
if [[ $FILE == *.cia ]]; then /ftproot/despacer.sh "$FILE" & /ftproot/rename.sh
fi
done
done


despacer.sh

#!/bin/bash

mv "$1" `echo $1 | sed 's/ /_/g'` 2>/dev/null


rename.sh

#!/bin/bash

basename -s .cia *.cia > qr.txt

for i in $(cat qr.txt);
do qrencode -o $i.png http://www.mywebsite.com/$i.cia
done

mv *.png qr/
rm qr.txt

exit

Answer

There are several issues with quoting, that's the reason for a failure with spaces. The most problematic is the script you call rename.sh.

All three edited are:

qr.sh

#!/bin/bash

dir=/ftproot
while true 
do
    inotifywait -m -q --format '%w%f' -e create,moved_to "$dir" |
    while read -r FILE; do
        if [[ $FILE == *.cia ]]
        then
            /ftproot/despacer.sh "$FILE" & /ftproot/rename.sh "$FILE"
        fi
    done
done

despacer

#!/bin/bash

mv "$1" "${1// /_}"

rename.sh

#!/bin/bash

encode(){
                for i in "${@#.cia}";
                do qrencode -o "$i.png" "http://www.mywebsite.com/$i.cia"
                done
        }

encode "$@"
mv ./*.png qr/

I did not run the scripts, but believe they are correct.