[-f:找不到命令,Bash脚本不存在文件 [英] [-f: Command not found, Bash script does file exist

查看:50
本文介绍了[-f:找不到命令,Bash脚本不存在文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要编程的脚本有问题.缩小范围并简化代码,这会导致找不到命令的错误.如果我在命令行中执行"test -f file",则不返回任何内容,未找到命令

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

        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
       }

它是较大脚本的一部分,该脚本可以根据输入内容进行移动.我已经在CentOS 6和FreeBSD中运行了它们,它们都给出了相同的错误"[[-f:找不到命令"

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"

推荐答案

只需在 [[[]和 -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
}

这篇关于[-f:找不到命令,Bash脚本不存在文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆