linux学习之shell脚本 ------- 脚本参数传递

[本文是自己学习所做笔记,欢迎转载,但请注明出处:http://blog.csdn.net/jesson20121020]

今天再来看一下如何向shell脚本传递参数,需要掌握两个命令,一个是 shift命令,另一个是getopts。

脚本参数传递

  shift命令

   用法:

    shift n 每次将参数位置向左偏移n位

   假如我们要实现统计多个文件的总行数,就可以用到这个shift命令了,如下:

opt2.sh

#!/bin/bash
#op2  static files total lines;
staticlines(){
   echo "static:`basename $0` filenames"
   exit
}
totalline=0
if [ $# -lt 2 ]
then
    staticlines
fi
while [ $# -ne 0 ]
do
    line=`cat $1 | wc -l`
    echo "$1:${line}"
    totalline=$[ $totalline+$line ]
    shift
done
echo "-------------------------------------"
echo "totalline:${totalline}"
   给予可执行权限,执行:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ chmod a+rx opt2.sh 
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh 
static:opt2.sh filenames
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt
static:opt2.sh filenames
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt 
lsout.txt:18
name.txt:4
-------------------------------------
totalline:22
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./opt2.sh lsout.txt name.txt while_test1.sh 
lsout.txt:18
name.txt:4
while_test1.sh:6
-------------------------------------
totalline:28
   通过shift 命令,我们可以很容易地实现统计log等信息。

  getopts命令

   该命令可以获得多个命令行参数。

   还是一个脚本来分析getopts的用法

optsget.sh

#!/bin/bash
#optsget
ALL=false
HELP=false
FILE=false
VERBOSE=false
while getopts ahfvc: OPTION
do
     case $OPTION in
     a)
         ALL=true
         echo "ALL is $ALL"
         ;;
     h)
         HELP=true
         echo "HELP is $HELP"
         ;;
     f)
         FILE=true
         echo "FILE is $FILE"
         ;;
     v) 
         VERBOSE=true
         echo "VERBOSE is $VERBOSE"
         ;;
     c)
         c=$OPTARG
         echo "c valuse is $c"
         ;;
     \?)
         echo "`basename $0` -[a h f v] -[c value]"
         ;;
      esac
done
   给予可执行权限,执行结果如下:

jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a
ALL is true
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f
ALL is true
FILE is true
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -h
ALL is true
FILE is true
HELP is true
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -h -v
ALL is true
FILE is true
HELP is true
VERBOSE is true
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -c
ALL is true
FILE is true
./optsget.sh: 选项需要一个参数 -- c
optsget.sh -[a h f v] -[c value]
jesson@jesson-K43SV:~/develop/worksapce/shell_workspace$ ./optsget.sh -a -f -c jesson
ALL is true
FILE is true
c valuse is jesson
  不难看出,可以通过getopts命令取得多个参数,而且还可以为每个参数指定值,在需要指定值的参数后加:即可。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值