简体   繁体   中英

Binary operator expected in my bash script

With this bash script, I check the directory for *xls files, and if they are, I send them for conversion using ssconvert to *xlsx. All this works if there is only one file in the directory. If there are several or more in the directory, a "binary operator expected" error appears. Please help me fix my script.

#!/bin/bash
while true
do
test -f /home/doc/testy/*.xls && for f in /home/doc/testy/*.xls; do ssconvert "$f" "${f%.xls}.xlsx";
chown www-data:www-data "${f%.xls}.xlsx";
rm -f -- "$f"; done
sleep 10
done

test -f can only handle 1 file at a time, but you can use find instead. I don't know how to combine find's -exec with your ${f%.xls}.xlsx , so i made this look a little bit different.

#!/bin/bash -u                                                                                                                                                                                

DIRECTORY=/home/doc/testy/
export EXTENSION_OLD=xls
export EXTENSION_NEW=xlsx

function doConvert {
    fOld="$1"
    fNew="${fOld%.$EXTENSION_OLD}.$EXTENSION_NEW"
    ssconvert "$fOld" "$fNew";
    chown www-data:www-data "$fNew";
    rm -f -- "$fOld";
}
export -f doConvert

cd $DIRECTORY
while true; do
    find -type f -name "*.$EXTENSION_OLD" -exec bash -c "doConvert {}" \;
    sleep 10
done

Let me assume you are intentionally creating an infinite loop to watch the specified directory for newly generated files.
You need to check the existence of the file within the for f in.. loop. Then would you please try:

while true; do
    for f in /home/doc/testy/*.xls; do
        if [[ -f $f ]]; then
            newfile=${f}x
            ssconvert "$f" "$newfile"
            chown www-data:www-data "$newfile"
            rm -f -- "$f"
        fi
    done
    sleep 10
done

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM