shell脚本 - 总括(入门级)

注意shell脚本中是需要空格隔离的,

如:if后一定要有空格,if [ $# -ne 2 ]正确;if[$#=2]错误;因为空格是一个词的分界;

如:echo后也要有空格;

1 shell脚本第一行都是   

#!/bin/sh 

  符号#!用来告诉系统它后面的参数是用来执行该文件的程序。在这个例子中我们使用/bin/sh来执行程序。 

    

  要使脚本可执行: 

      chmod +x filename 

  然后,您可以通过输入: ./filename 来执行您的脚本。

2 变量

在shell编程中,所有的变量都由字符串组成,并且您不需要对变量进行声明。要赋值给一个变量,您可以这样写: 
变量名=值 
取出变量值可以加一个美元符号($)在变量前面: 

#!/bin/sh 
#对变量赋值: 
a="hello world"
# 现在打印变量a的内容: 
echo "A is:"
echo $a

再如:echo "this is the ${num}nd" 


3 shell脚本中的三类命令

1)UNIX命令

file somefile: 得到文件类型 

2)管道, 重定向:  

重定向:将命令的结果输出到文件,而不是标准输出(屏幕)。 
  > 写入文件并覆盖旧文件 
  >> 加到文件的尾部,保留旧文件内容。

3) 流程控制 

3.1) if

Shell 有且只有三种 if ... else 语句:

  • if ... fi 语句;
  • if ... else ... fi 语句;
  • if ... elif ... else ... fi 语句。

if ....; then 
  .... 
elif ....; then 
  .... 
else 
  .... 
fi 

通常用" [ ] "来表示条件测试。注意这里的空格很重要。要确保方括号的空格。 

[ -f "somefile" ] :判断是否是一个文件 

[ -x "/bin/ls" ] :判断/bin/ls是否存在并有可执行权限 

[ -n "$var" ] :判断$var变量是否有值 

[ "$a" = "$b" ] :判断$a和$b是否相等 

条件表达式

if [ -f  file ]    如果文件存在
if [ -d ...   ]    如果目录存在
if [ -s file  ]    如果文件存在且非空 
if [ -r file  ]    如果文件存在且可读
if [ -w file  ]    如果文件存在且可写
if [ -x file  ]    如果文件存在且可执行   

算术比较运算符
if [ int1 -eq int2 ]    如果int1等于int2   
if [ int1 -ne int2 ]    如果不等于    
if [ int1 -ge int2 ]       如果>=
if [ int1 -gt int2 ]       如果>
if [ int1 -le int2 ]       如果<=
if [ int1 -lt int2 ]       如果<

if [ = ]

if [ != ]

如:

#!/bin/sh 
if [ "$SHELL" = "/bin/bash" ]; then 
 echo "your login shell is the bash (bourne again shell)" 
else 
 echo "your login shell is not bash but $SHELL" 
fi 







3.2) case

case表达式可以用来匹配一个给定的字符串,而不是数字。 

case ... in 

...) do something here ;; 

esac 

如:

#!/bin/sh 
ftype=`file "$1"` 
case "$ftype" in 
"$1: Zip archive"*) 
  unzip "$1" ;; 
"$1: gzip compressed"*) 
  gunzip "$1" ;; 
"$1: bzip2 compressed"*) 
  bunzip2 "$1" ;; 
*) error "File $1 can not be uncompressed with smartzip";; 
esac 







case $pinmux_vo_select in
bt1120)
    echo "use bt1120";
    vo_bt1120_mode;
;;
rmii)
    echo "use rmii";
    net_rmii_mode;
;;
*)
    echo "use mii";
    net_mii_mode;
;;
esac


3.3)while、for

while ...; do .... done 如:

#!/bin/bash  
while [ $# != 0 ];do  
echo "第一个参数为:$1,参数个数为:$#"  
shift  
done 

输入如下命令运行:run.sh a b c d e f
结果显示如下:
第一个参数为:a,参数个数为:6
第一个参数为:b,参数个数为:5
第一个参数为:c,参数个数为:4
第一个参数为:d,参数个数为:3
第一个参数为:e,参数个数为:2
第一个参数为:f,参数个数为:1
从上可知 shift(shift 1) 命令每执行一次,变量的个数($#)减一(之前的$1变量被销毁,之后的$2就变成了$1),而变量值提前一位。
同理,shift n后,前n位参数都会被销毁

for var in ....; do 
 .... 
done 


如:
#!/bin/sh 
for var in A B C ; do 
 echo "var is $var" 
done



4 例子:

例子1:if [ $# -lt 3 ] ; then 

讲解

-e filename 如果 filename存在,则为真
-d filename 如果 filename为目录,则为真 
-f filename 如果 filename为常规文件,则为真
-L filename 如果 filename为符号链接,则为真
-r filename 如果 filename可读,则为真 
-w filename 如果 filename可写,则为真 
-x filename 如果 filename可执行,则为真
-s filename 如果文件长度不为0,则为真
-h filename 如果文件是软链接,则为真
filename1 -nt filename2 如果 filename1比 filename2新,则为真。
filename1 -ot filename2 如果 filename1比 filename2旧,则为真。
-eq 等于
-ne 不等于
-gt 大于
-ge 大于等于
-lt 小于
-le 小于等于
至于!号那就是取非了呗!

Shell的名字 $0

第一个参数 $1

第二个参数 $2

第n个参数 $n

所有参数 $@ 或 $*

参数个数 $#



例子2:














#!/bin/sh 
help() 
{ 
 cat < 
This is a generic command line parser demo. 
USAGE EXAMPLE: cmdparser -l hello -f -- -somefile1 somefile2 
HELP 
 exit 0 
} 
while [ -n "$1" ]; do 
case $1 in 
  -h) help;shift 1;; # function help is called 
  -f) opt_f=1;shift 1;; # variable opt_f is set 
  -l) opt_l=$2;shift 2;; # -l takes an argument -> shift by 2 
  --) shift;break;; # end of options 
  -*) echo "error: no such option $1. -h for help";exit 1;; 
  *) break;; 
esac 
done 

echo "opt_f is $opt_f" 
echo "opt_l is $opt_l" 
echo "first arg is $1" 
echo "2nd arg is $2" 


  您可以这样运行该脚本: 
cmdparser -l hello -f -- -somefile1 somefile2 
  返回的结果是: 
opt_f is 1 
opt_l is hello 
first arg is -somefile1 
2nd arg is somefile2
































發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章