bash - Shell:间接扩展会导致错误的替代我是 Windows 但不是 Mac
问题描述
这是我正在谈论的脚本
#!/bin/bash
_sv="77777777777777777a"
if [ -n "${!_sv}" ]; then
echo "the value is there"
${echo} "${!sv}"
elif [ -n "${!1}" ]; then
echo "${!1}"
else
echo "False"
fi
当我在 Windows10 的 GNU中使用sh test.sh或bash test.shbash
运行它(使用 Cmder)时,它给了我一个错误的替换错误
dir_1/test.sh: line 2: 77777777777777777a: bad substitution
但是,当且仅当变量是纯字母或数字时,相同的脚本在 macOS 终端 shell 和 Windows10 中运行时不会出现任何错误。
重要提示:如果 _sv 值是字母数字,则会给出错误的替换错误。
请帮忙!
解决方案
这是预期的行为。您收到此错误是因为您尝试扩展的名称不是有效的变量名称。
这个检查是在 Bash 4.4 commit a0c0a00fc419b7 中引入的:
@@ -5926,6 +6661,16 @@ parameter_brace_expand_indir (name, var_is_special, quoted, quoted_dollar_atp, c
if (t == 0)
return (WORD_DESC *)NULL;
+ if (valid_brace_expansion_word (t, SPECIAL_VAR (t, 0)) == 0)
+ {
+ report_error (_("%s: bad substitution"), t);
+ free (t);
+ w = alloc_word_desc ();
+ w->word = &expand_param_error;
+ w->flags = 0;
+ return (w);
+ }
+
w = parameter_brace_expand_word (t, SPECIAL_VAR(t, 0), quoted, 0, 0);
free (t);
以前的版本改为将其扩展为空字符串。这包括 macOS 的 Bash 3.2。
推荐阅读
- c# - IAsyncEnumerable 中是否需要 Task.Delay()?
- django - Django无法使用UserFactory在测试中登录用户
- c# - Request New System Time on Runtime when Timezone Changes
- node.js - 在 Ubuntu 中降级版本节点时权限被拒绝
- woocommerce - Woocommerce - 需要使用 SQL 或插件删除与类别无关的产品
- node.js - 如何使用 Sequelize 在同一张表上创建外键?
- mongodb - MongoDB (mongoose) - 对文档中的类别和子类别进行分组
- python-3.x - 具有动态 PostgreSQL 架构的 SQLAlchemy ForeignKey
- javascript - 如何使线性渐变跟随 html 中范围滑块的拇指
- sql - 我在 Oracle Apex ORA-00907 上遇到错误:缺少右括号