cmp in if statement (Bash) [duplicate] - linux

This question already has answers here:
if fi [: too many arguments
(3 answers)
Closed 5 years ago.
if [ cmp -s "$expectedOut" "$actualOut" ]; then
The following line of code keeps giving me errors saying that there are too many arguments. however I know this is the proper typical use of cmp so I think it may have to do with the brackets. Anyone know whats really going on here?

Lose the [ ].
if cmp -s "$expectedOut" "$actualOut" ; then
The syntax of if is
if Command; then
[ is just one possible command (on that happens to expect ] as its last argument to make things look pretty).

Related

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

Is there any difference in how variables are referenced in shell script? [duplicate]

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

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 ] ...

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 [ ... ].

Bash file existence checking missing ] [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am attempting a simple file existence check in my bash script:
FILE_TO_CHECK=/home/username/path/to/file
if [ ! -f $FILE_TO_CHECK]; #line 9 in actual script
then
echo File not found.
fi
Seems simple enough to me, but I'm getting the following error and I don't know why:
/path/to/script: line 9: [: missing `]'
I'm not exactly a bash expert, but I was pretty sure a backtick is not necessary in this context. What gives?
Missing space before the closing ].
You have to understand that [ is a command and everything following it, until the ;, are its arguments. Command [ expects its last argument to be ]. But if you omit the space, then ] becomes the last character of the previous argument.
It might seem that [ ] is part of the if syntax. It's not the case. if has to be followed by any command, and if evaluates its exit status.
if true; then foo; fi
In the above line true is a command too.
$ which true
/bin/true
true is a command with the sole purpose of always having a true (0) exit status.
You could also try:
if (test ! -f $FILE_TO_CHECK);
or
if !(test -f $FILE_TO_CHECK);
as [ is a shorthand for the test command.

Resources