2019-04-21 11:50:48 +08:00

29 lines
1.5 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<h1 id="exit">exit</h1>
<p>退出当前的shell</p>
<h2 id="补充说明">补充说明</h2>
<p><strong>exit命令</strong> 同于退出shell并返回给定值。在shell脚本中可以终止当前脚本执行。执行exit可使shell以指定的状态值退出。若不设置状态值参数则shell以预设值退出。状态值0代表执行成功其他值代表执行失败。</p>
<h3 id="语法">语法</h3>
<pre><code class="language-bash">exit(参数)</code></pre>
<h3 id="参数">参数</h3>
<p>返回值指定shell返回值。</p>
<h3 id="实例">实例</h3>
<p>退出当前shell</p>
<pre><code class="language-bash">[root@localhost ~]# exit
logout</code></pre>
<p>在脚本中,进入脚本所在目录,否则退出:</p>
<pre><code class="language-bash">cd $(dirname $0) || exit 1</code></pre>
<p>在脚本中,判断参数数量,不匹配就打印使用方式,退出:</p>
<pre><code class="language-bash">if [ &quot;$#&quot; -ne &quot;2&quot; ]; then
echo &quot;usage: $0 &lt;area&gt; &lt;hours&gt;&quot;
exit 2
fi</code></pre>
<p>在脚本中,退出时删除临时文件:</p>
<pre><code class="language-bash">trap &quot;rm -f tmpfile; echo Bye.&quot; EXIT</code></pre>
<p>检查上一命令的退出码:</p>
<pre><code class="language-bash">./mycommand.sh
EXCODE=$?
if [ &quot;$EXCODE&quot; == &quot;0&quot; ]; then
echo &quot;O.K&quot;
fi</code></pre>
<!-- Linux命令行搜索引擎https://jaywcjlove.github.io/linux-command/ -->