Check file and if it doesnt exist , exit script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check file and if it doesnt exist , exit script
# 1  
Old 07-16-2009
Check file and if it doesnt exist , exit script

Hi,

Another problem, here is my code

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?

Code:
#!/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 Smilie
# 2  
Old 07-16-2009
Code:
if [ ! -f /opt/apps/script/data/daily.txt ]; then
  exit 1;
fi

# 3  
Old 07-16-2009
Code:
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.

Last edited by clx; 07-16-2009 at 10:35 AM.. Reason: replaced "u" with "you".
# 4  
Old 07-16-2009
Must agree with anchal_khare, -e is much better.
# 5  
Old 07-16-2009
getting infinite loop :P
# 6  
Old 07-16-2009
Does the file /opt/apps/script/data/daily.txt exist?
# 7  
Old 07-16-2009
it does
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check file if not found send mail if exit call second script

I need to check my script and change to working mode. currently it was not sending the mail and exit without calling the second script. I need to check the file is present ="/home/Rvtools/test.csv" if this file not found after the time retry send mail file not found If the file exit run the... (2 Replies)
Discussion started by: ranjancom2000
2 Replies

2. Shell Programming and Scripting

Create user if UID not exist; else, exit the script

Hi, I want to write a script to check whether an user ID is used in my server and then create that user. If the user ID is not used, I will echo something like "OK, continue" and then continue to execute the script. Else, I will echo something like "Used, exit" and then exit the script. As... (4 Replies)
Discussion started by: dirkaulo
4 Replies

3. Shell Programming and Scripting

Shell script to check files if exist else touch the file

Hi All, Thanks in Advance I wrote the following code if then echo "version is 1.1" for i in "subscriber promplan mapping dedicatedaccount faflistSub faflistAcc accumulator pam_account" do FILE="SDP_DUMP_$i.csv" echo "$FILE" ... (5 Replies)
Discussion started by: aealexanderraj
5 Replies

4. Shell Programming and Scripting

Code to remove files when corresponding file doesnt exist isnt working.

I am trying to add some code to the begging of a script so that it will remove all the .transcript files, when their is no coressponding .wav file. But it doesnt work. This is the code I have added: for transcriptfile in `$voicemaildir/*.transcript`; do wavfile=`echo $transcriptfile | cut -d'.'... (2 Replies)
Discussion started by: ghurty
2 Replies

5. UNIX for Dummies Questions & Answers

Removing a user that doesnt exist from a group

Hi there, normally if I want to remove a user tht I have added to a specific group, i would do the following this is what my group2 looks like # grep group2 /etc/group group2:x:7777:user2,user1,user4 user1 has been defined in a few groups # id -nG user1 group1 group2 group3 So... (3 Replies)
Discussion started by: rethink
3 Replies

6. Shell Programming and Scripting

Check if file exist

Hi, I am trying to create a bash script which will check if file exist then remove that file else do nothing. I have to do same process for three files in same script. I have written code for one file and trying to run it. if then rm -r /user1/abc/File1 fi When I run this code it... (1 Reply)
Discussion started by: palak08
1 Replies

7. UNIX for Dummies Questions & Answers

Need Script to check file exist and compare

I need a script that will check for the existence of new files that FTP'd in the morning, results go to log file. The 2nd step is to compare the new file with the previous days file. If the new file size is 30% or more smaller in size then previous day this needs to also be sent to log. This... (1 Reply)
Discussion started by: rbknisely
1 Replies

8. Red Hat

trying to use arp command... it doesnt exist

im trying to get an ARP readout using the command 'arp -a'... but the command doesnt exist in Fedora Core 6 - IPv6.... is there an equivalent command? (4 Replies)
Discussion started by: HMSS013
4 Replies

9. Shell Programming and Scripting

Have a shell script check for a file to exist before processing another file

I have a shell script that runs all the time looking for a certain type of file and then it processes the file through a series of other scripts. The script is watching a directory that has files uploaded to it via SFTP. It already checks the size of the file to make sure that it is not still... (3 Replies)
Discussion started by: heprox
3 Replies

10. UNIX for Dummies Questions & Answers

how to check if the file exist or not?

say i would like to check if the file is existed before i use rm command. How can i do it? i know if i can use find, but i would like to have a good interface (in a shell script) thks (3 Replies)
Discussion started by: gusla
3 Replies
Login or Register to Ask a Question