简体   繁体   English

使用glob扩展为环境变量添加错误替换

[英]Bash bad substitution with glob expansion for environment variables

How can I match environment variables which include the case-insensitive segment "proxy" that is not a prefix? 如何匹配包含不是前缀的不区分大小写的“代理”的环境变量? I'm on bash: 我在打击:

root@PDPINTDEV9:~# echo ${SHELL}
/bin/bash

I want to unset a bunch of proxy variables simultaneously. 我想同时取消一堆代理变量。 They all have "proxy" or "PROXY" in the name, such as http_proxy or NO_PROXY . 它们在名称中都有“proxy”或“PROXY”,例如http_proxyNO_PROXY I would like to use glob expansion, which this answer & comment says is what bash uses . 我想使用glob扩展, 这个答案和评论说是bash使用的

Also based on that answer , I see that I can find environment vars which start with "PROXY": 同样基于这个答案 ,我看到我可以找到以“PROXY”开头的环境变量:

root@PDPINTDEV9:~# echo "${!PROXY*}"
PROXY_IP PROXY_PORT

But that doesn't make sense with what I've read about glob expansion. 但是,这并不能使 感什么 我读过关于水珠扩张。 Based on those, "${!PROXY*}" should match anything that doesn't start with proxy... I think. 基于这些, "${!PROXY*}"应匹配任何不以代理开头的东西......我想。

Furthermore, I can't get anything that does make sense with glob syntax to actually work: 此外,我无法得到任何有效的glob语法实际工作:

root@PDPINTDEV9:~# echo ${*proxy}
-bash: ${*proxy}: bad substitution
root@PDPINTDEV9:~# echo "${!*[pP][rR][oO][xX][yY]}"
-bash: ${!*[pP][rR][oO][xX][yY]}: bad substitution

SOLVED below: Turns out you can't. 求助于以下:原来你不能。 Crazy, but thanks everyone. 疯了,但谢谢大家。

Variable name expansion , as a special case of shell parameter expansion , does not support globbing. 变量名扩展 ,作为shell参数扩展的特例,不支持globbing。 But it has two flavors: 但它有两种口味:

${!PREFIX*}
${!PREFIX@}

In both, the * and @ characters are hard-coded . 在两者中, *@字符都是硬编码的

The first form will expand to variable names prefixed with PREFIX and joined by the first character of the IFS (which is a space, by default): 第一个表单将扩展为以PREFIX为前缀的变量名,并由IFS的第一个字符(默认为空格)连接:

$ printf "%s\n" "${!BASH*}"
BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND ...

The second form will expand to variable names (prefixed with PREFIX ), but as separate words: 第二种形式将扩展为变量名(前缀为PREFIX ),但作为单独的单词:

$ printf "%s\n" "${!BASH@}"
BASH
BASHOPTS
BASHPID
BASH_ALIASES
BASH_ARGC
...

Both of these forms are case-sensitive , so to get the variable names in a case-insensitive manner, you can use set , in combination with some cut and grep : 这两种形式都区分大小写 ,因此要以不区分大小写的方式获取变量名, 可以使用set ,结合一些cutgrep

$ (set -o posix; set) | cut -d= -f1 | grep -i ^proxy
PROXY_IP
proxy_port

But that doesn't make sense with what I've read about glob expansion. 但这与我读到的关于全球扩张的内容没有关系。 Based on those, " ${!PROXY*} " should match anything that doesn't start with proxy... I think. 基于这些,“ ${!PROXY*} ”应匹配任何不以代理开头的东西......我想。

No and no. 不,不。

In the first place, the ! 首先, ! character is not significant to pathname expansion, except when it appears at the beginning of a character class in a pattern, in which case the sense of the class is inverted. 字符对于路径名扩展并不重要,除非它出现在模式中字符类的开头,在这种情况下,类的意义被反转。 For example, fo[!o] is a pattern that matches any three-character string whose first two characters are "fo" and whose third is not another 'o'. 例如, fo[!o]是一个匹配任何三个字符的字符串的模式,前两个字符是“fo”,第三个字符不是另一个“o”。 But there is no character class in your expression. 但是你的表达式中没有字符类。

But more importantly, pathname expansion isn't relevant to your expression ${!PROXY*} at all. 但更重要的是,路径名扩展与您的表达式${!PROXY*}完全无关。 There is no globbing there. 那里没有斑点。 The '!' '!' and '*' are fixed parts of the syntax for one of the forms of parameter expansion. 和'*'是参数扩展形式之一的语法的固定部分。 That particular expansion produces, by definition , the names of all shell variables whose names start with "PROXY", separated by the first character of the value of the IFS variable. 根据定义 ,该特定扩展产生名称以“PROXY”开头的所有shell变量的名称,由IFS变量的值的第一个字符分隔。 Where it appears outside of double quotes, it is equivalent to ${!PROXY@} , which is less susceptible to globbing-related confusion. 如果它出现在双引号之外,它相当于${!PROXY@} ,它不太容易受到与globbing相关的混淆。

Furthermore, I can't get anything that does make sense with glob syntax to actually work: [...] 此外,我无法得到任何有效的glob语法实际工作:[...]

No, because, again, there is no globbing going on. 不,因为再次没有发生全局变化。 You need exactly ${! 你需要${! followed by the name prefix of interest, followed by *} or @} to form the particular kind of parameter expansion you're asking about. 然后是感兴趣的名称前缀,后跟*}@}以形成您要询问的特定类型的参数扩展。

How can I match environment variables which include the case-insensitive segment "proxy"? 如何匹配包含不区分大小写的段“代理”的环境变量?

You need to explicitly express the case variations of interest to you. 您需要明确表达您感兴趣的案例变体。 For example: 例如:

${!PROXY*} ${!proxy*} ${!Proxy*}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM