bash 除非在shell脚本中没有?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21888393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 09:37:44  来源:igfitidea点击:

Is there no unless in shell script?

bashif-statementhash

提问by olala

I tried to do the opposite of if in shell to only do something if a value in a hash doesn't exist. I've learned to create a hash in bash from here:Associative arrays in Shell scripts

我试图在 shell 中做相反的事情,只在哈希中的值不存在时才做一些事情。我从这里学会了在 bash 中创建哈希:Shell 脚本中的关联数组

declare -A myhash

I declared a simple hash:

我声明了一个简单的哈希:

myhash[$key]="1"

and

[ ${myhash[$key]+abc} ]

to see if myhash[$key] exist or not from here: Easiest way to check for an index or a key in an array?

从这里查看 myhash[$key] 是否存在:检查数组中索引或键的最简单方法?

I also learned to add ! in front of an expression to do the opposite in if clause from here:How to make "if not true condition"?

我也学会了添加!在 if 子句中从这里做相反的表达式之前:如何制作“if not true condition”?

But the following expression

但是下面的表达式

if ! [ ${myhash[$key]+abc} ]; then
     echo $key

fi

doesn't work and no information is printed. There is no error message.

不起作用,也不打印任何信息。没有错误信息。

I tried this:

我试过这个:

[ ${myhash[$key]+abc} ] && echo $key

And got the error message:

并得到错误信息:

abc: command not found

Can anyone tell me how to make it work?

谁能告诉我如何使它工作?

采纳答案by chepner

First, this

首先,这

[ ${myhash[$key]+abc} ]

will test if the result of the parameter expansion is empty or not. Either ${myhash[$key]}exists and expands to a string (which itself may or may not be empty), or it does not and it expands to the string "abc". Assuming you don't set myhash[$key]="", the expansion always produces a non-empty string, and so the command succeeds. As a result, ! [ ${myhash[$key]+abc} ]would always fail.

将测试参数扩展的结果是否为空。要么${myhash[$key]}存在并扩展为字符串(它本身可能为空,也可能不为空),或者不存在并扩展为字符串“abc”。假设你没有设置myhash[$key]="",扩展总是产生一个非空字符串,所以命令成功。结果, ! [ ${myhash[$key]+abc} ]总是失败。

Assuming "abc" is not a valid entry in myhash, you need to actually check if the parameter expands to an actual value, or "abc" for unset keys:

假设“abc”不是 中的有效条目myhash,您需要实际检查参数是否扩展为实际值,或“abc”是否为未设置的键:

if [[ ${myhash[$key]+abc} == abc ]]; then
    echo "$key does not exist in myhash"
else
    echo "myhash[$key]=${myhash[$key]}"
fi

回答by that other guy

The reason nothing is printed is because you check if the value is unset after setting it.

什么都不打印的原因是因为您在设置后检查该值是否未设置。

Here's what you're doing:

这是你在做什么:

#!/bin/bash
key=foo

declare -A myhash                     # create associative array
myhash[$key]="1"                      # set the value
if ! [ ${myhash[$key]+abc} ]; then    # check if value is not set
     echo $key                        # if so, print key 
fi

And now you're asking "Why isn't the unset check triggering when the value is set?"

现在您问“为什么在设置值时未触发未设置检查?”

If you instead wanted to check if the key is set, you should not have inverted the existence check using !.

如果您想检查密钥是否已设置,则不应使用!.

As for [ ${myhash[$key]+abc} ] && echo $keygiving abc: command not found, I can't explain that. It would never normally happen.

至于[ ${myhash[$key]+abc} ] && echo $key给予abc: command not found,我无法解释。它通常永远不会发生。

That is what you would have seen if you did ${myhash[$key]+abc} && echo $keythough. Are you sure you're correctly presenting the actual examples you ran, and the actual results you got

如果你这样做了,那是你会看到的${myhash[$key]+abc} && echo $key。你确定你正确地展示了你运行的实际例子,以及你得到的实际结果

回答by Vuade

Easiest way to check for an index or a key in an array?shows how to check if it exists with [ ${array[key]+abc} ].

检查数组中索引或键的最简单方法?显示如何检查它是否存在[ ${array[key]+abc} ]

If it does, the command exits successfully, so the portion after the &&gets executed (echo "exists")

如果是,则命令成功退出,因此&&执行之后的部分( echo "exists")

You want to check if it doesn't exist. So you want your command to be executed if the [..]portion exits unsuccessfully. So use ||instead of &&:

你想检查它是否不存在。因此,如果该[..]部分未成功退出,您希望执行您的命令。所以使用||代替&&

[ ${myhash[$key]+abc} ] || echo "does not exist"

&&: carry on with next command only if first succeeded

&&: 仅当第一次成功时才继续执行下一个命令

||: only carry on with next command if first did not succeed

||: 如果第一次没有成功,只继续执行下一个命令