Bash 脚本:如何在 "if" 存在文件中的文件名前传递通配符?
Bash Script : How to pass a wildcard before my filename in "if" exist file?
我希望能够检查文件是否存在。
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
但是通配符/小丑不起作用
我怎样才能通过它?
您的通配符无效,因为它被引用了。然而,取消引用它可能会破坏 [
命令,因为它只需要一个文件名参数,如果两个或更多文件被 globbed,它就会破坏。
在 bash 中,您可以使用 compgen
来生成匹配 globbing 模式的文件列表,如果没有找到 glob,它还会设置正确的退出状态,这是 hack 吗?我不知道,但它可能看起来像:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi
我希望能够检查文件是否存在。
if [ -f "/var/run/screen/user/*.$InstanceName" ]; then
echo -e "screen instance exist"
fi
但是通配符/小丑不起作用
我怎样才能通过它?
您的通配符无效,因为它被引用了。然而,取消引用它可能会破坏 [
命令,因为它只需要一个文件名参数,如果两个或更多文件被 globbed,它就会破坏。
在 bash 中,您可以使用 compgen
来生成匹配 globbing 模式的文件列表,如果没有找到 glob,它还会设置正确的退出状态,这是 hack 吗?我不知道,但它可能看起来像:
if compgen -G "/var/run/screen/user/*/$InstanceName" > /dev/null; then
printf "screen instance exist\n"
fi