当'let'表达式的值为0时,为什么带有-e选项的Bash退出? [英] Why does Bash with -e option exit when 'let' expression evaluates to 0?

查看:48
本文介绍了当'let'表达式的值为0时,为什么带有-e选项的Bash退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力理解为什么bash的 -e 选项会退出此脚本.仅当计算出的表达式给出 0 :

I am struggling to understand why the bash -e option exits this script. It happens only when the expression calculated gives 0:

#!/bin/bash
set -ex
table_year=( 1979 1982 1980 1993 1995 )
year=$1 
let indice=year-1
real_year=${table_year[$indice]}
echo OK $real_year

在以下情况下可以吗?

./bash_test_array 2

但不是在以下情况下

./bash_test_array 1 

indice 是这种情况,它等于 0 .为什么 -e 选项导致退出?

indice is this case equals to 0. Why the -e option causes an exit ?

推荐答案

请参见帮助让:

退出状态:如果最后一个ARG的值为0,则返回1;否则,返回1.let否则返回0.

Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise..

内置 let 的行为与常用的 expr 命令的行为相同:

The behavior of the let builtin is the same as for the commonly used expr command:

如果EXPRESSION为空,则退出状态为[...] 1或[...]

Exit status is [...] 1 if EXPRESSION is null or 0 [...]

您可以改用算术扩展:

indice=$(( year - 1 ))

即使指定的表达式的计算结果为0,该语句也将返回0.

This statement will return 0 even if the assigned expression evaluates to 0.

这篇关于当'let'表达式的值为0时,为什么带有-e选项的Bash退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆