bash - [-f : Command not found, Bash 脚本文件是否存在

标签 bash

<分区>

我在尝试编写脚本时遇到问题。缩小并简化了代码,它给出了一个错误,即找不到命令。如果我在命令行中执行“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”

最佳答案

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

关于bash - [-f : Command not found, Bash 脚本文件是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827718/

相关文章:

bash - 在安装hadoop时,当我运行start-dfs.sh命令时,它显示 'no such file or directory found'

linux - 是否可以在同一行中打印 awk 输出

linux - sudo: (whatever): 在 RHEL 5.8 上找不到命令

用于计算多个整数值的平均值的 bash 脚本

bash - Shell 脚本 - 数字检查和 if 语句问题

bash - 如果 bash 脚本中除了第一个参数之外没有设置任何参数,则使用默认值

Bash 脚本不工作

linux - 向本地 Linux FIFO 队列提交命令

bash - linux上的进程状态

arrays - 来自用户输入的 Bash 数组键