摘要:在(嵌入式)linux软件开发中,经常要用到linux shell编写一些脚本,或用来代替一些重复的手工操作,或用来实现某种功能(如启动初始化脚本等),甚至实现一些复杂的功能。实际上,在linux中本身就存着很多的脚本文件。因此,从某种意义上来说,进行嵌入式linux开发,或多或少需要知道一些shell编程知识。

本文总结了应用linux shell编写脚本时常用到的一些基本技术:判断文件是否存在;判断变量是否有值或是否相等;shell中实现睡眠命令;统计行数。

判断文件是否存在

shell判断文件,目录是否存在或者具有权限   
 

#!/bin/sh  
myPath="/var/log/httpd/"  
myFile="/var /log/httpd/access.log"  
#这里的-x 参数判断$myPath是否存在并且是否具有可执行权限  
if [ ! -x "$myPath"]; then  
mkdir "$myPath"  
fi  
#这里的-d 参数判断$myPath是否存在   
if [ ! -d "$myPath"]; then  
mkdir "$myPath"  
fi  
#这里的-f参数判断$myFile是否存在  
if [ ! -f "$myFile" ]; then  
touch "$myFile"  
fi 
判断变量是否有值或是否相等

#其他参数还有-n,-n是判断一个变量是否是否有值  

if [ ! -n "$myVar" ]; then  
echo "$myVar is empty"  
exit 0  
fi  
#两个变量判断是否相等  
if [ "$var1" = "$var2" ]; then  
echo '$var1 eq $var2'  
else  
echo '$var1 not eq $var2'  
fi  
shell中实现睡眠命令

sleep 秒数

详见sleep命令

统计行数

能用wc -l统计文件行数

储到变量:a=`cat ufile |wc -l`

» 文章出处: reille博客—http://velep.com , 如果没有特别声明,文章均为reille博客原创作品
» 郑重声明: 原创作品未经允许不得转载,如需转载请联系reille#qq.com(#换成@)
分享到:

  2 Responses to “linux shell脚本编程1—零星总结”

  1. I just couldn’t go away your web site prior to suggesting that I extremely enjoyed the usual info a person provide for your guests? Is gonna be back continuously to inspect new posts

  2. I simply want to mention I’m all new to blogging and site-building and definitely liked this web page. Likely I’m going to bookmark your website . You really come with great article content. Regards for revealing your blog site.

Leave a Reply to Hung Aderhold Cancel reply

(必须)

(我会替您保密的)(必须)

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
© 2012 velep.com | reille blog | 管理| 粤ICP备15065318号-2| 谷歌地图| 百度地图| Suffusion theme|Sayontan Sinha

无觅相关文章插件,快速提升流量