2

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"

Sean Sullivan
  • 329
  • 3
  • 6
  • 17

1 Answers1

6

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
}
Edouard Thiel
  • 5,878
  • 25
  • 33
  • Thank you!, that fixed it, I was starting to lose my mind. If you don't mind me asking what is the "${1-}" and the [ind] for the array? – Sean Sullivan Nov 09 '14 at 11:47
  • `${1-hello}` is expanded by argument 1 if there is one, else by `hello` (here empty string). `ind` is `$2`, and in `[]` bash do arithmetic expansion, so `VFY[ind]` stands for `VFY[$2]`. – Edouard Thiel Nov 09 '14 at 11:59