3

How to find multiple files present in a directory in ksh (on AIX)

I am trying below one:

if [ $# -lt 1 ];then
    echo "Please enter the path"
    exit
fi
path=$1
if [ [ ! f $path/cc*.csv ] && [ ! f $path/cc*.rpt ] && [ ! f $path/*.xls ] ];then
    echo "All required files are not present\n"
fi

I am getting error like check[6]: !: unknown test operator //check is my file name.

what is wrong in my script. Could someone help me on this.

Aravind
  • 1,599

2 Answers2

4

test -f won't work for multiple files expanded from wildcards. Instead you might well use a shell function with null-redirected ls.

present() {
        ls "$@" >/dev/null 2>&1
}

if [ $# -lt 1 ]; then
    echo "Please enter the path"
    exit
fi
path=$1
if ! present $path/cc*.csv && ! present $path/cc*.rpt && ! present $path/*.xls; then
    echo "All required files are not present\n"
fi

Btw is it fine to use &&? In this case you get not present only when there're no files named cc*.csv or cc*.rpt or cc*.xls in $path.

yaegashi
  • 12,326
  • 1
    difference to test -f: ls will not report a failure when the files is a broken symlink (pointing to nowhere), or when the "file" is a directory. – simohe Oct 17 '18 at 08:31
3
if [ [ ! f $path/cc*.csv ] && [ ! f $path/cc*.rpt ] && [ ! f $path/*.xls ] ];then
    echo "All required files are not present\n" fi

check[6]: !: unknown test operator

I think you forgot the operand 'f' is an unkown operand --> '-f'

if [ [ ! -f $path/cc*.csv ] && [ ! -f $path/cc*.rpt ] && [ ! -f $path/*.xls ] ];then
    echo "All required files are not present\n"
fi

In your case you all files have to be missing before it will echo your ... it of course depend on your goal.

I'm not able to check it out in ksh on AIX.

kris
  • 163