[-f: 命令未找到,Bash 脚本文件存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26827718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 11:45:46  来源:igfitidea点击:

[-f: Command not found, Bash script does file exist

bash

提问by Wilken

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

我正在尝试编写的脚本有问题。缩小和简化代码,它给出了找不到命令的错误。如果我在命令行中执行“test -f file”,它不会返回任何内容,而不是找不到命令

        PATH=

        #!/bin/bash
        DIR=

                    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 ]]; then
         VFY[]="f"
         echo " is a file"
       elif [[-d ]]
       then
         VFY[]="d"
         echo " is a directory"
       else 
         VFY[]=0
         echo -e "\r"
         echo " 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”

回答by Edouard Thiel

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= ind=

    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
}