shell --选项详解

getopts从命令中获取这些参数,并且删去了“-”,并将其赋值在第二个参数中,如果带有自己参数,这个参数赋值在“optarg”中。提供getopts的shell内置了optarg这个变变,getopts修改了这个变量。

这儿是强调参数和选项的顺序,如果乱了顺序就完蛋了。 

但是选项的顺序是可以颠倒的。

例子来源:

https://www.linuxidc.com/Linux/2018-07/153121.htm

https://www.cnblogs.com/yinghao1991/p/7123550.html

 

https://www.cnblogs.com/yaoyaojcy/p/9227272.html

getopts 不会重排所有参数的顺序,getopt 会重排参数顺序

 

https://www.cnblogs.com/qingergege/p/5914218.html

两个冒号表示该选项有一个可选参数,可选参数必须紧贴选项,如-carg 而不能是-c arg

 

注意: 

--表示后面的是参数

getopt有时分不清什么是选项, 什么是参数, 最好在参数前面添加一个 --

getopt会对参数进行排序。 

其他的博客中的 --long 不知道怎么使用,反正通不过。 

https://linuxeye.com/389.html

 

 

 

 

http://blog.itpub.net/23655288/viewspace-735049/

一、找出选项
1、处理简单选项  在抽取每个参数是,使用case语句判断参数是否符合选项格式
[root@rac2 ~]# cat t3.sh 
#!/bin/bash
while [ -n "$1" ]  ---此处的$1必须加且只能用双引号
do
  case $1 in   ---此处的$1可以加双引号或不加
  -a)  echo "found the -a option";;  ---此处的两个分号一定要加上
  -b)  echo "found the -b option";;
  -c)  echo "found the -c option";;
   *)  echo "$1 is not an option";;
  esac
  shift
done
[root@rac2 ~]# ./t3.sh  -a -b -c -g
found the -a option
found the -b option
found the -c option
-g is not an option
case语句检查每个参数是否为有效的选项,当找到一个选项时,就在case语句中运行适当的命令。
2、从参数中分离选项
linux使用特殊字符码将选项和普通参数分开,这个字符码告诉脚本选项结束和普通参数开始的位置。所以发现双破折号后,
脚本就能够安全的将剩余的命令行参数作为参数而不是选项来处理。
[root@rac2 ~]# cat t4.sh 
#!/bin/bash
while [ -n "$1" ]
do
  case $1 in
  -a) echo "found the -a option";;
  -b) echo "found the -b option";;
  -c) echo "found the -c option";;
  --) shift          
      break ;;
  *) echo "$1 is not an option";;
  esac
  shift
done
count=1
for param in $@
do
  echo "parameter #count:$param"
  count=$[ $count + 1 ]
done

[root@rac2 ~]# ./t4.sh 
[root@rac2 ~]# ./t4.sh -a -v -b -- test1 test2 test3  ---当脚本到达双破折号是,停止处理选项,并将其余的参数视为命令行参数
found the -a option
-v is not an option
found the -b option
parameter #count:test1
parameter #count:test2
parameter #count:test3
3、处理带值的选项
[root@rac2 ~]# cat t5.sh 
#!/bin/bash
while [ -n "$1" ]
do
  case $1 in 
   -a) echo "found the -a option";;
   -b) param="$2" 
       echo "found the -b option,with parameter value $param"
       shift 1;;
   -c) echo "found the -c option";;
   --) shift
       break;;
   esac
   shift
done
count=1
 for param in "$@"
 do
  echo "parameter #count:$param"
  count=$[ $count + 1 ]
done

[root@rac2 ~]# ./t5.sh -a -b 56 -c -- test1 test2 test3
found the -a option
found the -b option,with parameter value 56
found the -c option
parameter #count:test1
parameter #count:test2
parameter #count:test3
本例中-b有附加的参数值,由于要处理的参数是$1.所以知道附加参数位于$2,于是从变量$2中抽取参数值。
二、getopt命令
getopt命令是个不错的工具,在处理命令行选项时非常的方便,它对命令行参数进行重新的组织,使其更便于在脚本中解析。
1、命令格式
getopt options optstring parameters
其中optstring是处理的关键,他定义命令行中有效的选项字母,然后,在每个需要参数值的选项字母后面放置一个冒号。
[root@rac2 ~]# getopt ab:cd -a -b test1 -cd test2 test3
 -a -b test1 -c -d -- test2 test3
如果指定的选项不包含在选项字符串内,getopt命令会默认生成一个错误信息
[root@rac2 ~]# getopt ab:cd -a -b test1 -cde test2 test3
getopt:无效选项 -- e
 -a -b test1 -c -d -- test2 test3
如果希望忽略这个错误消息,可以再命令中使用-q选项:
[root@rac2 ~]# getopt -q ab:cd -a -b test1 -cde test2 test3
 -a -b 'test1' -c -d -- 'test2' 'test3'
2、在脚本中使用getopt
使用set命令可以讲命令行参数变量替换为set命令的的命令行中的值。
set -- `getopt -q ab:cd "$@"`
[root@rac2 ~]# cat t7.sh 
#!/bin/bash
set -- `getopt -q ab:c "$@"`
while [ -n "$1" ]
do
  case $1 in 
  -a) echo "found the -a option";;
  -b) param="$2"
      echo "found -b option with parameter value $param"
      shift;;
   -c) echo "found the -c option";;
   --) shift
       break;;
   *) echo "$1 is not an option";;
  esac
  shift
done
count=1
for param in "$@"
do
  echo "parameter #$count:$param"
  count=$[ $count + 1 ]
done

[root@rac2 ~]# ./t7.sh -ac
found the -a option
found the -c option
[root@rac2 ~]# ./t7.sh -a -b zhou -cd test1 test2 test3
found the -a option
found -b option with parameter value 'zhou'
found the -c option
parameter #1:'test1'
parameter #2:'test2'
parameter #3:'test3'
如果命令行参数中存在空格就会出现下面的情况
[root@rac2 ~]# ./t7.sh -a -b zhou -cd  "test1 test2" test3
found the -a option
found -b option with parameter value 'zhou'
found the -c option
parameter #1:'test1
parameter #2:test2'
parameter #3:'test3'
getopt命令不能很好的处理带空格的参数值,它将空格解析为参数分隔符,而不是将双引号引起来的两个值合并为一个参数。
解决上面的问题可以使用getopts命令。

 

 

<think>好的,我现在要回答用户关于Shell命令和脚本中`-a`选项的用法和含义的问题。首先,我需要整理一下已有的引用资料,看看有哪些相关的信息可以提取。 根据引用[2],`set`命令用于设置shell选项,其中`+`和`-`分别表示关闭和打开某个模式。例如,`set -a`会打开自动导出变量的功能。这里提到的`-a`是在`set`命令中的用法,所以用户的问题可能涉及`set -a`的情况。 引用[4]提到在条件测试中,`-a`作为逻辑与(AND)操作符,用于连接两个条件,例如`[ condition1 -a condition2 ]`。不过,这里也指出`-a`在测试中可能已被弃用,建议使用`&&`代替。此外,`-a`还可以作为`test`命令的参数,用于检查文件是否存在,例如`[ -a file ]`。 引用[5]同样提到条件测试中的比较操作符,其中`-ge`表示大于等于,但这里没有直接提到`-a`,不过可以确认`-a`在条件测试中的两种用途:逻辑与和文件存在性检查。 用户的问题可能涵盖了这两种情况,即`set -a`和条件测试中的`-a`。需要分别解释这两种用法,并给出示例。 接下来,我需要检查是否有其他可能的用法。比如,在某些命令中`-a`可能有不同的含义,但在提供的引用中没有提到,所以可能不需要涉及。根据引用资料,主要的两种情况是`set -a`和测试中的`-a`。 还需要注意引用[4]中提到的`-a`在条件测试中的两种不同上下文:作为逻辑运算符和作为文件测试运算符。这可能容易混淆,需要明确区分。 总结,回答的结构应该是: 1. 介绍`-a`在不同上下文中的不同含义。 2. 分点详细说明每种用法,包括示例。 3. 指出潜在的问题或建议,比如弃用`-a`而使用`&&`。 4. 提供相关问题供用户进一步了解。 需要确保使用正确的LaTeX格式,行内公式用$...$,独立公式用$$...$$,不过在这个问题中可能涉及较少数学公式,但如果有条件表达式,可能需要使用。例如,条件测试中的逻辑运算可以写成类似$[ condition1 -a condition2 ]$。 需要检查是否有遗漏的信息,例如其他引用中是否有关于`-a`的内容。引用[1]和[3]主要关于调试选项和其他参数处理,可能与`-a`无关,可以忽略。 最后,生成相关问题,帮助用户深入理解,例如关于其他测试选项或`set`命令的其他参数。</think>### Shell命令/脚本中`-a`选项的功能与用法详解Shell脚本中,`-a`的**具体含义取决于上下文**,主要分为以下两类: --- #### 一、`set -a`:自动导出变量 在`set`命令中使用`-a`选项时,表示**启用自动变量导出**功能。所有后续定义的变量将自动被标记为环境变量,可供子进程访问[^2]。 **语法示例**: ```bash set -a # 启用自动导出 VAR1="value1" # 该变量会被自动导出 export VAR2="value2" # 传统显式导出方式(与set -a效果相同) set +a # 关闭自动导出 ``` **应用场景**: - 批量导出变量时简化代码,避免重复使用`export`命令。 --- #### 二、条件测试中的`-a`:逻辑与或文件存在性检查 在条件测试(`test`或`[ ]`)中,`-a`有两种用途: 1. **
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值