This question already has an answer here:
这个问题已经有了答案:
- Why should there be a space after '[' and before ']' in Bash? 4 answers
- 为什么在Bash中“['和before ']”后面要有空格?4答案
I'm having an issue with a script that I am trying to program. Narrowed down and simplified code and it gives an error that command is not found. If i do "test -f file" in command line it returns nothing, not command not found
我对我正在编写的脚本有意见。缩小和简化的代码,并给出一个错误的命令没有找到。如果我在命令行中执行“测试-f文件”,它不会返回任何内容,而不会返回未找到的命令
PATH=$1
#!/bin/bash
DIR=$1
if [[-f $PATH]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit
Here is the actual more complicated script I'm trying to run
下面是我要运行的更复杂的脚本
verify()
{
if [[-f $1]]; then
VFY[$2]="f"
echo "$1 is a file"
elif [[-d $1]]
then
VFY[$2]="d"
echo "$1 is a directory"
else
VFY[$2]=0
echo -e "\r"
echo "$1 is neither a file or a directory"
echo -e "\r"
fi
}
Its part of a larger script that can move things around depending on inputs. I've run this in CentOS 6, and FreeBSD, both give the same error "[[-f: Command not found"
它是一个更大的脚本的一部分,可以根据输入来移动东西。我在CentOS 6和FreeBSD中运行过这个,它们都给出相同的错误"[-f: Command not found"
1 个解决方案
#1
4
Simply add an extra space between [[
and -f
, and also before ]]
.
只需在[[和-f,以及之前]之间添加额外的空格即可。
You will get:
你将得到:
#! /bin/bash
DIR=${1-} # unused in your example
if [[ -f test.sh ]]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
exit
and for your function
和你的函数
verify() # file ind
{
local file=$1 ind=$2
if [[ -f "$file" ]]; then
VFY[ind]="f" # no need of $ for ind
echo "$file is a file"
elif [[ -d "$file" ]]; then
VFY[ind]="d"
echo "$file is a directory"
else
VFY[ind]=0
echo -e "\n$file is neither a file or a directory\n"
fi
}
#1
4
Simply add an extra space between [[
and -f
, and also before ]]
.
只需在[[和-f,以及之前]之间添加额外的空格即可。
You will get:
你将得到:
#! /bin/bash
DIR=${1-} # unused in your example
if [[ -f test.sh ]]; then
echo "expression evaluated as true"
else
echo "expression evaluated as false"
fi
exit
and for your function
和你的函数
verify() # file ind
{
local file=$1 ind=$2
if [[ -f "$file" ]]; then
VFY[ind]="f" # no need of $ for ind
echo "$file is a file"
elif [[ -d "$file" ]]; then
VFY[ind]="d"
echo "$file is a directory"
else
VFY[ind]=0
echo -e "\n$file is neither a file or a directory\n"
fi
}