<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[[实践OK]shell函数以及函数失败返回的写法，常用的shell判断语句及常用shell函数]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 14 Jan 2010 05:17:47 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	shell 时用$?查看是否执行成功，成功是0，而在函数里是只要有没有成功，那么整个函数的$?就会是1，于是用它和&#124;&#124;结合起来可以实现Shell函数是否成功执行的一个判断并提示。<br/>cat fun.sh <br/><textarea name="code" class="php" rows="15" cols="100">
#!/bin/bash
save_cycle_data()&#123;
&nbsp;&nbsp;&nbsp;&nbsp;echo &quot;hello world...&#92;n&quot;
&nbsp;&nbsp;&nbsp;&nbsp;ls /tmp2
&#125;

save_cycle_data &#124;&#124; &#123; echo &#039;ls /tmp2 failed&#039; &amp;&amp; exit 1; &#125;
</textarea><br/><br/>sh fun.sh <br/>hello world...&#92;n<br/>ls: cannot access /tmp2: No such file or directory<br/>ls /tmp2 failed<br/><br/><br/>今天重新再翻看了一下shell程序相关的资料。准备重新将shell再提高一下。<br/>将http://seo-dic.com.cn/archives/2496 提到的《Advanced Bash-Scripting Guide》《高级bash脚本编程指南》上附带的源码重新再看一次。<br/>为了方便查看。我将所有的源代码全部导到一个文件中，再一一查看。省去了每次只打开一个脚本的麻烦。同时，将几个经常用的shell脚本函数稍做了下列举。希望后面可以用得到。<br/>find -type f -name &quot;*.sh&quot; -exec cat &#123;&#125; &gt;&gt;sh.all &#92;;<br/><br/>检查目录是否存在，若不存在，则创建相应目录。<br/><div class="code"><br/><br/># Check for and create datadir if necessary:<br/>if test ! -d $datadir<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;then<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mkdir $datadir<br/>fi<br/></div><br/><br/>shell参数判断，若不符合则显示使用方式及退出。<br/><div class="code"><br/>E_BADARGS=65<br/><br/>case $# in<br/>&nbsp;&nbsp;0&#124;1)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # The vertical bar means &quot;or&quot; in this context.<br/>&nbsp;&nbsp;echo &quot;Usage: `basename $0` old_file_suffix new_file_suffix&quot;<br/>&nbsp;&nbsp;exit $E_BADARGS&nbsp;&nbsp;# If 0 or 1 arg, then bail out.<br/>&nbsp;&nbsp;;;<br/>esac<br/></div><br/>另例：<br/><div class="code"><br/>E_OPTERR=65<br/><br/>if &#91; &quot;$#&quot; -eq 0 &#93;<br/>then&nbsp;&nbsp; # Script needs at least one command-line argument.<br/>&nbsp;&nbsp;echo &quot;Usage $0 -&#91;options a,b,c&#93;&quot;<br/>&nbsp;&nbsp;exit $E_OPTERR<br/>fi<br/></div><br/><br/>检查是用户是否是root权限的方式：<br/><br/><br/>ROOT_UID=0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Root has $UID 0.<br/>E_WRONG_USER=65&nbsp;&nbsp;&nbsp;&nbsp;# Not root?<br/><br/>E_NOSUCHUSER=70<br/>SUCCESS=0<br/><br/><br/>if [ &quot;$UID&quot; -ne &quot;$ROOT_UID&quot; ]<br/>then<br/>&nbsp;&nbsp;echo; echo &quot;Only root can run this script.&quot;; echo<br/>&nbsp;&nbsp;exit $E_WRONG_USER<br/>else<br/>&nbsp;&nbsp;echo<br/>&nbsp;&nbsp;echo &quot;You should know better than to run this script, root.&quot;<br/>&nbsp;&nbsp;echo &quot;Even root users get the blues... &quot;<br/>&nbsp;&nbsp;echo<br/>fi<br/><br/>进行用户目录判断：<br/>LOG_DIR=/var/log<br/>ROOT_UID=0&nbsp;&nbsp;&nbsp;&nbsp; # Only users with $UID 0 have root privileges.<br/>LINES=50&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Default number of lines saved.<br/>E_XCD=66&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; # Can&#039;t change directory?<br/>E_NOTROOT=67&nbsp;&nbsp; # Non-root exit error.<br/><br/>cd $LOG_DIR<br/><br/>if [ `pwd` != &quot;$LOG_DIR&quot; ]&nbsp;&nbsp;# or&nbsp;&nbsp; if [ &quot;$PWD&quot; != &quot;$LOG_DIR&quot; ]<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# Not in /var/log?<br/>then<br/>&nbsp;&nbsp;echo &quot;Can&#039;t change to $LOG_DIR.&quot;<br/>&nbsp;&nbsp;exit $E_XCD<br/>fi&nbsp;&nbsp;# Doublecheck if in right directory, before messing with log file.<br/>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [实践OK]shell函数以及函数失败返回的写法，常用的shell判断语句及常用shell函数]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>