导航:首页 - java专业语言:shell--传入参数的处理

java专业语言:shell--传入参数的处理
作者:jeovani 来源:深圳教育在线 更新日期:2015-4-14

  #经过getopt的处理,下面处理具体选项。

  while true ; do

  case "$1" in

  -a|--a-long) echo "Option a" ; shift ;;

  -b|--b-long) echo "Option b, argument /`$2'" ; shift 2 ;;

  -c|--c-long)

  # c has an optional argument. As we are in quoted mode,

  # an empty parameter will be generated if its optional

  # argument is not found.

  case "$2" in

  "") echo "Option c, no argument"; shift 2 ;;

  *) echo "Option c, argument /`$2'" ; shift 2 ;;

  esac ;;

  --) shift ; break ;;

  *) echo "Internal error!" ; exit 1 ;;

  esac

  done

  echo "Remaining arguments:"

  for arg do

  echo '--> '"/`$arg'" ;

  done

  比如我们使用

  ./test -a -b arg arg1 -c

  你可以看到,命令行中多了个arg1参数,在经过getopt和set之后,命令行会变为:

  -a -b arg -c -- arg1

  $1指向-a,$2指向-b,$3指向arg,$4指向-c,$5指向--,而多出的arg1则被放到了最后。

  3.总结

  一般小脚本手工处理也许就够了,getopts能处理绝大多数的情况,getopt较复杂,功能也更强大。

  ******************************************************************************************************************************

  shift其实很简单的,就是左移参数列表,shift一次就将最左边的参数$1移出去了,然后

  原来的$2现在就变成了$1。

  TEMP=`getopt -o ab:c:: --long a-long,b-long:,c-long:: /

  shift后面还可以带上一个数字,指明要移出多少个参数(默认只移出一个),比如说

  shift 3 就是移出3个参数,之后原来的$4就变成了现在的$1。

  eval就是先将后面的参数执行一遍,将必要的置换都做了,再来执行命令。举个例子:

  MYFILE="cat myfile"

  echo $MYFILE   # output: cat myfile

  eval $MYFILE   # output: contents of myfile

  再举个详细点儿的例子:

  #!/bin/bash

  # evalit

  echo " Total number of arguments passed: $#"

  echo " The process ID:

  "echo"Lastargument:"$(evalecho/

  #)

  运行脚本:

  $ ./evalit alpha bravo charlie

  output as follows:

  Total number of arguments passed: 3

  The process ID: 780

  Last argument:   charlie

  读取文件

  for i in `cat abc.txt`

  do

  echo $i

  done

  -n 'example.bash' -- "$@"`

  if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

  # Note the quotes around `$TEMP': they are essential!

  #set 会重新排列参数的顺序,也就是改变$1,$2...$n的值,这些值在getopt中重新排列过了

  eval set -- "$TEMP"

[本文共有 2 页,当前是第 2 页] <<上一页 下一页>>


报 名 此 课 程 / 咨 询 相 关 信 息
【预约登门】 【网上咨询】 【订座试听】 【现在报名】
课程名称
java专业语言:shell--传入参数的处理
真实姓名
* 性 别
联系电话
* E-mail:
所在地区
咨询内容

      

相关文章:

Copyright© 2004-2017 www.szedu.net 深圳教育在线 版权所有
中国·深圳 粤ICP备06023013号