• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >linux shell > Shell脚本传递参数的3种方法比较

Shell脚本传递参数的3种方法比较

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了shell脚本参数,shell脚本接收参数,shell脚本传参数,shell脚本获取参数,shell脚本 p参数等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com
#!/bin/bash
#extracting command text_text_text_line options as parameters

help_info(){
  echo "NAME"
  echo "\t$0"
  echo "SYNOPSIS"
  echo "\t$0 is a shell test about process options"
  echo "DESCRIPTION"
  echo "\toption like -a -b param1 -c param2 -d"
}

if [ $# -lt 0 ]
then
  help_info
fi

nomal_opts_act()
{
  echo -e "\n### nomal_opts_act ###\n"

  while [ -n "$1" ]
  do
  case "$1" in 
    -a)
      echo "Found the -a option"
      ;;
    -b)
      echo "Found the -b option"
      echo "The parameter follow -b is $2" 
      shift
      ;;
    -c)
      echo "Found the -c option"
      echo "The parameter follow -c is $2"
      shift
      ;;
    -d)
      echo "Found the -d option"
      ;;
     *)
       echo "$1 is not an option"
      ;;
  esac
  shift
  done
}

#用shell命令自建的选项解析,可以按照自己的想法实现
#优点:自己定制,没有做不到,只有想不到
#缺点:麻烦

getopt_act()
{
  echo -e "\n### getopt_act ###\n"

  GETOPTOUT=`getopt ab:c:d "$@"`
  set -- $GETOPTOUT 
  while [ -n "$1" ] 
  do
  case $1 in 
    -a)
      echo "Found the -a option"
      ;;
    -b)
      echo "Found the -b option"
      echo "The parameter follow -b is "$2""
      shift
      ;;
    -c)
      echo "Found the -c option"
      echo "The parameter follow -c is "$2""
      shift
      ;;
    -d)
      echo "Found the -d option"
      ;;
    --)
      shift
      break
      ;;
     *)
       echo "Unknow option: "$1""
      ;;
  esac
  shift
  done

  param_index=1
  for param in "$@"
  do
    echo "Parameter $param_index:$param"
    param_index=$[ $param_index + 1 ] 
  done
}

#用getopt命令解析选项和参数
#优点:相对与getopts来说是个半自动解析,自动组织选项和参数,用 -- 符号将选项与参数隔开
#缺点:相对于getopts的缺点
#1.需要与set -- 命令配合,不是必须,需要手动shift
#2.选项参数中不支持空格如 -a -b dog -c "earth moon" -d -f param1 param2 就会解析错误

getopts_act()
{
  echo -e "\n### getopts_act ###\n"
  while getopts :ab:c:d ARGS
  do
  case $ARGS in 
    a)
      echo "Found the -a option"
      ;;
    b)
      echo "Found the -b option"
      echo "The parameter follow -b is $OPTARG"
      ;;
    c)
      echo "Found the -c option"
      echo "The parameter follow -c is $OPTARG"
      ;;
    d)
      echo "Found the -d option"
      ;;
     *)
       echo "Unknow option: $ARGS"
      ;;
  esac
  done

  shift $[ $OPTIND -1 ] 
  param_index=1
  for param in "$@"
  do
    echo "Parameter $param_index:$param"
    param_index=$[ $param_index + 1 ] 
  done
}

#getopts 命令解析选项和参数
#优点:可在参数中包含空格如:-c "earth moon"
#      选项字母和参数值之间可以没有空格如:-bdog
#      可将未定义的选项绑定到?输出
#      Unknow option: ?

nomal_opts_act -a -b dog -c earth -d -f param1 param2
getopts_act -a -b dog -c "earth moon" -d -f param1 param2
getopt_act -a -b dog -c earth -d -f param1 param2
</div> </div>

您可能想查找下面的文章:

  • Shell脚本传递参数的3种方法比较
  • Shell脚本传参数方法总结
  • Shell脚本中判断输入参数个数的方法
  • Shell最多支持多少个参数?

相关文章

  • shell生成简单格式的xml实例
  • 神奇的shell命令行输入与输出功能介绍
  • Shell中使用plink工具实现远程批量关机
  • 使用bash shell删除目录中的特定文件的3种方法
  • Linux下的tar压缩解压缩命令详解(小结)
  • linux shell在while中用read从键盘输入的实现
  • shell中的各种括号的使用方法
  • Python创建、删除桌面、启动组快捷方式的例子分享
  • 实时查看系统流量的Shell脚本分享
  • Linux Shell脚本编程的注意事项

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • linux awk时间计算脚本及awk命令详解
    • 解决VirtualBox中Ubuntu 14.04屏幕分辨率不能设置的问题
    • Linux/Nginx如何查看搜索引擎蜘蛛爬虫的行为
    • exit(-1)或者return(-1)shell得到的退出码为什么是255
    • Linux vim编辑命令模式
    • Shell常用操作符总结
    • 学习linux常用命令(推荐)
    • Shell脚本实现自动修改IP地址
    • shell脚本连接并重启远程服务器的方法
    • linux shell实现转换输入日期的格式

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有