MakeFile Backup Target


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting MakeFile Backup Target
# 1  
Old 08-11-2011
Question MakeFile Backup Target

Goal: I'm trying to create a PHONY target inside my Makefile so that when I run the command "make backup",
It will move all the files that end in "~" into the specified backup folder.

Here is my code currently, and I'll explain the problem after:
Code:
.PHONY: backup
backup:
    @mkdir -p ./backup/include    #make folder (don't complain if it already exists)
    @mkdir -p ./backup/src        #make folder (don't complain if it already exists)
    @mv -fu *.h~ ./backup/include #move header file backups into desired folder
    @mv -fu *.cpp~ ./backup/src   #move source file backups into desired folder

Problem: This works great, BUT ONLY if a file "*.h~" and "*.cpp~" file already exist in Makefile's current directory. I would like this target to move the files if it can, and just be quiet if there are no files to move..

Error Message: mv: cannot stat `*.h~': No such file or directory

*I'm on linux(ubuntu) and so my shell is "bash"

Appreciate the help, I've been stuck on this all night trying all kinds of commands from if statements to the "find" command. Just thought I'd finally ask for some assistance Image
# 2  
Old 08-12-2011
This might do the trick:

Code:
    @mv -fu *.h~ ./backup/include/ >/dev/null 2>&1 || true

The redirection tosses any errors away, and the ||true causes any error return value from mv to be ignored. From make's perspective, the command is always successful because of the addition of the true statement.

The risk here, is that if the files exist, and cannot legitimately be moved, you'll not see the errors. If you're concerned about that, then this should also work though it might be less efficient, but I don't think you'll notice.

Code:
@if  ls *.h~ >/dev/null 2>&1; then mv -fu *.h~ backup/include/; fi

Again redirection hides any errors from ls. This time the status of mv, will be noticed by make, but there won't be any 'false alarms' because mv won't be invoked unless those files exist. If the true branch of the if isn't taken, make should see the command as having been successful.
# 3  
Old 08-12-2011
Beautiful! thanks for the great answer
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Makefile No rule to make target

I am trying to create a makefile to build a program and am getting the following error: make -f tsimplex.mk make: *** No rule to make target `/main/tsimplex_main.cpp', needed by `tsimplex_main.o'. Stop. OPSYS = $(shell uname -s ) TARGET = tsimplex ROOTDIR = ../../.. GTSDIR =... (1 Reply)
Discussion started by: kristinu
1 Replies

2. HP-UX

After adding new iscsi target port, still the session state of that target port is showing offline

Hi, I wanted to configure new iscsi port on HPUX system, i added the target port address and configured it, once done, went to array side and searched for that host iqn number , but was nt able to find the same, came to host, then when i ran "iscsiutil -pVS" command it gave me below result ... (0 Replies)
Discussion started by: Vinay Kumar D
0 Replies

3. Programming

How to define dynamic Target/Prerequisite in Makefile?

I am writing a Makefile for Cobol on Linux.My sample Makefile looks like below. I do not want to Hardcode Program names in Makefile.Is there any way we can mention directories in Target and Prerequisites instead of File names sothat Makefile Pickup all the files in mentioned path as below.... (8 Replies)
Discussion started by: kiranksb
8 Replies

4. Homework & Coursework Questions

Help with Simple Multi-Level Makefile (Extremely New at Makefile)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Basically, the prompt is make a makefile with various sub makefiles in their respective subdirectories. All code... (1 Reply)
Discussion started by: Tatl
1 Replies

5. Solaris

Setting/Modifying variable specific to target in Makefile

Hi, I have a small piece of Makefile script which throw's error on Sun Sparc machine, but works fine with Sun Optron, Linux, AIX machines. FOO=Naveen test1:FOO=Dhilip test1: @echo FOO is ${FOO} test2: @echo Me is ${FOO} Output on Sun Sparc - ukhml-v890new-~/test: make test1... (5 Replies)
Discussion started by: nsriram
5 Replies

6. Programming

Makefile: multiple target variable substitution

Greetings! Basically, I would like to properly handle this with gnu make: alltools: my_tool mysecond_tool mythird_tool etc_tool %_tool: dir1/%_tool.vf dir2/%_tool/subdir2/%_tool.ver <tab>@echo done %.vf: <tab>RUN_VF $* %.ver: <tab>RUN_VER $* So, if I were to do something like:... (0 Replies)
Discussion started by: Harlinator
0 Replies

7. UNIX for Advanced & Expert Users

Makefile problem - How to run module load in a Makefile

Hi, I'm trying to run the module load command in a Makefile and i'm getting the following error: make: module: command not found Why is this? Is there any way to run this command in a Makefile? NOTE: command - module load msjava/sunjdk/1.5.0 works fine outside of the Makefile (2 Replies)
Discussion started by: hernandinho
2 Replies

8. Shell Programming and Scripting

Makefile debug target

Hello, I'm having a problem with a makefile script i'm trying to write. I want to compile a C++ program in two possible ways: "make" (default target, main) and "make debug". When i supply the debug target, exactly the same as main should be built, but then with the DEBUG flag (-g -D... (2 Replies)
Discussion started by: StevenR
2 Replies

9. HP-UX

Oracle 9i install: Error in invoking target install of makefile

I receive an error while installing Oracle 9i: Error in invoking target install of makefile /opt/oracle/product/9.2.0/sqlplus/lib/ins_sqlplus.mk Furthermore: $ whoami oracle $ echo $ORACLE_HOME /opt/oracle/product/9.2.0 $ pwd /opt/oracle/product/9.2.0/sqlplus/lib $ ll total... (5 Replies)
Discussion started by: chris2005
5 Replies

10. Solaris

Makefile:57: *** multiple target patterns. Stop.

Hi, I am getting the following error while building on Solaris 64 , while I am trying to build. Error Snippet :- ---------------------- Makefile:57: *** multiple target patterns. Stop. make: Leaving directory `/work1/patch/vds6053sun64o/vobs/jvi' make: *** Error 2 make: Leaving directory... (0 Replies)
Discussion started by: nileshborate
0 Replies
Login or Register to Ask a Question