本文介绍了BASH通配符:选择所有变量(而非内容)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一堆要检查的变量,如果它们包含值"None",那么我想清空它们.

I have a bunch of variables that I want to check, and if they contain the value "None" then I want to empty them.

    var1=(check for some value, sometimes it returns "none")
    var2=(check for some value, sometimes it returns "none")
    var3=(check for some value, sometimes it returns "none")
    someBizzareName=(check for some value, sometimes it returns "none")

    if [[ "${var1}" == "None" ]] ; then
        var1=""
    fi
    if [[ "${var2}" == "None" ]] ; then
        var2=""
    fi

这一切都很好,很花哨,只是因为我有很多 varN,如果[[" $ {varN}"==无"]] ,我必须知道他们的名字;所以我想知道,由于搜索和匹配所有内容在BASH中非常自然,如果变量在for循环内中有通配符,那么将匹配 all vars,类似 $ {*} (我尝试过,行不通)吗?我进行了各种各样的搜索,但是总是找到有关匹配变量 content 的信息,而不是var本身..?

And this is all working fine and dandy, only since I have a lot of varN, I will end up with a ton of if [[ "${varN}" == "None" ]] and I have to know their names ; so I was wondering, since it's in BASH very nature to search and match everything, if there is a wild-card for variables, inside a for loop, that will match all vars, something like ${*} (I tried that, does'nt work) ? I have done all kinds of searches but always find something about matching variable content, not the var itself..?

推荐答案

全部,否.但是您可以匹配模式(但不能匹配 * ).

All, no. But you can match a pattern (but not *).

$ echo "${!B*}"
BASH BASHOPTS BASHPID BASH_ALIASES BASH_ARGC BASH_ARGV BASH_CMDS BASH_COMMAND BASH_LINENO BASH_SOURCE BASH_SUBSHELL BASH_VERSINFO BASH_VERSION

这篇关于BASH通配符:选择所有变量(而非内容)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 11:51