<?xml version="1.0" encoding="ISO-8859-1"?>

<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/">
	<channel>
		<title><![CDATA[LinuxQuestions.org - Blogs - Jukka's script corner by kshfi]]></title>
		<link>http://www.linuxquestions.org/questions/blog/kshfi-438523/</link>
		<description>LinuxQuestions.org offers a free Linux forum where Linux newbies can ask questions and Linux experts can offer advice. Topics include security, installation, networking and much more.</description>
		<language>en</language>
		<lastBuildDate>Wed, 22 May 2013 19:13:25 GMT</lastBuildDate>
		<generator>vBulletin</generator>
		<ttl>60</ttl>
		<image>
			<url>https://lqo-thequestionsnetw.netdna-ssl.com/questions/images/misc/rss.jpg</url>
			<title><![CDATA[LinuxQuestions.org - Blogs - Jukka's script corner by kshfi]]></title>
			<link>http://www.linuxquestions.org/questions/blog/kshfi-438523/</link>
		</image>
		<item>
			<title>If - how to use</title>
			<link>http://www.linuxquestions.org/questions/blog/kshfi-438523/if-how-to-use-1389/</link>
			<pubDate>Fri, 05 Dec 2008 08:27:26 GMT</pubDate>
			<description>Programmers - shell *if* is command. 
 
 
Code: 
--------- 
if command exit code is 0 
then 
    do something 1 
    do something 2 
fi</description>
			<content:encoded><![CDATA[<div>Programmers - shell <b>if</b> is command.<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 98px;
		text-align: left;
		overflow: auto">if command exit code is 0
then
    do something 1
    do something 2
fi</pre>
</div>Ex.<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 114px;
		text-align: left;
		overflow: auto">if  cp fromfile tofile
then
   echo &quot;Works fine&quot;
else
   echo &quot;Error ???&quot;
fi</pre>
</div><b>[</b> = <b>test</b><br />
You can write <i>test</i> or <i>[</i><br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 194px;
		text-align: left;
		overflow: auto">a=1
b=1
test &quot;$a&quot; = &quot;$b&quot;
echo $?
0

a=1
b=2
test &quot;$a&quot; = &quot;$b&quot;
echo $?
1</pre>
</div>is same as<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 194px;
		text-align: left;
		overflow: auto">a=1
b=1
[ &quot;$a&quot; = &quot;$b&quot; ]
echo $?
0

a=1
b=2
[ &quot;$a&quot; = &quot;$b&quot; ]
echo $?
1</pre>
</div>In some *nix systems test and [ are same binary. Bash, ksh, ... [ and test are built-in commands.<br />
<br />
If you like to test command exit status, then it is possible with <b>if</b>.<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 194px;
		text-align: left;
		overflow: auto">a=1
b=1
if [ &quot;$a&quot; = &quot;$b&quot; ] ; then
   echo &quot;equal&quot;
fi
# or
a=1
b=1
if test &quot;$a&quot; = &quot;$b&quot;  ; then
   echo &quot;equal&quot;
fi</pre>
</div>You can write it also:<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 114px;
		text-align: left;
		overflow: auto">a=1
b=1
[ &quot;$a&quot; = &quot;$b&quot; ] 
if [ $? = 0 ] ; then
   echo &quot;equal&quot;
fi</pre>
</div><b>Why ; before then ?</b><br />
if and then on same line = you need<br />
command delimeter ;<br />
<br />
<b>Remember</b><br />
if is command which has arguments and options. = space between every arguments.<br />
<br />
Basic string testing<br />
= equal<br />
!= not equal<br />
<br />
Basic numeric testing (options)<br />
-eq  equal<br />
-ne  not equal<br />
-lt  less than<br />
-le  less or equal<br />
-gt  greater than<br />
-ge  greater or equal<br />
<br />
Basic file testing (options)<br />
-f file exist<br />
-d directory exist<br />
-x execute priviledges<br />
-r read ...<br />
-w write ...<br />
<br />
<b>Negation !</b><br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 66px;
		text-align: left;
		overflow: auto">if [ ! -f file ] ; then
   echo &quot;file $file not exists&quot;
fi</pre>
</div><div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 66px;
		text-align: left;
		overflow: auto">if ! cp x y ; then
   echo &quot;error&quot;
fi</pre>
</div><b>And, or </b><br />
-a and<br />
-o or<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 98px;
		text-align: left;
		overflow: auto">if cp x y -a cp b z ; then
   echo &quot;noth copy done&quot;
else
   echo &quot;co not worked
fi</pre>
</div>if not like to see errors<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 98px;
		text-align: left;
		overflow: auto">if cp x y 2&gt;/dev/null -a cp b z 2&gt;/dev/null; then
   echo &quot;both copy done&quot;
else
   echo &quot;cp not worked&quot;
fi</pre>
</div><br />
<br />
<b>&amp;&amp; ||</b><br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 34px;
		text-align: left;
		overflow: auto">cp x y &amp;&amp; cp a b || echo &quot;error&quot;</pre>
</div>Is same as<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 146px;
		text-align: left;
		overflow: auto">if cp x y ; then
  if ! cp a b ; then
        echo &quot;error&quot;
  fi
fi

&amp;&amp; if prev. exit stat 0, then this
|| if prev. exit stat &lt;&gt; 0, then this</pre>
</div><b>Using variables</b><br />
when use variables in test commands, use &quot;&quot;. Why ? If variable is empty - you get error.<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 82px;
		text-align: left;
		overflow: auto">if [ &quot;$a&quot; = &quot;&quot; ] ; then
   echo &quot;empty&quot;
   exit 
fi</pre>
</div>If $a value is empty and you write<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 82px;
		text-align: left;
		overflow: auto">if [ $a = &quot;&quot; ] ; then
   echo &quot;empty&quot;
   exit 
fi</pre>
</div>you get error, because arg-1 is nothing.<br />
&quot;$a&quot; is string, length 0<br />
<br />
<b>[[ ]]  ((  ))</b><br />
[[ is not same as [<br />
(( is for only numeric testing (C-like)<br />
<br />
<div style="margin:20px; margin-top:5px">
	<div class="smallfont" style="margin-bottom:2px">Code:</div>
	<pre class="bbcodeblock" dir="ltr" style="
		margin: 0px;
		margin-right: -99999px;
		padding: 3px;
		border: 1px inset;
		width: 98%;
		height: 66px;
		text-align: left;
		overflow: auto">a=1
b=2
((  a&gt;b  )) &amp;&amp; echo &quot;$a &gt; $b &quot;</pre>
</div></div>

]]></content:encoded>
			<dc:creator>kshfi</dc:creator>
			<guid isPermaLink="true">http://www.linuxquestions.org/questions/blog/kshfi-438523/if-how-to-use-1389/</guid>
		</item>
	</channel>
</rss>
