[英]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.使用这个 bash 脚本,我检查目录中是否有 *xls 文件,如果是,我将它们发送给使用 ssconvert 转换为 *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. test -f
一次只能处理 1 个文件,但您可以改用find
。 I don't know how to combine find's -exec
with your ${f%.xls}.xlsx
, so i made this look a little bit different.我不知道如何将 find 的
-exec
与你的${f%.xls}.xlsx
结合起来,所以我让它看起来有点不同。
#!/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.您需要在
for f in..
循环中检查文件是否存在。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.