nixCraft Linux Forum

nixCraft

Linux / UNIX 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

Linux answers from nixCraft.


Shell scripting You can discuss the shell scripting, request shell scripts and scripting techniques

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-02-2008, 11:56 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
OS: Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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
  #2 (permalink)  
Old 07-02-2008, 11:48 PM
rockdalinux's Avatar
Is that all you got?
User
 
Join Date: May 2005
Location: Planet Vegeta
OS: Redhat
Posts: 708
Thanks: 15
Thanked 19 Times in 18 Posts
Rep Power: 10
rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light rockdalinux is a glorious beacon of light
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.
What's wrong? I hope I am not making you uncomfortable...

Never send a boy to do a mans job.
Reply With Quote
  #3 (permalink)  
Old 19-02-2008, 09:53 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
OS: Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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; 19-02-2008 at 09:55 PM. Reason: Mistake in code I wrote
Reply With Quote
  #4 (permalink)  
Old 19-02-2008, 10:04 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
OS: Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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 21-02-2008, 09:39 PM
agn agn is offline
Member
User
 
Join Date: Feb 2008
OS: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
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 21-02-2008, 10:21 PM
seeker082's Avatar
Junior Member
User
 
Join Date: Feb 2008
OS: Kubuntu
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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 22-02-2008, 12:39 AM
agn agn is offline
Member
User
 
Join Date: Feb 2008
OS: OpenBSD/FreeBSD/Debian/Fedora/RHEL
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Rep Power: 3
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


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 Off


Similar Threads

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


All times are GMT +5.5. The time now is 06:39 AM.


Powered by vBulletin® Version 3.8.5 - Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2
©2005-2010 nixCraft. All rights reserved

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 37 38