Check that at least one file exists in the directory.

There are some files with suffix dates like abc_20032019.dat abc_17032019.dat
If at least one file exists then perform some operation else exit from execution.
Korn shell
----------------------------------

array=($inputdir/abc*.dat)
If [  [ -f ${array[0]  ] ]
then 
echo " file exits"
else
echo " file does not exists"
fi

error inspected "("

Please let me know how to fix it.

Don't think array here is required, as well as a testing if file(s) exists.
Only in some corner cases, if file is deleted during program execution..
Is that your case ?

Will this fill your requirment :

INPUTDIR=/path/with/files
for i in $INPUTDIR/abc*.dat
do
        echo "$i" # action on existing file, here echo.
done

Hope that helps
Regards
Peasant.

If at least one file does not exists then script should throw error and execution should stop.

array=($inputdir/abc*.dat)
If [  [ -f ${array[0]  ] ]
then 
EXITCD=44
else
echo " file does not exists"
fi
return $EXITCD

--- Post updated at 05:27 AM ---

If at least one file does not exists then script should throw error and execution should stop.

array=($inputdir/abc*.dat)
If [ [ -f ${array[0] ] ]
then
echo "file exists"
else
echo " filesl does not exists"
EXITCD=44
fi
return $EXITCD
1 Like

Complement the code with :

INPUTDIR=/path/with/files
for i in $INPUTDIR/abc*.dat
do
        if  [ -f $i ]; then
                echo "File exist, processing..."
        else
                echo "File does not exist, exiting.."
                exit 1
        fi
done

Please use code tags for code, as required by forum rules.

Hope that helps
Regards
Peasant.

1 Like

Thanks.
I have doubt

When I use below command it deletes the file even there is no file exist.

INPUTDIR=/path/with/files
If [[ 'ls $INPUTDIR/abc*.dat | wc -l > 0' ]]
then
rm fiile1
else 
exit 4
if

While executing above script returning 0 and deleting file instead of exit with code 4 and log file says no such file or directory.
What is the mistake I am making?.

Using the file_exists function

if file_exists $inputdir/abc*.dat
then
  echo "a file exists"
fi

You never used code i posted, and you have expanded your code with pipes and line counts.

If [[ 'ls $INPUTDIR/abc*.dat | wc -l > 0' ]] -> will always evaluate to true
Without test it will default on -n , at least on bash here debian linux.

The command inside single quotes is never executed, just checked if the length of string passed 'ls $INPUTDIR/abc*.dat | wc -l > 0' is greater then zero.
This will always be true, and your code will never hit else condition.

if should be lower case.
Ending the if clause is done with fi , not another if as you wrote.
The fiile1 is a hard-coded file name and will be removed when you execute the script from current running directory, deleting the file if it exists.

I'm not sure what are you are trying to write here, hopefully i cleared some or all misunderstanding.
Also MiG suggested an alternative with custom function found in thread linked.

Can you be more precise about your exact requirement, with input and output expected or desired effect of your code ?
Please specify your operating system and shell details as well.

Hope that helps.
Regards
Peasant.

Below code is doing file validation properly but returning error after completion second loop. error "file does not existing". my requirement is to check only once if the file exists then it should delete data from table and enter in to the second do loop and print the file names then exit without any error. if there is no single file exists in the directory then should give error.
abc_234.dat abc _123.dat

INPUTDIR=/path/with/files
for i in $INPUTDIR/abc*.dat
do
        if  [ -f $i ]; then
                echo "delete data from table..."
do
ls -t abc*.dat | sort > ABC.lst
cat  ABC.lst | while read line
do
export file1=$line
echo "${file1}"
done
        else
                echo "File does not exist, exiting.."
                exitcd=44
        fi
done
return $exitcd

Your words are unclear, and your code sample is unclear.
Problably you mean

NPUTDIR=/path/with/files
exitcd=44
for i in $INPUTDIR/abc*.dat
do
        if  [ -f $i ]; then
                echo "delete data from table..."
                echo "$i"
                exitcd=0
                break
        fi
done
return $exitcd

The break terminates the loop.

1 Like

you can also try

#!/bin/bash
cd <PATH>
a=`(ls -lrt  | grep "total 0")`
if [ $? == "0" ]
then
echo "no file
else
<your condition>
fi

May be there are multiple files with different names like STG_123,abc_345,abc_123,STG_345 I need to look only for abc* if at least one abc file exists enter in to the loop and delete the abc files and exit.