嵌入式成长轨迹10 【嵌入式环境及基础】【Linux shell强化】【脚本参数传递】

 

一、shift命令
每次将参数位置向左偏移n位

 1 #!/bin/bash
2 #shift use
3
4 function use()
5 {
6 echo "Hello now i am $0! "
7 return 0
8 }
9 totalline=0;
10 if [ $# -lt 2 ];then
11 use
12 exit
13 fi
14
15 while [ $# -ne 0 ]
16 do
17 line=`cat $1 |wc -l`
18 echo "$l : $line"
19 totalline=$[ $totalline+$line ]
20 shift
21 done
22 echo "----------------"
23 echo "total:$totalline"

 

二、getopts命令
获得多个命令行参数
输入-a -h -c ff试试~

 

 1 #!/bin/bash
2 #getopts use
3
4 ALL=false
5 HELP=false
6 FILE=false
7 VERBOSE=false
8
9 while getopts ahfvc: OPTION
10 do
11 case $OPTION in
12 a)
13 ALL=true
14 echo "ALL IS $ALL"
15 ;;
16 h)
17 HELP=true
18 echo "HELP is $HELP"
19 ;;
20 f)
21 FILE=true
22 echo "FILE IS $FILE"
23 ;;
24 h)
25 HELP=true
26 echo "HELP is $HELP"
27 ;;
28 f)
29 FILE=true
30 echo "FILE IS $FILE"
31 ;;
32 v)
33 VERBOSE=true
34 echo "VERBOSE = $VERBOSE"
35 ;;
36 c)
37 c=$OPTARG
38 echo "c value is $c"
39 ;;
40 \?)
41 echo "`basename $0` -[a h f v] -[c value]file"
42 ;;
43 esac
44 done

 

 

 

 

终端

1 [root@localhost ~]# vi hellofun.sh
2 [root@localhost ~]# hellofun.sh -a -h -c ff
3 ALL IS true
4 HELP is true
5 c value is ff

 


 

posted @ 2012-03-06 19:17  MooreZHENG  阅读(253)  评论(0编辑  收藏  举报