AnswerBun.com

Can I define functions and global variables within debian rules make file?

Unix & Linux Asked by Veverke on December 15, 2020

Can I define functions and global variables within a debian rules make file, to be used within the different override_**** sections ?

Have not had success in doing so.

For example, the following is an excerpt of one of my script files. I would like to use this function and global variables within my debian rules file as well, throughout the override sections.

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
BLUE='33[1;34m'
GREEN='33[1;32m'
YELLOW='33[1;33m'

#return code
rc=999

######################### Functions #############################
function logCommandExecution()
{
    commandName=$1
    exitCode=$2
    #echo 'command name: '${commandName}' exitCode: '${exitCode}
    if [ ${exitCode} -eq 0 ] 
    then
        printf "${GREEN}${commandName}' completed successfully${NC}n"
    else 
        printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
        exit ${exitCode}
    fi
}

One Answer

The debian/rules file is a makefile, not a sh file.

I stuck your function into a makefile to try it out:

#!/usr/bin/make -f

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
GREEN='33[1;32m'

######################### Functions #############################
function logCommandExecution()
{
    commandName=$1
    exitCode=$2
    #echo 'command name: '${commandName}' exitCode: '${exitCode}
    if [ ${exitCode} -eq 0 ] 
    then
        printf "${GREEN}${commandName}' completed successfully${NC}n"
    else 
        printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
        exit ${exitCode}
    fi
}


all:
        logCommandExecution Passcmd 0
        logCommandExecution Failcmd 1

Then wehen I try to execute this I get:

$ make all
makefile:14: *** missing separator.  Stop.

So the answer is not directly. However there are some ways to run shell syntax in makefiles.
This answer might help with that.

I think the easiest way is to put the function in another file, and call it from debian/rules:

makefile:

#!/usr/bin/make -f
all:
        ./logCommandExecution Passcmd 0
        ./logCommandExecution Failcmd 1

logCommandExecution:

#!/bin/sh

# console output colors
NC='33[0m' # No Color
RED='33[1;31m'
GREEN='33[1;32m'

commandName=$1
exitCode=$2
#echo 'command name: '${commandName}' exitCode: '${exitCode}
if [ ${exitCode} -eq 0 ] 
then
    printf "${GREEN}${commandName}' completed successfully${NC}n"
else 
    printf "${RED}${commandName} failed with error code [${exitCode}]${NC}n"
    exit ${exitCode}
fi

Now when I make it, I get:

$ make
./logCommandExecution Passcmd 0
Passcmd' completed successfully
./logCommandExecution Failcmd 1
Failcmd failed with error code [1]
make: *** [makefile:5: all] Error 1

Answered by Stewart on December 15, 2020

Add your own answers!

Related Questions

Linux kernel.panic not working as expected

0  Asked on November 11, 2021 by holis

     

Is knowing Linux required to learn Unix?

4  Asked on November 9, 2021

   

How can I change the default gateway?

7  Asked on November 9, 2021 by www

   

wget – how to reject string from downloading html pages

2  Asked on November 9, 2021 by speld_rwong

 

Unable to manually bind driver in Linux

0  Asked on November 9, 2021 by nzaegik

   

Three monitors: xrandr configure crtc failed

1  Asked on November 9, 2021 by cash-weaver

       

Tool to play back pipe including timing

3  Asked on November 9, 2021

   

How to add 2 column values and find out percentage value?

1  Asked on November 9, 2021 by user3559780

     

Default username when using TTY login

1  Asked on November 9, 2021 by thekarlo95

       

ip is assigned, local netowrk works but the external network is unreachable

1  Asked on November 9, 2021 by arghavan-mohammadhassani

       

Issue with an update on fedora 32

2  Asked on November 9, 2021

 

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP