simplest bash string comparison in makefile is failing [duplicate] - string

This question already has answers here:
Why am i getting an unexpected operator error in bash string equality test? [duplicate]
(2 answers)
Bash scripting unexpected operator
(1 answer)
Closed 3 years ago.
My makefile is as follows:
test:
if [ "a" == "a" ]; then echo "hooray"; fi
running make test yields the following error:
/bin/sh: 1: [: a: unexpected operator
(Note, if I change the conditional to "a" == "b", the error still refers to "a", so the "problem" is (at least) with the first "a").
I feel like I must be missing something silly, but I cannot get this to work, and can't think of any way to further simplify the problem.
I'm on a raspberry pi (raspbian), running the default/latest everything (apt-get update && apt-get upgrade daily).

Gah. The clue is /bin/sh. apparently make doesn't use bash as its shell under my configuration? very strange...
Anyways, == isn't POSIX sh. Even this cheat sheet: https://www.joedog.org/articles-cheat-sheet/ gets it wrong (thank you Rebecca Cran, who commented as such a year ago...)
To be clear- the solution is to use a single =, as in:
test:
if [ "a" = "a" ]; then echo "hooray"; fi

Related

Why is bash string comparator only working when given substring? [duplicate]

This question already has answers here:
Case insensitive comparison of strings in shell script
(14 answers)
Closed 1 year ago.
I am trying to add users from a csv file to a group in a bash script running on CentOS 8. The group names are "Faculty" and "Students", which I am forcing them to be lowercase. The following did not work. It defaults to the "else" clause, even when $groupName is "Faculty" (I would "echo" before the if statement).
if [ "$groupName" = "Faculty" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
However, it worked when I gave it a substring of only the capital letter:
if [ "${groupName:0:1}" = "F" ]
then
goodGroup="faculty"
else
goodGroup="student"
fi
Using the second method gives me the outcome I need, I am just curious why the first bit of code did NOT work. All the answers I've seen on StackOverflow say that's the syntax for comparing strings, so I can't see what I'm doing wrong.
Ways to force a variables value to lowercase in bash without having to check for specific values:
#!/usr/bin/env bash
# Using declare to give a variable the lowercase attribute
declare -l groupName1
groupName1=Faculty
printf "%s\n" "$groupName1"
# Using parameter expansion
groupName2=FACULTY
printf "%s\n" "${groupName2,,}" # or "${groupName2#L}"

How do I correctly read in a file with sh scripting and using it in an if statement? [duplicate]

This question already has answers here:
Assignment of variables with space after the (=) sign?
(4 answers)
Assing a variable and use it inside of if statement shell scripting
(2 answers)
Difference between sh and Bash
(11 answers)
Closed 3 years ago.
So there is a kernel adiutor for android, that let's you add custom controls with shell scripting. I'm trying to add a switch, but I have trouble setting the switch up correctly, when it's active, and when it's not. So there is a (text)file I'm trying to read (you will see it in the code), whether it's 0 or 1 inside, and that determines the switch on-off state.
I've tried with cat, read, everything, but honestly I think the problem is that I'm not familiar with sh scripting, and there is a problem with my syntax. Sometimes the script won't return anything when using cat. Also, su is available so that's not a problem, also the file has the correct permissions.
#!/system/bin/sh
var= $(<sys/class/lcd/panel/mdnie/hdr)
if ( "$var" = 0) then
echo 0
else echo 1
fi
The problem with my code is that right now it returns 1 (on), even when the file has a 0.
When assigning a variable in shell, there must be no space after the assignment sign. Also, make sure you use the correct syntax for conditions (and be aware of whitespace sensitivity):
var=$(cat sys/class/lcd/panel/mdnie/hdr)
if [ "$var" = "0" ]; then
# if [ "$var" -eq 0 ], if you want numeric comparison (won't really matter here)
echo 0
else
echo 1
fi

What do z${variable} and zfalse in bash script mean? [duplicate]

This question already has answers here:
Why do shell script comparisons often use x$VAR = xyes?
(7 answers)
Test for empty string with X"" [duplicate]
(2 answers)
Portable way to check emptyness of a shell variable [duplicate]
(5 answers)
Closed 4 years ago.
I have an existing script in my Linux host with these statements:
local variable=$1
if [ "z${variable}" != "zfalse" ]; then
local flag="--some_flag"
fi
I haven't found an explanation of these "z${variable}" and "zfalse" notation or syntax in my Shell Scripting book. Hope someone can help explain what they mean. Thanks in advance.
'z' is there to prevent syntax error in case ${variable} evaluates to nothing. If your user does not provide $1 parameter, ${variable} is empty, and without 'z', the if condition would look something like
if [ != false ] ...
which is syntactically incorrect. With 'z', it becomes:
if [ z != zfalse ] ...

Linux batch: create multi folder by combine name with serial number [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I'm try to create multi folder by combine string and counter. I don't why what is the wrong with my code:
echo 'Start'
let count=0
for p in {1..10}
do
DirName= "dir"
NUM = "${DirName}${count}"
let count++
mkdir $NUM
mkdir "$NUM"/decoded
done
I got this kind of error
./test.sh: line 6: dir: command not found
./test.sh: line 7: NUM: command not found
thank in advance
No need to use a loop here. The shell will do all the necessary expansion for you. In fact, you're already relying on the shell to expand {1..10} for you as part of your for loop. So you can just use that expansion directly with mkdir. Also by using mkdir -p <path> (make parent directories as needed), you can avoid having to first do mkdir $NUM before doing mkdir $NUM/decoded.
Putting it all together, you can do what you need in a single line:
mkdir -p dir{1..10}/decoded
Edit: To answer your question more directly regarding the command not found errors, it looks like (as Bjorn A. mentioned) you just need to get rid of the spaces before and after the = in your variable assignments.
You cannot have spaces around the assignment operator in bash. Lines 6 and 7 must look like:
DirName="dir"
NUM="${DirName}${count}"

Adding one or two directories to PATH variable in linux ( Using Bash script) [duplicate]

This question already has answers here:
Check number of arguments passed to a Bash script
(10 answers)
Add a bash script to path
(5 answers)
Closed 5 years ago.
So far
I have the following code:
#!/bin/bash
echo "Adding new path...."
if [[$# -eq1] || [$# -eq2]]
then
if [$# -eq2]
then
export PATH=$PATH:/$1:/$2
fi
if [$# -eq1]
then
export PATH=$PATH:/$1
fi
else echo "Incorrect number of parameters. No more than two directories can be added at once."
fi
echo $PATH
exit 0
When I run this script passing it one parameter i get an error:
"./addDir: line 3: [[1: command not found
./addDir: line 3: [1: command not found "
when I run it with 2 parameters instead of "1" it says "2"
What's going on?
You're missing some spaces. Basically, if you're trying to use the [...] construction, you need to have spaces before and after each bracket - think of [ as being the name of a command, in the same way as echo, and ] as being an argument to that command. (In fact, there might actually be a /bin/[ program on your system.) Just as you can't type echofoo and expect it to run the echo program, similarly you can't type [[$# if you expect it to run [.
In your case, you'd need to do things like
if [ $# -eq 2 ]; ...
And for the compound test you're doing in line 3, I don't think you can use [ and ] within the test. In other words, don't use those brackets for grouping; it has to be [ something ] where the something doesn't contain any brackets. Read the relevant section of the bash man page for the full details of what you can put there.
There is also a shell construct [[ ... ]] which does basically the same thing but has different syntax. You could use that instead, but be aware that it's very different from [ ... ].

Resources