help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Irregularities when using -v, -z, -n


From: Alex fxmbsw7 Ratchev
Subject: Re: Irregularities when using -v, -z, -n
Date: Thu, 29 Jul 2021 15:30:55 +0200

here a few examples

foo1() { if eval "$1" ; then eval "$2" ; else eval "$3" ; fi ; }
foo1 '[[ a@b == *@* ]]' 'printf email\\n' 'printf no@\\n'

var=test foo2='[[ -v $var ]]'
if eval "$foo2" ; then printf %s\ exists\\n "$var" ; else printf no\
%s\ no\\n "$var" ; fi

shopt -s expand_aliases
alias 1='[[ ' 2='-v ' 3=var ' 4=']]' s=$'\nprintf ye\\n\n '
e=$'\nprintf no\\n\n '
if 1 2 3 4 ;  then s ; else e ; fi

On Thu, Jul 29, 2021 at 3:18 PM Alex fxmbsw7 Ratchev <fxmbsw7@gmail.com> wrote:
>
> use aliases or eval, and proper var assignments
>
> On Thu, Jul 29, 2021, 15:12 <eduardo-chibas@caramail.com> wrote:
>>
>> How can I print if some condition is true or false?
>>
>> Have tried but failed using
>>
>>   local aa
>>
>>   echo "local aa"
>>   p=[[ -v aa ]] ; q=[[ ! -v aa ]]
>>   echo "[[ -v aa ]] $p [[ ! -v aa ]] $q"
>>   [[ -v aa ]] && echo "[[ -v aa ]]: Variable exists."
>>   [[ ! -v aa ]] && echo "[[ ! -v aa ]]: Variable unknown."
>>
>>
>>
>> > Sent: Friday, July 30, 2021 at 1:04 AM
>> > From: eduardo-chibas@caramail.com
>> > To: leonid.isaev@ifax.com
>> > Cc: help-bash@gnu.org
>> > Subject: Irregularities when using -v, -z, -n
>> >
>> > How does manual never give examples of how to use things !
>> >
>> > 6.4 Bash Conditional Expressions
>> >
>> > Everyone is expected to figure things out by themselves.
>> >
>> >
>> >
>> > > Sent: Friday, July 30, 2021 at 12:34 AM
>> > > From: "Leonid Isaev (ifax)" <leonid.isaev@ifax.com>
>> > > To: help-bash@gnu.org
>> > > Subject: Re: Irregularities when using -v, -z, -n
>> > >
>> > > On Thu, Jul 29, 2021 at 10:27:08AM +0200, eduardo-chibas@caramail.com 
>> > > wrote:
>> > > > I am getting confused with the following code
>> > >
>> > > Well, perhaps you could read "help test"? If you did, you'd see:
>> > > -----8<-----
>> > > ...
>> > >    String operators:
>> > >       -z STRING      True if string is empty.
>> > >       -n STRING      True if string is not empty.
>> > > ...
>> > >       -v VAR         True if the shell variable VAR is set.
>> > >       -R VAR         True if the shell variable VAR is set and is a name
>> > >                      reference.
>> > > ...
>> > > ----->8-----
>> > >
>> > > Therefore,
>> > >
>> > > >   [[ -v aa ]] && echo "[[ -v aa ]]: Variable exists."
>> > >
>> > > tests if a variable is set, but
>> > >
>> > > >   [[ -z aa ]] && echo "[[ -z aa ]]: Variable exists, size is zero."
>> > >
>> > > tests that a string "aa" is zero length. You need to replace aa -> "$aa" 
>> > > here.
>> > > Also, [[ -z ... ]] returns 0 also for unset variables.
>> > >
>> > > --
>> > > Leonid Isaev
>> > >
>> > >
>> >
>> >
>>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]