我正在尝试编写一个脚本问题。缩小和简化代码,它给出了一个错误,即找不到命令。如果我在命令行中执行“test -f file”,则不返回任何内容,而不返回命令
PATH=$1
#!/bin/bash
DIR=$1
if [[-f $PATH]]; then
echo expression evaluated as true
else
echo expression evaluated as false
fi
exit
这是我正在尝试运行的实际更复杂的脚本
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
}
它是一个更大的脚本的一部分,可以根据输入移动东西。我在CentOS 6中运行它,而FreeBSD都给出了同样的错误“[[-f:Command not found”
答案 0 :(得分:4)
只需在[[
和-f
之间以及]]
之前添加额外的空格。
你会得到:
#! /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
和你的功能
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
}