mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-08 23:14:06 +08:00
29 lines
1.5 KiB
HTML
29 lines
1.5 KiB
HTML
<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 [ "$#" -ne "2" ]; then
|
||
echo "usage: $0 <area> <hours>"
|
||
exit 2
|
||
fi</code></pre>
|
||
<p>在脚本中,退出时删除临时文件:</p>
|
||
<pre><code class="language-bash">trap "rm -f tmpfile; echo Bye." EXIT</code></pre>
|
||
<p>检查上一命令的退出码:</p>
|
||
<pre><code class="language-bash">./mycommand.sh
|
||
EXCODE=$?
|
||
if [ "$EXCODE" == "0" ]; then
|
||
echo "O.K"
|
||
fi</code></pre>
|
||
<!-- Linux命令行搜索引擎:https://jaywcjlove.github.io/linux-command/ -->
|