Please reproduce your error on a second machine before posting, and check the error by running without saved changes or extra modules (See FAQ No. 13, "How to report a bug"). For unstable Porteus versions (alpha, beta, rc) please use the relevant thread in our "Development" section.
-
Ed_P
- Contributor
- Posts: 8388
- Joined: 06 Feb 2013, 22:12
- Distribution: Cinnamon 5.01 ISO
- Location: Western NY, USA
Post#1
by Ed_P » 29 Mar 2023, 06:10
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.
Ed
Ed_P
-
Rava
- Contributor
- Posts: 5424
- Joined: 11 Jan 2011, 02:46
- Distribution: XFCE 5.01 x86_64 + 4.0 i586
- Location: Forests of Germany
Post#2
by Rava » 29 Mar 2023, 06:46
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?
Cheers!
Yours Rava
Rava
-
burdi01
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Post#3
by burdi01 » 29 Mar 2023, 09:29
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#
burdi01
-
Rava
- Contributor
- Posts: 5424
- Joined: 11 Jan 2011, 02:46
- Distribution: XFCE 5.01 x86_64 + 4.0 i586
- Location: Forests of Germany
Post#4
by Rava » 29 Mar 2023, 09:59
Mega
That I neither thought of the debug mode, nor about quoting the parameters.
Cheers!
Yours Rava
Rava
-
Ed_P
- Contributor
- Posts: 8388
- Joined: 06 Feb 2013, 22:12
- Distribution: Cinnamon 5.01 ISO
- Location: Western NY, USA
Post#5
by Ed_P » 29 Mar 2023, 14:37
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:
Ed
Ed_P
-
burdi01
- Shogun
- Posts: 206
- Joined: 18 Aug 2013, 12:09
- Distribution: Slackware PartedMagic Xubuntu
- Location: The Netherlands
Post#6
by burdi01 » 30 Mar 2023, 09:38
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.
burdi01