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.

1
  • 3
    If they are required, then it seems to me your script should fail if any of them is missing, not if all of them are missing. Aug 27, 2015 at 11:20

2 Answers 2

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.

1
  • 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, 2018 at 8: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.

Not the answer you're looking for? Browse other questions tagged .