2

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.

  • 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. – Stéphane Chazelas Aug 27 '15 at 11:20
1

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.

| improve this answer | |
  • 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 8:31
1
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.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.