您现在的位置是:网站首页> 编程资料编程资料
Shell脚本通过参数名传递参数的实现代码_linux shell_
2023-05-26
331人已围观
简介 Shell脚本通过参数名传递参数的实现代码_linux shell_
平常在写shell脚本都是用$1,$2…这种方式来接收参数,然而这种接收参数的方式不但容易忘记且不易于理解和维护。Linux常用的命令都可指定参数名和参数值,然而我们怎样才能给自己的shell脚本也采用参数名和参数值这样的方式来获取参数值呢?而不是通过$1,$2这种方式进行获取。下面的例子定义了短参数名和长参数名两种获取参数值的方式。其实是根据getopt提供的特性进行整理而来。
#!/bin/bash while getopts i:o:p:s:t: OPT; do case ${OPT} in i) in_file=${OPTARG} ;; o) out_dir=${OPTARG} ;; p) product_code=${OPTARG} ;; s) software_version=${OPTARG} ;; t) type=${OPTARG} ;; \?) printf "[Usage] `date '+%F %T'` -i -o -o -s -t \n" >&2 exit 1 esac done # check parameter if [ -z "${in_file}" -o -z "${out_dir}" -o -z "${product_code}" -o -z "${software_version}" -o -z "${type}" ]; then printf "[ERROR] `date '+%F %T'` following parameters is empty:\n-i=${in_file}\n-o=${out_dir}\n-p=${product_code}\n-s=${software_version}\n-t=${type}\n" exit 1 fi # block enc java -jar openailab-command-line-auth-0.1-SNAPSHOT.jar ${in_file} ${out_dir} ${product_code} ${software_version} ${type}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
相关内容
- Linux shell 提取文件名和目录名的方法_linux shell_
- 处理Shell脚本中带有空格的变量(bash脚本)_linux shell_
- CentOS7运行.sh脚本提示syntax error: unexpected end of file的解决方法_linux shell_
- shell 里面的奇葩字符实现_linux shell_
- linux 开机自启动redis服务的方法_linux shell_
- Linux实现定时备份MySQL数据库并删除30天前的备份文件_linux shell_
- Linux 中shell脚本设置开头固定格式的实现方法_linux shell_
- shell中set指令的用法详解_linux shell_
- shell字符串截取报错:Bad substitution的解决_linux shell_
- 详解shell中脚本参数传递的两种方式_linux shell_
