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

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

This question already has an answer here:

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        } 

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"

回答1:

Simply add an extra space between [[ and -f, and also before ]].

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 } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!