[SFS] bash scripters only
Chris Fedde
chris@fedde.us
Sat, 26 Nov 2016 20:47:40 -0700
--001a113d463c8b6c19054240371f
Content-Type: text/plain; charset=UTF-8
This behavior is documented in the CONDITIONAL EXPRESSIONS section of the
bash manual page. Specifically:
string
-n string
True if the length of string is non-zero.
chris
On Fri, Nov 25, 2016 at 7:00 PM, David L. Willson <DLWillson@thegeek.nu>
wrote:
> ...might find this interesting.
>
> Want to quickly test a variable for a non-empty definition? It seems you
> can just test it with the bash internal [[ and no operator at all, except
> the ]] to close the expression, of course.
>
> [dlwillson@localhost ~]$ dingo=monkey
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> true
> [dlwillson@localhost ~]$ dingo=false
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> true
> [dlwillson@localhost ~]$ dingo=False
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> true
> [dlwillson@localhost ~]$ dingo=
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> false
> [dlwillson@localhost ~]$ dingo=""
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> false
> [dlwillson@localhost ~]$ dingo=" "
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> true
> [dlwillson@localhost ~]$ unset dingo
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> false
> [dlwillson@localhost ~]$ dingo=0
> [dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo false
> true
>
> So, if the variable is unset or empty, the test returns false. If it's set
> to anything other than an empty string, it tests true. No operators needed.
>
> --
> David L. Willson
> Teacher, Engineer, Evangelist
> RHCE+Satellite CCAH Linux+ LPIC-1 SUSE_CLP LFCS
> Mobile 720-333-LANS(5267)
> http://sofree.us
>
> This is a good time for a r3VOLution.
>
>
--001a113d463c8b6c19054240371f
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div>This behavior is documented in the CONDITIONAL EXPRES=
SIONS section of the bash manual page. Specifically:<br><br>=C2=A0 =C2=A0 =
=C2=A0=C2=A0 string<br>=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 -n string<br>=
=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=
=A0 True if the length of string is non-zero.<br><br>chris<br></div><div><b=
r></div></div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote">On =
Fri, Nov 25, 2016 at 7:00 PM, David L. Willson <span dir=3D"ltr"><<a hre=
f=3D"mailto:DLWillson@thegeek.nu" target=3D"_blank">DLWillson@thegeek.nu</a=
>></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0 =
0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div style=3D"fo=
nt-family:times new roman,new york,times,serif;font-size:12pt;color:#000000=
">...might find this interesting.<br><br>Want to quickly test a variable fo=
r a non-empty definition? It seems you can just test it with the bash inter=
nal [[ and no operator at all, except the ]] to close the expression, of co=
urse.<br><br>[dlwillson@localhost ~]$ dingo=3Dmonkey<br>[dlwillson@localhos=
t ~]$ [[ $dingo ]] && echo true || echo false<br>true<br>[dlwillson=
@localhost ~]$ dingo=3Dfalse<br>[dlwillson@localhost ~]$ [[ $dingo ]] &=
& echo true || echo false<br>true<br>[dlwillson@localhost ~]$ dingo=3DF=
alse<br>[dlwillson@localhost ~]$ [[ $dingo ]] && echo true || echo =
false<br>true<br>[dlwillson@localhost ~]$ dingo=3D<br>[dlwillson@localhost =
~]$ [[ $dingo ]] && echo true || echo false<br>false<br>[dlwillson@=
localhost ~]$ dingo=3D""<br>[dlwillson@localhost ~]$ [[ $dingo ]]=
&& echo true || echo false<br>false<br>[dlwillson@localhost ~]$ di=
ngo=3D" "<br>[dlwillson@localhost ~]$ [[ $dingo ]] && ech=
o true || echo false<br>true<br>[dlwillson@localhost ~]$ unset dingo<br>[dl=
willson@localhost ~]$ [[ $dingo ]] && echo true || echo false<br>fa=
lse<br>[dlwillson@localhost ~]$ dingo=3D0<br>[dlwillson@localhost ~]$ [[ $d=
ingo ]] && echo true || echo false<br>true<br><br>So, if the variab=
le is unset or empty, the test returns false. If it's set to anything o=
ther than an empty string, it tests true. No operators needed.<br><br><div>=
<span name=3D"x"></span>--<br>David L. Willson<br>Teacher, Engineer, Evange=
list<br>RHCE+Satellite CCAH Linux+ LPIC-1 SUSE_CLP LFCS<br>Mobile 720-333-L=
ANS(5267)<br><a href=3D"http://sofree.us" target=3D"_blank">http://sofree.u=
s</a><br><br>This is a good time for a r3VOLution.<span name=3D"x"></span><=
br></div><br></div></div></blockquote></div><br></div>
--001a113d463c8b6c19054240371f--