nixCraft Linux Forum

nixCraft

Linux Tech Support Forum

Checking files exist

This is a discussion on Checking files exist within the Shell scripting forums, part of the Development/Scripting category; Hi there. New to the forum so hello. I am currently studying a Computer Science degree and have some linux ...


Go Back   nixCraft Linux Forum > Development/Scripting > Shell scripting

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-06-2008, 11:56 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
My distro: Kubuntu
Posts: 6
Rep Power: 0
seeker082 is on a distinguished road
Default Checking files exist

Hi there. New to the forum so hello.

I am currently studying a Computer Science degree and have some linux shell course work to do which is giving me some problems and would appreciate some help.

I am in the process of writing a script that takes 3 input files, sorts these files in alphabetical order and then outputs the last line of each file to another file in reverse alphabetical order.

I have written the code no problem and had it working fine but there are various checks that have to be done before it actually processes the files and creates the output file.

I have all but one check figured out and that is that for every temporary file used in the script a check must be done to make sure that it does not all ready exist in the working directory. If it does exist in the working directory then it adds a number to the file e.g. if my temp file is s.txt but all ready exists then the program should check to see s1.txt exists and so until it finds one that does not exist and uses it as the temp file.

I have various temp files which are: s1.txt, s2.txt, s3.txt, op1.txt, op2.txt, op3.txt, op4.txt and output.txt.

Also when the program has finished the process I would also like to remove all temp files and just leave the output.txt file in the directory. I am unsure of how to do this but some light may be shed once I find out how to do the first part.

Any help would be greatly appreciated. Thank you very much for reading.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-07-2008, 11:48 PM
rockdalinux's Avatar
Contributors
User
 
Join Date: May 2005
Location: Bangalore
My distro: RHEL, HP-UX, Solaris, FreeBSD, Ubuntu
Posts: 557
Rep Power: 6
rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough rockdalinux is a jewel in the rough
Default

Welcome to forum!

Use the -f option

Code:
[ -f /etc/fack.file ] && echo "File exists" || echo "Sorry "
In a shell script:

Code:
if [ -f /path/to/a.txt ]; then
 # file exist do something
 echo "File found"
else
  # file does NOT exist do something like give error
  echo "Error file not found"
fi
__________________
Rocky Jr.
You may have my body & soul, but you will never touch my pride!

If you have knowledge, let others light their candles at it.

Certified to work on HP-UX / Sun Solaris / RedHat
Reply With Quote
  #3 (permalink)  
Old 02-19-2008, 09:53 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
My distro: Kubuntu
Posts: 6
Rep Power: 0
seeker082 is on a distinguished road
Default

Sorry I should have been more specific. The code I was looking for was how to get the temp files to increment by 1 until the file is not found.

I thought it would have been a for, while or until loop

e.g.
for (( i=0; i<=100; i++ ))
do
if ! [ -s file$i.txt ]
then
# use this as file name
else
# increment the file name by 1 and try again until a file does not exist
done

basically what I'm trying to represent is that we check to see that file0.txt exists. If it does we then check to see if file1.txt exists and so on until it finds that filen.txt in which case it some code under:
if ! [ -s file$1.txt ]
then
# use this as file name and execute next code
sort -r file$i.txt

but it needs to do this for multiple temporary files.

Thanks for your help.

Last edited by seeker082; 02-19-2008 at 09:55 PM. Reason: Mistake in code I wrote
Reply With Quote
  #4 (permalink)  
Old 02-19-2008, 10:04 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
My distro: Kubuntu
Posts: 6
Rep Power: 0
seeker082 is on a distinguished road
Default

I have written the rest of the code. It is just this bit I cannot figure out. I can send the code privately to anyone who is willing to look at it and try and get it to work.

Thanks.
Reply With Quote
  #5 (permalink)  
Old 02-21-2008, 09:39 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Can't you use mktemp(1) ? You can avoid the loop then.

[1] mktemp(1): make temporary filename - Linux man page
Reply With Quote
  #6 (permalink)  
Old 02-21-2008, 10:21 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
My distro: Kubuntu
Posts: 6
Rep Power: 0
seeker082 is on a distinguished road
Default

Hi could you be a bit more specific?

I have tried to use the mktemp command with no joy.

The code I have for doing the job is:

sort $1 > tempfile1.txt
sort $2 > tempfile2.txt
sort $3 > tempfile3.txt

tail -n 1 tempfile1.txt > tempfile4.txt
tail -n 1 tempfile2.txt > tempfile5.txt
tail -n 1 tempfile3.txt > tempfile6.txt

sort -m tempfile4.txt tempfile5.txt tempfile6.txt > tempfile7.txt

sort -r tempfile7.txt > output.txt

Thanks.
Reply With Quote
  #7 (permalink)  
Old 02-22-2008, 12:39 AM
agn agn is offline
Member
User
 
Join Date: Feb 2008
My distro: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Rep Power: 1
agn is on a distinguished road
Default

Quote:
Originally Posted by seeker082 View Post

I have all but one check figured out and that is that for every temporary file used in the script a check must be done to make sure that it does not all ready exist in the working directory. If it does exist in the working directory then it adds a number to the file e.g. if my temp file is s.txt but all ready exists then the program should check to see s1.txt exists and so until it finds one that does not exist and uses it as the temp file.
mktemp(1) creates unique files, so you don't have to worry about file existence. And so you can avoid the loop that checks for it. Or you can avoid all those temp files like this assuming I understood your problem :

Code:
for f in $1 $2 $3
do
   sort $f | tail -n 1
done | sort -r > output.txt
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads

Thread Thread Starter Forum Replies Last Post
checking windows file from linux jhn_daz@yahoo.com Getting started tutorials 1 10-31-2007 07:40 PM
Shell script for automatic conversion of files in tar files kasimani Shell scripting 2 02-08-2007 03:45 PM


All times are GMT +5.5. The time now is 05:09 AM.


Powered by vBulletin® Version 3.7.2 - Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36