shell指定参数名传参

简介: shell指定参数名传参

参考:https://www.computerhope.com/unix/bash/getopts.htm

如果使用shell指定参数名称传参,是使用到了“getopts”命令,下面举个例子,定义入参(-n NAME and -t TIMES.)并获取参数,里面已包含注释:

#!/bin/bash
# 1. 定义参数变量名
NAME=""                                   # Name of person to greet.
TIMES=1                                   # Number of greetings to give. 
# 2. 用法提示
usage() {                                 # Function: Print a help message.
  echo "Usage: $0 [ -n NAME ] [ -t TIMES ]" 1>&2 
}
exit_abnormal() {                         # Function: Exit with error.
  usage
  exit 1
}
# 3. while循环设置进定义的变量名,并校验参数的合法性
while getopts ":n:t:" options; do         # Loop: Get the next option;
                                          # use silent error checking;
                                          # options n and t take arguments.
  case "${options}" in                    # 
    n)                                    # If the option is n,
      NAME=${OPTARG}                      # set $NAME to specified value.
      ;;
    t)                                    # If the option is t,
      TIMES=${OPTARG}                     # Set $TIMES to specified value.
      re_isanum='^[0-9]+$'                # Regex: match whole numbers only
      if ! [[ $TIMES =~ $re_isanum ]] ; then   # if $TIMES not whole:
        echo "Error: TIMES must be a positive, whole number."
        exit_abnormal
        exit 1
      elif [ $TIMES -eq "0" ]; then       # If it's zero:
        echo "Error: TIMES must be greater than zero."
        exit_abnormal                     # Exit abnormally.
      fi
      ;;
    :)                                    # If expected argument omitted:
      echo "Error: -${OPTARG} requires an argument."
      exit_abnormal                       # Exit abnormally.
      ;;
    *)                                    # If unknown (any other) option:
      exit_abnormal                       # Exit abnormally.
      ;;
  esac
done
# 4. 使用定义的变量名
if [ "$NAME" = "" ]; then                 # If $NAME is an empty string,
  STRING="Hi!"                            # our greeting is just "Hi!"
else                                      # Otherwise,
  STRING="Hi, $NAME!"                     # it is "Hi, (name)!"
fi
COUNT=1                                   # A counter.
while [ $COUNT -le $TIMES ]; do           # While counter is less than
                                          # or equal to $TIMES,
  echo $STRING                            # print a greeting,
  let COUNT+=1                            # then increment the counter.
done
exit 0   

运行结果:

./greeting
Hi!
-----
./greeting -n Dave
Hi, Dave!
-----
./greeting -t 3
Hi!
Hi!
Hi!
-----
./greeting -t 4 -n Betty
Hi, Betty!
Hi, Betty!
Hi, Betty!
Hi, Betty!
------
./greeting -n
Error: -n requires an argument.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
------
./greeting -t
Error: -t requires an argument.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
------
./greeting -t 0
Error: TIMES must be greater than zero.
Usage: ./greeting [ -n NAME ] [ -t TIMES ]
目录
相关文章
|
1月前
|
存储 缓存 Linux
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
【Shell 命令集合 磁盘维护 】Linux 设置和查看硬盘驱动器参数 hdparm命令使用教程
36 0
|
4月前
|
机器学习/深度学习 Shell
Shell 传递参数
Shell 传递参数
43 0
|
5月前
|
SQL 分布式计算 Hadoop
55 Hive Shell参数
55 Hive Shell参数
28 0
|
1月前
|
编解码 Linux Shell
【Shell 命令集合 系统设置 】Linux 设置Linux系统的控制台参数和属性setconsole命令 使用指南
【Shell 命令集合 系统设置 】Linux 设置Linux系统的控制台参数和属性setconsole命令 使用指南
33 0
|
1月前
|
存储 Shell Linux
【Shell 命令集合 系统设置 】Linux 将参数作为命令行输入 eval命令 使用指南
【Shell 命令集合 系统设置 】Linux 将参数作为命令行输入 eval命令 使用指南
25 0
|
2月前
|
存储 Shell
Shell变量和参数
Shell变量和参数
16 3
|
4月前
|
存储 Unix Shell
Linux【脚本 04】Shell脚本传递参数的4种方式(位置参数、特殊变量、环境变量和命名参数)实例说明
Linux【脚本 04】Shell脚本传递参数的4种方式(位置参数、特殊变量、环境变量和命名参数)实例说明
187 0
|
4月前
|
Shell
shell变量和参数
shell变量和参数
27 1
|
5月前
|
JavaScript Shell 开发工具
使用 npm 的配置参数 script-shell 来避免 window 执行脚本失败
使用 npm 的配置参数 script-shell 来避免 window 执行脚本失败
38 1
|
5月前
|
Shell Linux
Linux 如何给shell脚本传参数
name=中1"中1"中1为系统提供的位置参数,代表程序的名称,0代表程序的名称,[0代表程序的名称,[1/$2/…]从1开始为传递的参数。
50 0