I need to call a function from a make target, this function would be called multiple time ,
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then
echo "$1" > $(2).txt
else
echo "It is Not Setup";
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
right now i am stuck at this Error:
bash-5.0# make
if [ "" = "ci" ]; then
/bin/sh: syntax error: unexpected end of file (expecting "fi")
make: *** [Makefile:10: all] Error 2
Your function is multiple line, which will try to execute as separate shell invocations. This will fail as any single line is not syntactically correct on its own. You can make it work by setting it up in a single line, i.e.:
$ cat Makefile
define generate_file
if [ "${RQM_SETUP}" = "ci" ]; then \
echo "$1" > $(2).txt; \
else \
echo "It is Not Setup"; \
fi
endef
all:
$(call generate_file,John Doe,101)
$(call generate_file,Peter Pan,102)
Output:
$ make
if [ "" = "ci" ]; then echo "John Doe" > 101.txt; else echo "It is Not Setup"; fi
It is Not Setup
if [ "" = "ci" ]; then echo "Peter Pan" > 102.txt; else echo "It is Not Setup"; fi
It is Not Setup
Related
Here is the correct code, but it is very long
$(shell if [ ! -f .oldmodel ] || [ "$(MODEL)" != `cat .oldmodel` ] ; then echo $(MODEL) > .oldmodel ; fi )
I tried
$(shell if [ ! -f .oldmodel ] || [ "$(MODEL)" != `cat .oldmodel` ] ; \
then echo $(MODEL) > .oldmodel ; fi )
But There is a error
common.mak:21: *** missing separator. Stop.
the strange thing is that the .oldmodel is generated, and it's content is right, the next time call make, no error anymore.
Can anyone explain this.
Looks like you are trying to achieve something in a wrong way. Consider the following solution:
ifeq ($(MODEL),)
MODEL := $(shell cat .oldmodel 2>/dev/null)
endif
stuff_to_do: \.oldmodel
mkdir -p $(MODEL)
\.oldmodel: $(if $(shell echo '$(MODEL)' | cmp $# 2>/dev/null), phony,)
#if [ -z "$(MODEL)" ]; then echo "error: MODEL is not set" && exit 1; fi
echo '$(MODEL)' > $#
clean::
rm -rf $(MODEL)
rm -f .oldmodel
The stuff_to_do is your rule where you use the $(MODEL) variable.
Note, the prerequisites for .oldmodel rule is either a phony or empty depending on equality of .oldmodel file contents and $(MODEL). This phony is taken as some non-existent file/rule, so when phony is there it causes the .oldmodel to be rebuilt.
I am creating a target that can be used as a hook to add some extra procedure when is needed.
This is the code in the main Makefile
export MSG:="Linux hook"
linux-build:
# echo "Dependency target"
linux-build-post: linux-build
# make -s -f linux.hook -q linux_build_post 2> /dev/null ; \
if [ $$? -le 1 ] ; then \
echo "Target exist" ; \
make -s -f linux.hook linux_build_post ; \
else \
echo "Target doesn't exist" ; \
fi
# touch $#
This is the code in the file linux.hook
linux_build_post:
# echo ${MSG}
This code works correctly, but now I am trying to create a template in the main Makefile. For example:
export MSG:="Linux hook"
linux-build:
# echo "Dependency target"
# Common hook target
# Parameters:
# 1: Target name
# 2: Dependecies
# 3: Hook name
define COMMON_HOOK_TARGET
$(1): $(2)
make -s -f $(3).hook -q $(1) 2> /dev/null ; \
if [ $$? -le 1 ] ; then \
echo "Target exist" ; \
make -s -f $(3).hook $(1) ; \
else \
echo "Target doesn't exist" ; \
fi
touch $(1)
endef
$(eval $(call COMMON_HOOK_TARGET,linux-build-post,linux-build))
But in this case the make command fails because $$? is replaced with the dependency linux-build then the if condition is evaluated like this if [ linux-build -le 1 ].
Error reported:
/bin/sh: 1: [: Illegal number: linux-build
How I can change the code in order to use $$? as the exit code of the previous command make -s -f $(3).hook -q $(1) 2> /dev/null ?
I think the answer is actually this:
if [ $$$$? -le 1 ] ; ...
The call to call turns "$$$$" into "$$", then when Make executes the rule it converts "$$?" to "$?" and passes it to the shell.
I am trying to make the simplest comparison within an if condition in a makefile.
I am using cygwin. I keep getting the error XX was unexpected at this time. Where XX is the LHS of the comparison operator. This exact makefile runs on mks, but now I have to use cygwin because I am running it on a 64 bit windows at the moment.
Here is my code:
DEBUG=0
all:
if test $(DEBUG) != 0 then\
echo "not equal to 0"\
fi
I also tried this:
DEBUG=0
all:
if [ $(DEBUG) != 0 ] then\
echo "not equal to 0"\
fi
and this:
DEBUG=0
all:
if [ "$(DEBUG)" != "0" ] then\
echo "not equal to 0"\
fi
This is the error:
0 was unexpected at this time. *** [all] Error 255
====================================================
Updates:
I have tried adding the missing ; and also making it a one liner as #Jonas. suggested but still getting the same problem.
One Liner:
if [ $(DEBUG) != 0 ] ; then echo "not equal to 0" ; fi
With semicolons:
if [ $(DEBUG) != 0 ] ; then \
echo "not equal to 0" ; \
fi
And yes I am using tabs not spaces. And I have changed the line endings to LF only.
You need two ;. Without the last semicolon then fi is a parameter to echo. It works with: GNU Make 4.0 on Linux debian 3.16.0-4-amd64.
Note: There may be issues when using older versions of GNU Make.
DEBUG=1
all:
if [ $(DEBUG) != 0 ]; then \
echo "not equal to 0" ; \
fi
I have the following humble zsh function:
function remember()
{
if [ "$1" != "" ]
then
if[ "$1" != "clean" ]
then
echo "Why";
#echo $1 >> {~/.remember_info};
else
rm -r ~/.remember_info;
touch ~/.remember_info;
fi
else
cat .remember_info;
fi
}
When I try to source it I get:
parse error near `echo' (The echo being the line with echo "Why";)
The error is quite non descriptive and I assume its related to part of the loop's logic (since no matter what instruction I give after then it error out there).
Is there any way to "debug" this kind of thing ? zsh -n doesn't help much (at all)
You forgot the space between if and [ when comparing to clean.
This is case, though, where your function can be made simpler by handling the = case first.
function remember()
{
if [ "$1" = "" ]; then
cat ~/.remember_info
elif [ "$1" = clean ]; then
rm -r ~/.remember_info
touch ~/.remember_info
else
echo "$1" >> ~/.remember_info;
fi
}
Or, use a case statement.
remember () {
f=~/.remember_info
case $1 in
"")
cat "$f"
;;
clean)
rm -r "$f"
touch "$f"
;;
*)
print "$1" >> "$f"
;;
esac
}
You are missing a whitespace after [. It should be:
function remember()
{
if [ "$1" != "" ]
then
if [ "$1" != "clean" ]
then
echo "Why";
#echo $1 >> {~/.remember_info};
else
rm -r ~/.remember_info;
touch ~/.remember_info;
fi
else
cat .remember_info;
fi
}
[ is the same as test. It is a separate command, described in man test:
TEST(1)
NAME
test - check file types and compare values
SYNOPSIS
test EXPRESSION
test
[ EXPRESSION ]
[ ]
[ OPTION
The goal is to create a simple trash utility using a Bourne shell (it's part of an assignment). I am receiving the following error: "line 17: Syntax Error: Unexpected end of file"
I have been staring at the code for a few hours now and I can't see the mistake (probably something simple I am overlooking)
#!/bin/sh
if [$# == 0] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if $1 == '-l'; then
dir $HOME/.trash
else if $1=='-p'; then
rm $HOME/.trash/*
else
for i in ${} ;do
mv i $HOME/.trash
done
fi
fi
Thanks!
This is what I achieved using shellcheck:
#!/bin/sh
if [ $# -eq 0 ] ;then
echo "Usage: trash -l | -p | { filename }*"
else
if [ "$1" = '-l' ]; then
dir "$HOME"/.trash
elif "$1"=='-p'; then
rm "$HOME"/.trash/*
else
for i in ${} ;do
mv "$i" "$HOME"/.trash
done
fi