LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-21-2008, 05:40 AM   #1
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Rep: Reputation: 15
Smile Makefile:condition to check whether a file exists ?


I am making a Makefile there i have made three files text11.o ,functext.o
and result.o.
Now i have written a make clean target to rm these files if these extst but
for better programming it should first check it that the file exists or not
i have written below mentioned code :

clean:
ifeq ($(findstring text11.o,$(wildcard %.o)), text11.o)
rm text11.o
else
echo "no file named text11"
endif
ifeq ($(findstring functext.o, $(wildcard %.o)), functext.o)
rm functext.o
else
echo "no file named functext"
endif
ifeq ($(findstring result.o,$(wildcard $(PWD)/%.o)), result.o)
rm result.o
else
echo "no file named result"



Now the problem is that interpreter is always giving the else part whether or not files are there.
what is the right code ?
 
Old 01-21-2008, 06:56 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,511

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
'rm --help' will show you the options.
(I think that 'rm -f *.o' will do the job.)
 
Old 01-22-2008, 03:58 AM   #3
Ashok_mittal
Member
 
Registered: Dec 2007
Posts: 73

Original Poster
Rep: Reputation: 15
Unhappy

Quote:
Originally Posted by knudfl View Post
'rm --help' will show you the options.
(I think that 'rm -f *.o' will do the job.)

I got the desired result by doing this ---------->



clean:
ifeq ($(findstring text11.o,$(wildcard *.o)), )
echo "no file named text11"
else
rm text11.o
endif
ifeq ($(findstring functext.o, $(wildcard *.o)), )
echo "no file named functext"
else
rm functext.o
endif
ifeq ($(findstring result.o, $(wildcard *.o)), )
echo "no file named result"
else
rm result.o
endif

#####################
But i am getting the result when rm filename is in else part but if i compare the $(findstring text11.o,$(wildcard *.o)) function with text11.o
like:
ifeq ($(findstring text11.o,$(wildcard *.o)), text11.o)
and put rm text11.o in true part not in else part.
it is always giving empty string if the file text11.o is present there or not or in other words it is always executing else part.
so what is the reason ?
 
Old 04-28-2010, 07:06 AM   #4
apjena
LQ Newbie
 
Registered: Nov 2009
Posts: 4

Rep: Reputation: 0
You could Try Something Like :

clean:
@for f in a $(VAR) $(OBJ) file.o; do \
if [ -f $$f ]; then \
rm "$$f"; \
echo "rm $$f" ; \
fi; done

Last edited by apjena; 04-28-2010 at 07:08 AM.
 
Old 12-06-2011, 07:52 PM   #5
Ajay Kumar
LQ Newbie
 
Registered: Jul 2008
Posts: 8

Rep: Reputation: 0
Hi Ashok_mittal, you may want to use the 'filter' function instead of 'findstring', otherwise use 'strip' over 'findstring'.

In your case the comparison of output from findstring with empty string is failing. A $(strip ) over your $(findstring ) may fix the problem as that would truncate the white spaces. I didn't try that as I could achieve my purpose using $(filter ) function.

I had a similar scenario where I had to check if file exists (inside the recipe) before executing actual command. I was doing that using shell commands. I tried your machanism and ran into the issues that you have mentioned. Then I used 'filter' instead of 'findstring' and that worked absolutely fine.

Last edited by Ajay Kumar; 12-06-2011 at 07:54 PM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script problem. check file already exists sinister1 Linux - Server 8 11-20-2007 03:13 PM
ksh check if file exists (using wildcard) problem r18044 Linux - Newbie 5 02-22-2005 07:52 AM
SIOCADDRT: File exists SIOCCADDRT: File Exists Failed to bring up eth0. opsraja Linux - Networking 0 01-10-2005 08:29 AM
c++ check if file exists Genjix Programming 3 03-15-2004 12:08 AM
Makefile and Zone-file-check michedlp Programming 0 02-18-2004 02:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration