这是我的 bash 脚本
while ./clipnotify;
do
SelectedText="$(xsel)"
CopiedText="$(xsel -b)"
Check="$(xclip -selection clip -t TARGETS -o)"
if ["$Check" != *"image"*]; then
if [[ $SelectedText != *"file:///"* ]]; then
ModifiedTextPrimary="$(echo "$SelectedText" | tr -s '\n' ' ')"
echo -n "$ModifiedTextPrimary" | xsel -i
fi
if [[ $CopiedText != *"file:///"* ]]; then
ModifiedTextClipboard="$(echo "$CopiedText" | tr -s '\n' ' ' )"
echo -n "$ModifiedTextClipboard" | xsel -bi
fi
fi
done
xclip -selection clip -t TARGETS -o
文本复制到剪贴板时的输出:
TIMESTAMP
TARGETS
MULTIPLE
SAVE_TARGETS
UTF8_STRING
COMPOUND_TEXT
TEXT
STRING
text/plain;charset=utf-8
text/plain;charset=ANSI_X3.4-1968
text/plain
xclip -selection clip -t TARGETS -o
图像复制到剪贴板时的输出:
application/x-qt-image
image/png
image/bmp
image/bw
image/cur
image/eps
image/epsf
image/epsi
image/icns
image/ico
image/jp2
image/jpeg
image/jpg
image/pbm
BITMAP
image/pcx
image/pgm
image/pic
image/ppm
PIXMAP
image/rgb
image/rgba
image/sgi
image/tga
image/tif
image/tiff
image/wbmp
image/webp
image/xbm
image/xpm
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS
我想做的是将输出保存到xclip -selection clip -t TARGETS -o
变量Check
并使用if
语句检查变量是否包含单词“图像”,以便我可以检测到剪贴板包含图像并禁用进一步执行。
运行脚本时出现错误
/home/user/Applications/Scripts/CopyWithoutLinebreaks/debug.sh: line 16: [application/x-qt-image
image/png
image/bmp
image/bw
image/cur
image/eps
image/epsf
image/epsi
image/icns
image/ico
image/jp2
image/jpeg
image/jpg
image/pbm
BITMAP
image/pcx
image/pgm
image/pic
image/ppm
PIXMAP
image/rgb
image/rgba
image/sgi
image/tga
image/tif
image/tiff
image/wbmp
image/webp
image/xbm
image/xpm
TARGETS
MULTIPLE
TIMESTAMP
SAVE_TARGETS: No such file or directory
当删除图像条件检查时,该脚本将起作用。
有什么办法解决错误吗?
答案1
使用内置函数进行比较操作时,Shell 脚本对缺少空格极为敏感
if ["$Check" != *"image"*]; then
# ^^ ^^ lack of spaces
if [ "$Check" != *"image"* ]; then
# right syntax
在 和下一个字符的开头之后应该有一个空格[
,之前也应该有一个空格,]
如果没有空格,语法评估就会[..]
失败,从而产生错误消息。