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
Related
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
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.
This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
Closed 5 years ago.
I am creating a script where the user must provide the first argument that is mandatory and the second argument is optional. Should throw an error if it is less than 1 or bigger than 2 arguments.
This is what I did so far:
if [ $# -eq 0 -o $# -gt 2]
then
echo " *** ! No arguments were supplied. *** !"
echo " Usage example is: sudo myserver pathToYourFolder [URL]"
echo ""
echo " The first argument 'pathToYourFolder' is mandatory.
It is the path to your mysite folder.
Please use like this example: sudo myserver /Users/jhon/Documents/mysite"
echo ""
echo " The second argument 'URL' is optional. It shuld be the desired URL to run with the server in Docker.
If not provided the default will be 'my-dev.com'.
If you want to set yours, please use llike this example: my-url.com.
Please note: It will be recorded in your /etc/hosts."
echo ""
else
if [ $1 -eq 0 ]
then
FULLPATH="/Users/jhon/Documents/mysite"
else
FULLPATH="$1"
fi
if [ $2 -eq 0 ]
then
DOMAIN="my-dev.com"
else
DOMAIN="$2"
fi
echo ""
echo "Sit path to $FULLPATH"
echo ""
echo "HTTP_PORT to $HTTP_PORT"
echo ""
echo "Sit domain to $DOMAIN"
echo ""
if ! grep -lq $DOMAIN /etc/hosts;
then
echo "127.0.0.1 $DOMAIN" >> /etc/hosts
echo "$DOMAIN saved in /etc/hosts"
else
echo "$DOMAIN already in /etc/hosts"
fi
echo ""
echo ""
docker run -p 80:80 -v $FULLPATH:/var/www/html myorg/srv_php56:v001
fi
The issue is in the first conditional if [ $# -eq 0 -o $# -gt 2]. The condition is if arguments is zero OR great than 2 throw the error.
The error message I get is
./myserver.sh: line 32: [: missing `]'
./myserver.sh: line 50: [: -eq: unary operator expected
./myserver.sh: line 58: [: -eq: unary operator expected
What am I doing wrong?
Go get a space here:
if [ $# -eq 0 -o $# -gt 2 ]
^
Bash separates arguments by whitespaces, specifically, spaces and tabs. It will be treated as a single argument if written as 2].
So I'm writing a bash shell script, and my first few lines looks like this:
if ! [ $# -eq 0 || $# -eq 1 ]; then
echo -e "Usage: myScriptName [\e[3mdir\e[0m] [\e[3m-f file\e[0m]"
exit 1
fi
But when I run it, it says "[: missing `]'". I don't see a missing ], and nothing except the ; is touching the ], so what am I missing?
You cannot use operators like || within single-brace test expressions. You must either do
! [[ $# -eq 0 || $# -eq 1 ]]
or
! { [ $# -eq 0 ] || [ $# -eq 1 ]; }
or
! [ $# -eq 0 -o $# -eq 1 ]
The double-brace keyword is a bash expression, and will not work with other POSIX shells, but it has some benefits, as well, such as being able to do these kinds of operations more readably.
Of course, there are a lot of ways to test the number of arguments passed. The mere existence of $2 will answer your question, as well.
In my case I got this error with the following:
if [ $# -eq 1]; then
Notice that there is no space between the 1 and the ]. Adding a space fixed the error.
In some cases this error happens even if everything looks fine as #kojiro mentioned above. in such cases a simple and proper line-break will help. if-statement where you are checking with || should have a line-break from it's prior-line of code.
I am new in shell script, trying to catch the return value of a program, and do something with it.
I have this script below
#!/bin/sh
if [ $# !=2 ] ; then
echo "Usage : param1 param2 "
exit 1;
elif [ $# -eq 2 ]; then
./callprogram
$out = $?
echo "$out"
fi
if [ $out==0 ]; then
echo "out ok"
fi
It keeps getting me error of
"[: 11: 0: unexpected operator
out ok
I have no clue why line 11 is wrong. if I remove "fi", it will promt that it needs "fi". Can anyone help with this matter?
Thank you
You need a space after the [ and you need to use -eq (equals) or -ne (not equals) to compare numbers in your if-statement.
To assign a variable use out=$?, not $out = $?. There should be no spaces on either side of the = sign.
Try this:
if [ $# -ne 2 ] ; then
echo "Usage : param1 param2 "
exit 1
elif [ $# -eq 2 ]; then
./callprogram
out=$?
echo "$out"
fi
if [ $out -eq 0 ]; then
echo "out ok"
fi
Change:
if [ $out==0 ]; then
to:
if [ $out = 0 ]; then
add spaces, and change '==' to '='. Note, that bash, executed as a bash accepts ==. But if you run is as a sh it will say "unexpected operator".
Why:
The [ is a command (or symlink to test binary, depending on your OS and shell). It expects $out and == and 0 and ] to be separate command arguments. If you miss the space around them, you have one argument $out==0.
BTW:
It's safer to always enquote the variables like that:
if [ "$var" ......
instead of
if [ $var
because when variable is empty, then you can get another error because of wrong number of arguments (no argument instead of empty string).
You have several problems. The one that is giving you the error is that you need a space after != on
if [ $# != 2 ]
(although -ne would be better than !=). It appears that you are calling the script with 11 arguments, and then calling [ with the arguments 11 !=2, and it does not know what to do with !=2 because you meant != 2 but forgot the space. Also, you want
out=$?
on the assignment (no $ on the LHS)
and
if [ $out = 0 ]
on the comparison. (Spaces around the operator, which is '=' instead of '=='. '==' will work on many shells, but '=' works in more shells.)
But your script would be better written without the explicit reference to $?
#!/bin/sh
if test $# != 2; then
echo "Usage: $0 param1 param2 " >&2 # Errors go to stderr, not stdout
exit 1;
fi
# you know $# is 2 here. No need to check
if ./callprogram; then
echo "out ok"
fi