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 ] ...
Related
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
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
This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Consider the following code:
name=John
echo ${name}
It prints "John", just as expected. Now consider this code:
name=John
echo $name
Again, this code prints "John" just as expected. Both codes work fine.
But I wonder is there any difference between the two, e.g. compatibility?
In your case, there is no difference.
In this case, there is:
name=John
echo ${name}Doe
echo $nameDoe
Read more: here
This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Variable variable assignment error -"command not found"
(1 answer)
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
So I've got a shell script to do some lazy stuff for if the directory isn't changing for a user. It's below. Essentially, it should be an if statement that if the user enters "default" for the directory, it'll pull them to the default directory for the files. However, I'm getting a command not found on line 16, which is the reassignment statement.
The entire if statement below:
if [ $directory = "default" ];
then
echo Enter your ldap:
read $ldap
$directory = "/usr/local/home/google/${ldap}/Downloads"
fi
I've tried doing it without the dollar sign too...nothing. What's going on here? New to shell, couldn't find this question asked before either.
This is how you should assign a value to a variable in shell:
directory="/usr/local/home/google/${ldap}/Downloads"
No dollar ($) sign.
No space around equal (=) sign.
Also, you should wrap your variables inside double quotes ("). This way, you avoid errors with undefined variables, arguments with spaces, etc.
That gives us:
if [ "$directory" = "default" ]
then
echo "Enter your ldap:"
read $ldap
directory="/usr/local/home/google/${ldap}/Downloads"
fi
This question already has answers here:
Why does a space in a variable assignment give an error in Bash? [duplicate]
(3 answers)
Closed 4 years ago.
I'm newer on bash scripting ,I have a global variable that I want to change his value insead a loop in my script but still get an error that commande not found
this my script :
SCRIPT_BASE = "/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH = ""
declare -a arr=("A" "B" "C" "D")
for i in "${arr[#]}"
do
if [ $i == "A" ]; then
CURRENT_SCRIPT_PATH = $SCRIPT_BASE
echo -e "Current Path : $CURRENT_SCRIPT_PATH"
fi
done
when I run this script I get that CURRENT_SCRIPT_PATH commande not found
Thanks in advance for any help
In bash you should be really cautious about spaces in if conditions but also when you assign a value to a variable.
Replace in your code the following tree lines:
SCRIPT_BASE="/home/scripts/test-Scripts"
CURRENT_SCRIPT_PATH=""
CURRENT_SCRIPT_PATH=$SCRIPT_BASE
If you keep a space after the variable name bash will interpret it as a command and as you do not have commands SCRIPT_BASE, CURRENT_SCRIPT_PATH, CURRENT_SCRIPT_PATH in your current $PATH you have the error command not found that is produced.