Confuse with the linus conditions '-z' '-n' [duplicate] - linux

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
here is my script:
#!/bin/bash
read -p "para:" teatp
if [ -z $teatp ]; then
echo '-z is ture'
else
echo '-z is false'
fi
if [ -n $teatp ]; then
echo '-n is ture'
else
echo '-n is false'
fi
when I input nothing and press enter, the result is
para:
-z is ture
-n is ture
on the other hand, when I input something and press enter, the result is
para:qwer1234
-z is false
-n is ture
which confused me is the first result -n is ture.
I think the -n and -z are antonyms, but why is the result the same?
There must be something I ignore or misunderstand.
I will be appreciate if someone can point out

the #testp should be quote by double quotation marks?
like:
[ -n "$teatp" ]

Related

How to assign variable to output of the program in bash? [duplicate]

This question already has answers here:
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 11 months ago.
Here I have the simple code in which I am trying to store the value of program output to a variable in bash.
#!/bin/bash
i=0
for value in {1..10}
do
variable=`$value | ./unpackme`
str="What's my favorite number? Sorry, that's not it!"
if [[ "$variable" == "$str" ]]
then
echo "hello"
fi
done
The output I am getting is as follows:
Output screenshot
Please help me fix this. Thanks in advance.
You may try :
#!/bin/bash
str="What's my favorite number? Sorry, that's not it!"
for value in {1..10}; do
variable=$(./unpackme <<< "$value")
if [ "$variable" = "$str" ]; then
echo "hello"
fi
done
Some notes :
New style $(command) is preferred to `command`.
string in command <<< string is what is called a here string : command standard input is fed with string (think of it as : echo string | command).
[ "$variable" = "$str" ]: Take care of [[ "$a" == "$b" ]]/[[ "$a" = "$b" ]], they will consider the right part of ==/= as a pattern, and may not do what you want.

Why testing for non-empty string using -n always true? [duplicate]

This question already has answers here:
Test for empty string with X"" [duplicate]
(2 answers)
Closed 2 years ago.
Hello I have a simple example which I am confused about:
#!bin/bash
#test_str is a script to test whether a string is empty or not
x=''
if [ -n $x ]
then
echo "'$x' is not empty"
else
echo "'$x' empty"
fi
chmod u+x test_str
./test_str
The output:
'' is not empty
This occurs also if I've not declared a variable (x here).
If I use the flag -z to test for emptiness it works fine:
if [ -z $x ]
then
echo "'$x' is empty"
else
echo "'$x' not empty"
fi
The output:
'' is empty
So how can I use -n correctly?
Thank you!
This is because [ -n $n ] is expanded to [ -n ] so there is nothing to compare and it will return true.
Instead, you should use [ -n "${n}" ] to prevent such errors and to prevent globbing.

Verify if IP present in file [duplicate]

This question already has answers here:
How to test if string exists in file with Bash?
(16 answers)
Closed 3 years ago.
Verify if base machine IP is present in file
Trying to resolve it using for loop with if condition
#!/bin/bash
#file with IP List /tmp/ip_list.txt
curr_ip="$(hostname -I)"
for n in `cat /tmp/ip_list.txt`;
do
if [ "$n" == "$curr_ip" ]; then
echo "success"
else
echo "fail"
fi
done
By default it is running else condition.
#!/bin/bash
curr_ip="$(hostname -I)"
output=$(grep -c /tmp/ip_list.txt)
if [ "$output" != "0" ]
then
echo "success"
else
echo "failure"
fi
The -c option in grep gives you a count. If it doesn't equal zero, it found something.

string check in bash with -z and -n

I made a mistake in test syntax in bash but now I want to understand what really happens in string check with -n and -z.
I wrote the following lines to get in LINENUM variable the line number from a grep. When the string is not found (there is only one in the file, for sure), the LINENUM variable is empty.
$ LINENUM=$(grep -w -n mystring myfile | cut -d: -f1)
$ echo --$LINENUM--
----
$ if [ -n $LINENUM ] ; then echo "Checked -n"; fi
Checked -n
$ if [ -z $LINENUM ] ; then echo "Checked -z"; fi
Checked -z
Then I realized I forgot the double quotes and then the following check gave to me:
$ if [ -n "$LINENUM" ] ; then echo "Checked -n"; fi
$ if [ -z "$LINENUM" ] ; then echo "Checked -z"; fi
Checked -z
So, in the former tests, where I forgot the double quotes, versus what did the if test check , really, since I got two positive checks from both -n and -z ?
Without the quotes, your test statement (with either operator, represented with -X here), reduces to
if [ -X ]; then echo "Checked -X"; fi
According to the POSIX standard, the one-argument form of test (which you now have here) is true if the argument is non-null. Since the literal string -X is non-null (it's not an operator anymore), it evaluates to true.
With the quotes, you get
if [ -X "" ]; then echo "Checked -X"; fi
Since the quotes force an empty 2nd argument, you have a 2-argument form of test, and -X (whether -n or -z) is properly recognized as a primary operator acting on the 2nd, null, argument.

shell script to compare files and print formatted output

I'm trying to write a shell script which will compare two files, and if there are no differences between then, it will indicate that there was a success, and if there are differences, it will indicate that there was a failure, and print the results. Here's what I have so far:
result = $(diff -u file1 file2)
if [ $result = "" ]; then
echo It works!
else
echo It does not work
echo $result
fi
Anybody know what I'm doing wrong???
result=$(diff -u file1 file2)
if [ $? -eq 0 ]; then
echo "It works!"
else
echo "It does not work"
echo "$result"
fi
Suggestions:
No spaces around "=" in the variable assignment for results
Use $? status variable after running diff instead of the string length of $result.
I'm in the habit of using backticks for command substitution instead of $(), but #Dennis Williamson cites some good reasons to use the latter after all. Thanks Dennis!
Applied quotes per suggestions in comments.
Changed "=" to "-eq" for numeric test.
First, you should wrap strings being compared with quotes.
Second, "!" cannot be use it has another meaning. You can wrap it with single quotes.
So your program will be.
result=$(diff -u file1 file2)
if [ "$result" == "" ]; then
echo 'It works!'
else
echo It does not work
echo "$result"
fi
Enjoy.
Since you need results when you fail, why not simply use 'diff -u file1 file2' in your script? You may not even need a script then. If diff succeeds, nothing will happen, else the diff will be printed.
bash string equivalence is "==".
-n is non-zero string, -z is zero length string, wrapping in quotes because the command will complain if the output of diff is longer than a single string with "too many arguments".
so
if [ -n "$(diff $1 $2)" ]; then
echo "Different"
fi
or
if [ -z "$(diff $1 $2)" ]; then
echo "Same"
fi

Resources