Solved

Unix Makefile

Posted on 2004-08-12
20
1,595 Views
Last Modified: 2013-12-26
How to assign value to a variable on given condition in unix Makefile. for eg
if a>10
then a =1
else
a = a+1

thanx
0
Comment
Question by:princyrajan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 4
  • +3
20 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 11781872
0
 
LVL 11

Expert Comment

by:avizit
ID: 11782185
Though it seems you can have only the following four conditons which can be checked

1) ifeq
2) ifneq
3) ifdef and
4) ifndef

so you cannot do a check like a>10  

/abhijit/
0
 
LVL 6

Expert Comment

by:Talmash
ID: 11783293
if a>10
then a =1
else
a = a+1

use :

if ($a > 10) then
       set a = 1
else
      @ a+=1
endif

1) please take care of the spaces as exactly as I wrote .
2) at the head of the file write :
   #! /bin/csh -f
3) chmod 755 file (to enable execution)

tal
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Assisted Solution

by:chris_calabrese
chris_calabrese earned 50 total points
ID: 11784317
I'm pretty sure talmash is pulling your leg here. But this really isn't the sort of thing that Make is good at, or even intended for.

It _is_ possible to set _static_ variables at the beginning of a makefile, and you can reference them from inside the shell-code action-bits like this:
  a="fubar"
  b="bozo"

  b: $(a)
        if [ -d "$(a)" ]; then mkdir "$(b)"; else touch "$(b)"; fi

But if you need anything more complex, then your options are either to not use Make at all and use a general-purpose scripting language (Perl and shell being the obvious choices), or to use a script to generate a makefile.
0
 
LVL 48

Expert Comment

by:Tintin
ID: 11788707
Can you give us some details on why you need to do this.  There may well be a better way of achieving what you want.
0
 

Author Comment

by:princyrajan
ID: 11790649
I am having a Makefile in which the library path have to be set (after checking
condition) according to the machine for eg: AIX or Solaris

thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11790666
In that case  I think you can use the

"ifeq" or "ifneq" conditionals ..

0
 

Author Comment

by:princyrajan
ID: 11790935
VERSION      =
ifeq ($(VERSION),DEBUG)
VERSION =-g
endif
------------------
but, in this case if I assign a value for VERSION in command line (while make -ing a file) as DEBUG, the if condition's true part doesn't get executes. why?

thanx
0
 
LVL 11

Assisted Solution

by:avizit
avizit earned 200 total points
ID: 11791557
I tried the following example ,seems to work for me

hello.c
-------------
#include <stdio.h>

int main(){
     printf("hello\n");
     return 0;
}

Makefile
-------------
CC=gcc

ifeq ($(TEST),gee)
        CFLAGS=-g
else
        CFLAGS=-pg
endif

hello:hello.c
        $(CC) $(CFLAGS) -o hello hello.c
=================================


now i can give the following two make commands and the results are shown too,

$ make TEST=gee
gcc -g -o hello hello.c

$ make TEST=nogee
gcc -pg -o hello hello.c

guess thats what you wanted.

btw maybe in yuor example you can just pass the version thing on the command line and you need not have to use conditional for the makefile

as in

make VERSION=-g


/abhijit/
0
 

Author Comment

by:princyrajan
ID: 11792391
Hi,
I used, (AIX is my machine)

OSNAME=`uname`
ifeq ($(OSNAME),AIX)
..
..
endif

in my makefile, but the true part doesn't get executes.

Thanx
0
 
LVL 11

Expert Comment

by:avizit
ID: 11792444
i am not sure  if OSNAME=`uname`  is allowed within a makefile

but what you want can be achieved by


removing the line  OSNAME=`uname`  from the makefile
and putting the following in the command like

make OSNAME=`uname`


0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 250 total points
ID: 11796539
OSNAME=`uname -s`
AIX-PATH=path/AIX
SunOS-PATH=path/Solaris
VERSION=
g-VERSION=-g
here-we-go:
        @echo path=$($(OSNAME)-PATH)
        @echo opt=$($(DEBUG)-VERSION)
# end-of-Makefile

# don't forget the tabs in the target, then it can be improved inmany ways
# you can test with:
  make
  env DEBUG=g make
0
 

Author Comment

by:princyrajan
ID: 11808718
No my friend its not working for me. Give me some other eg:

Thanx
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11808803
which part does not work?
if unshure try using -d and/or -dd option
0
 

Author Comment

by:princyrajan
ID: 11817541
"echo opt" is getting printed but not "echo path" (OSNAME).

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818337
please post result verbatim (drag&drop)
but I assume that uname -s returns something different
please adapt the "AIX"-PATH part in
  AIX-PATH=path/AIX
to what uname -s returns (must be exact string literal, case-sensitiv!)
0
 

Author Comment

by:princyrajan
ID: 11818593
In my Machine "uname -s" returns AIX.
Generally 'uname' gets executed and its value get stored in variable in shell script but it is not working in Makefiles.

If I execute ur code it giives o/p as follows
o/p:
path=
opt=-g


-Thanks a lot
0
 
LVL 11

Expert Comment

by:avizit
ID: 11818703
can you just invoke make as

 make OSNAME=`uname`

0
 

Author Comment

by:princyrajan
ID: 11818902
In my project I am just going to include this makefile. So its not possible to invoke as "make OSNAME =`uname`

Thanks
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 11818939
is this AIX's bundled make ?
then please check man make or other docs how to use shell inside makefile (sorry, don't have AIX handy)
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 56
format the code in java 6 117
I could not set window to top 4 63
Problem to Office 1 67
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question