<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; shell if</title>
	<atom:link href="http://www.liguosong.com/tag/shell-if/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.liguosong.com</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2018 01:02:19 +0000</lastBuildDate>
	<language>zh-CN</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.2</generator>
		<item>
		<title>shell结构化命令if-then-elif-fi</title>
		<link>http://www.liguosong.com/2014/01/05/shell%e7%bb%93%e6%9e%84%e5%8c%96%e5%91%bd%e4%bb%a4if-then-elif-fi/</link>
		<comments>http://www.liguosong.com/2014/01/05/shell%e7%bb%93%e6%9e%84%e5%8c%96%e5%91%bd%e4%bb%a4if-then-elif-fi/#comments</comments>
		<pubDate>Sun, 05 Jan 2014 13:06:39 +0000</pubDate>
		<dc:creator>lgs</dc:creator>
				<category><![CDATA[Shell]]></category>
		<category><![CDATA[shell]]></category>
		<category><![CDATA[shell if]]></category>

		<guid isPermaLink="false">http://www.liguosong.com/?p=234</guid>
		<description><![CDATA[在编写shell中，很少有脚本是顺序操作，大部分的时候需要进行逻辑判断。 先看一个简单的格式： if comm [...]]]></description>
				<content:encoded><![CDATA[<p>在编写shell中，很少有脚本是顺序操作，大部分的时候需要进行逻辑判断。<br />
先看一个简单的格式：</p>
<pre class="brush:bash">if command
then
    commands
fi</pre>
<p>bash shell会运行if后面的命令。如果该命令的退出状态码是0，then后的命令就会执行。否则不执行。<span id="more-234"></span>来个简单的例子：</p>
<pre class="brush:bash">#!/bin/sh
workPath=/Users/liguosong/work/shell
if cd $workPath
then
     echo Ok
fi</pre>
<p>如果这个路径存在，就会输出Ok。</p>
<p>上面是一个很单间的if语句。其它语言都会有else，else if语句。shell也有。如果if后面的命令执行结果是非零，就会执行else部分。先来一个简单的else的例子：</p>
<pre class="brush:bash">#!/bin/sh
workPath=/Users/liguosong/work/shell
if cd $workPath
then
    echo Ok
else
    echo Path not exist.
fi</pre>
<p>有时候需要进行多种条件判断，shell中我们可以用elif。基本的结构：</p>
<pre class="brush:bash">if command
then
    commands
elif command2
then
    commands
fi</pre>
<p>上面的结构，只是普通的shell命令，if的条件是否成立都是与状态码有关的。那么把其它条件作为if判断的依据，那么我们就要学习下面的test命令了。</p>
<p>shell test命令</p>
]]></content:encoded>
			<wfw:commentRss>http://www.liguosong.com/2014/01/05/shell%e7%bb%93%e6%9e%84%e5%8c%96%e5%91%bd%e4%bb%a4if-then-elif-fi/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
