Page 1 of 1
bash bug
Posted: 29 Mar 2023, 06:10
by Ed_P
Code: Select all
#!/bin/bash
if [ "$1" == "" ]; then
echo "== works - $ 1 = null"
fi
if [ -z $1 ]; then
echo "-z works - $ 1's length = 0"
fi
if [ -n $1 ]; then
echo "-n works - $ 1's length > 0"
fi
Code: Select all
guest@porteus:~$ ./test.sh x
-n works - $ 1's length > 0
guest@porteus:~$ ./test.sh
== works - $ 1 = null
-z works - $ 1's length = 0
-n works - $ 1's length > 0
guest@porteus:~$
If $1 is null it's length is 0. -n isn't working.
bash bug
Posted: 29 Mar 2023, 06:46
by Rava
Not sure if that's relevant, from man test :
Code: Select all
Except for -h and -L, all FILE-related tests dereference symbolic
links. Beware that parentheses need to be escaped (e.g., by back‐
slashes) for shells. INTEGER may also be -l STRING, which evaluates to
the length of STRING.
But maybe it is not relevant.
Could be that the shell handles the positional parameter differently so that the test fails.
but you have 2 ways that work correct, are they not sufficient?
bash bug
Posted: 29 Mar 2023, 09:29
by burdi01
Run the script in debug mode (bash -x) and you will see the problems.
Following is the corrected script:
Code: Select all
#!/bin/bash
if [ "$1" = "" ]; then
echo "= works - $ 1 = null"
fi
if [ -z "$1" ]; then
echo "-z works - $ 1's length = 0"
fi
if [ -n "$1" ]; then
echo "-n works - $ 1's length > 0"
fi
root@riposo:~/works# ./testIT x
-n works - $ 1's length > 0
root@riposo:~/works# ./testIT
= works - $ 1 = null
-z works - $ 1's length = 0
root@riposo:~/works#
bash bug
Posted: 29 Mar 2023, 09:59
by Rava
Mega
That I neither thought of the debug mode, nor about quoting the parameters.
bash bug
Posted: 29 Mar 2023, 14:37
by Ed_P
Debug mode works, both as bash -x and as set -x which I used last night;
Code: Select all
guest@porteus:/tmp$ ./test.sh
== works - $ 1 = null
-z works - $ 1's length = 0
-n works - $ 1's length > 0
guest@porteus:/tmp$ ./test.sh x
-n works - $ 1's length > 0
guest@porteus:/tmp$ bash -x ./test.sh
+ '[' '' == '' ']'
+ echo '== works - $ 1 = null'
== works - $ 1 = null
+ '[' -z ']'
+ echo '-z works - $ 1'\''s length = 0'
-z works - $ 1's length = 0
+ '[' -n ']'
+ echo '-n works - $ 1'\''s length > 0'
-n works - $ 1's length > 0
guest@porteus:/tmp$ ./test.sh << set -x used
+ '[' '' == '' ']'
+ echo '== works - $ 1 = null'
== works - $ 1 = null
+ '[' -z ']'
+ echo '-z works - $ 1'\''s length = 0'
-z works - $ 1's length = 0
+ '[' -n ']'
+ echo '-n works - $ 1'\''s length > 0'
-n works - $ 1's length > 0
guest@porteus:/tmp$
What doesn't work, for me, is seeing the problem.
-z worked without "" why shouldn't -n?
I don't remember the script I was working with several days ago that was causing me problems but I remembered the problem and wanted to report it before I forgot. And thank you for an easy solution to the problem.
A bash website I find useful is:
https://ryanstutorials.net/bash-scripti ... ements.php and this .png in particular:
bash bug
Posted: 30 Mar 2023, 09:38
by burdi01
Compare the "-z" and "-n" expansions in the debug output of the bad script:
+ '[' -z ']'
+ '[' -n ']'
with those for the corrected script:
+ '[' -z '' ']'
+ '[' -n '' ']'
In the "bad" case the string to test is missing!
As bash does not report this syntax error I would say that this is undefined behavior.