Check file and if it doesnt exist , exit script

Hi,

Another problem, here is my code

#!/bin/sh

dir='/opt/apps/script/CSV'
datadir='/opt/apps/script/data'

while : ; do
ls -1rt $dir/*.csv > /dev/null 2>&1
if [ $? -eq 0 ] ;then
cp $datadir/weekly.txt $dir/weekly.csv
else
exit 0
fi
done

before it executes the while do, I would like it to check for a file named daily.txt in /opt/apps/script/data/

check /opt/apps/script/data/daily.txt first, if it exists, proceed, else exit code.something like this I guess?

#!/bin/sh

dir='/opt/apps/script/CSV'
datadir='/opt/apps/script/data'

-> Check here first, if found, proceed, else exit

while : ; do
ls -1rt $dir/*.csv > /dev/null 2>&1
if [ $? -eq 0 ] ;then
cp $datadir/weekly.txt $dir/weekly.csv
else
exit 0
fi
done

Thanks buddies.Regards :slight_smile:

if [ ! -f /opt/apps/script/data/daily.txt ]; then
  exit 1;
fi
if [ -s "/opt/apps/script/data/daily.txt" ]; then
	while : ; do
	ls -1rt $dir/*.csv > /dev/null 2>&1
	if [ $? -eq 0 ] ;then
		cp $datadir/weekly.txt $dir/weekly.csv
	else
		exit 0
	fi
	done
else
	exit 1
fi

"-s" - checks for existance and have size greater than zero.
if you just want to check the existance, use "-e" instead.

Must agree with anchal_khare, -e is much better.

getting infinite loop :stuck_out_tongue:

Does the file /opt/apps/script/data/daily.txt exist?

it does

Well, if the file exists, the endless while loop executes.

so like, how do I fix that? can u please help thanks :slight_smile:

I couldn't quite get why you need the loop.

the loop is to check is if the file I want (matches the string) exists, and if it does, keep processing, until the file are no more available

I think my code is wrong logic wise :stuck_out_tongue:

what is should do is,

1) check if file matching abc.txt.(number) is available or not. eg,
abc.txt.2009070198763
2) if its available, do code
3) if its not available, break loop, exit code.

can anyone come up with a basic code for this please? thank you very much! :smiley:

check latest file matching string abc.txt.nnnnnnnnnnnn
if exists,
do blablabla
(at final stage I will remove the file processed just now ($lastfile), then,
repeat loop
else (if file not found at the beginning or during next loop)
break loop
exit