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

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.

Related

Bash script - test not working as expected [duplicate]

This question already has answers here:
Why should there be spaces around '[' and ']' in Bash?
(5 answers)
using OR in shell script
(2 answers)
Closed 7 months ago.
here is my simple script prova.sh:
#!/bin/bash
echo "\$# = [$#]"
echo "\$1 = [$1]"
if [ $# -ge 2 || $1="-h" ]
then
echo "#########################################################################"
echo -e "usage: $0 \t launches the program only"
echo -e " $0 -s\t shows the command fired and launches the program"
echo -e " $0 -so\t shows only the command fired"
echo -e " $0 -h\t shows this guide"
echo "#########################################################################"
exit 0
fi
command="ls -la"
if test "$1"="-so" || "$1"="-s"
then
echo "Fired command:"
echo $command
fi
if test "$1"!="-so"
then
$command
fi
here is the output:
$> ./prova.sh -h
$# = [1]
$1 = [-h]
./prova.sh: row 6 : [: "]" missing
./prova.sh: row6: -h=-h: command not found
Fired command:
ls -l
totale 4
-rwxrw-r--. 1 user user 632 20 lug 16.13 prova.sh
I expected just the help
you need to close every test using single brackets, the shell way, if [ $# -ge 2 ] || [ $1 = "-h" ]; then ... this will fix it. If you still use single brackets way, is safe to enclose you variables with double quotes, so if they are empty, you are safe, like this if [ "$#" -ge 2 ] || [ "$1" = "-h" ]; then ... or you can just put them inside a double bracket, and bash will handle this for you if [[ $# -ge 2 || $1 = "-h" ]]; then ...

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.

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

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

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.

if statement not working in Bash script [duplicate]

This question already has answers here:
Syntax error near unexpected token 'then'
(2 answers)
Closed 6 years ago.
I can't understand why this simple if statement to check whether a string is empty is not working. It's giving me:
syntax error near unexpected token `then'
#!/bin/bash
str="Hello World!"
if[ ! -z "$str"]; then
echo "str is not empty"
fi
I tried it on my PC and an online editor as well but it shows the same issue. Any idea?
#!/bin/bash
str="Hello World!"
if [ ! -z "$str" ]; then
echo "str is not empty"
fi
Maybe,
insert space after "if" statement.
and, before "]".
[[ ]] is better than [ ] for tests:
#!/bin/bash
str="Hello World!"
if [[ ! -z $str ]]; then
echo "str is not empty"
fi
Shorter form:
#!/bin/bash
str="Hello World!"
[[ ! -z $str ]] && echo "str is not empty"

Resources