Luk*_*kap 2 bash shell scripting
我有这个
if [[ -e file.jpg ]] ;then echo "aaaaaaaaa"; fi
Run Code Online (Sandbox Code Playgroud)
并打印出"aaaaaaa"
但是如果还有file.png或file.png我想要打印
所以我需要这样的东西
if [[ -e file.* ]] ;then echo "aaaaaaaaa"; fi
Run Code Online (Sandbox Code Playgroud)
但它不起作用我在语法中遗漏了一些东西
谢谢
如果启用bash的nullglob设置,如果没有这样的文件,模式文件.*将扩展为空字符串:
shopt -s nullglob
files=(file.*)
# now check the size of the array
if (( ${#files[@]} == 0 )); then
echo "no such files"
else
echo "at least one:"
printf "aaaaaaaaa %s\n" "${files[@]}"
fi
Run Code Online (Sandbox Code Playgroud)
如果你没有启用nullglob,那么files=(file.*)将导致一个包含一个元素的数组,字符串"file.*"