[SFS] bash scripters only

David L. Willson DLWillson@TheGeek.NU
Fri, 25 Nov 2016 19:00:34 -0700 (MST)


------=_Part_68412_13751188.1480125634018
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit

...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. 


------=_Part_68412_13751188.1480125634018
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<html><head><style type=3D'text/css'>p { margin: 0; }</style></head><body><=
div style=3D'font-family: times new roman,new york,times,serif; font-size: =
12pt; color: #000000'>...might find this interesting.<br><br>Want to quickl=
y 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 t=
he expression, of course.<br><br>[dlwillson@localhost ~]$ dingo=3Dmonkey<br=
>[dlwillson@localhost ~]$ [[ $dingo ]] &amp;&amp; echo true || echo false<b=
r>true<br>[dlwillson@localhost ~]$ dingo=3Dfalse<br>[dlwillson@localhost ~]=
$ [[ $dingo ]] &amp;&amp; echo true || echo false<br>true<br>[dlwillson@loc=
alhost ~]$ dingo=3DFalse<br>[dlwillson@localhost ~]$ [[ $dingo ]] &amp;&amp=
; echo true || echo false<br>true<br>[dlwillson@localhost ~]$ dingo=3D<br>[=
dlwillson@localhost ~]$ [[ $dingo ]] &amp;&amp; echo true || echo false<br>=
false<br>[dlwillson@localhost ~]$ dingo=3D""<br>[dlwillson@localhost ~]$ [[=
 $dingo ]] &amp;&amp; echo true || echo false<br>false<br>[dlwillson@localh=
ost ~]$ dingo=3D" "<br>[dlwillson@localhost ~]$ [[ $dingo ]] &amp;&amp; ech=
o true || echo false<br>true<br>[dlwillson@localhost ~]$ unset dingo<br>[dl=
willson@localhost ~]$ [[ $dingo ]] &amp;&amp; echo true || echo false<br>fa=
lse<br>[dlwillson@localhost ~]$ dingo=3D0<br>[dlwillson@localhost ~]$ [[ $d=
ingo ]] &amp;&amp; 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 other=
 than an empty string, it tests true. No operators needed.<br><br><div><spa=
n name=3D"x"></span>--<br>David L. Willson<br>Teacher, Engineer, Evangelist=
<br>RHCE+Satellite CCAH Linux+ LPIC-1 SUSE_CLP LFCS<br>Mobile 720-333-LANS(=
5267)<br>http://sofree.us<br><br>This is a good time for a r3VOLution.<span=
 name=3D"x"></span><br></div><br></div></body></html>
------=_Part_68412_13751188.1480125634018--