为什么我不能使用来自 zenity 的这段文字来查找文件?



我想使用zenity文本输入来选择文件,这样我就可以拖放了。看起来应该有效,但没有。当我拖动文件时,它找不到它,但路径和文件名似乎是正确的。

";切割";就是去掉前面的";文件:/";从输入文本。

xinput=$(zenity --entry 
--title="Drag in file" 
--text="" 
--entry-text ""  )
xfile=$(echo "$xinput" | cut -c 7-)
if test -f "$xfile"; then
echo "Found!"
else 
echo "Not Found!"
fi
echo $xfile

拖放操作似乎是以file://为前缀,并将r附加到文件路径中。它还将潜在的问题字符转换为url编码的版本。

#!/bin/bash
xinput=$(
zenity --entry 
--title="Drag in file" 
--text="" 
--entry-text ""
)
# extract the part of interest
[[ $xinput =~ ^file://(.*)$'r'$ ]] || exit 1
# decode the string
printf -v xfile %b "${BASH_REMATCH[1]//%/\x}"
if [[ -e "$xfile" ]]
then
printf '%qn' "$xfile"
else
printf 'No such file or directory: %qn' "$xfile"
fi

备注:我使用包含所有ASCII字符(0x010x7F(的路径测试代码

最新更新