我对bash脚本感到困惑。

我有以下代码:

function grep_search() {
    magic_way_to_define_magic_variable_$1=`ls | tail -1`
    echo $magic_variable_$1
}

我希望能够创建一个变量名,其中包含命令的第一个参数,并承载例如ls的最后一行的值。

为了说明我想要的:

$ ls | tail -1
stack-overflow.txt

$ grep_search() open_box
stack-overflow.txt

那么,我应该如何定义/声明$magic_way_to_define_magic_variable_$1,我应该如何在脚本中调用它?

我已经试过eval, ${…}, \$${...},但我还是很困惑。


当前回答

使用声明

没有必要像其他答案一样使用前缀,也没有数组。只使用声明、双引号和参数展开。

我经常使用下面的技巧来解析包含1到n个参数的参数列表,格式为key=value otherkey=othervalue etc=etc,例如:

# brace expansion just to exemplify
for variable in {one=foo,two=bar,ninja=tip}
do
  declare "${variable%=*}=${variable#*=}"
done
echo $one $two $ninja 
# foo bar tip

但是展开argv列表就像

for v in "$@"; do declare "${v%=*}=${v#*=}"; done

额外的建议

# parse argv's leading key=value parameters
for v in "$@"; do
  case "$v" in ?*=?*) declare "${v%=*}=${v#*=}";; *) break;; esac
done
# consume argv's leading key=value parameters
while test $# -gt 0; do
  case "$1" in ?*=?*) declare "${1%=*}=${1#*=}";; *) break;; esac
  shift
done

其他回答

我希望能够创建一个包含该命令的第一个参数的变量名

script.sh文件:

#!/usr/bin/env bash
function grep_search() {
  eval $1=$(ls | tail -1)
}

测试:

$ source script.sh
$ grep_search open_box
$ echo $open_box
script.sh

根据帮助评估:

作为shell命令执行参数。


你也可以使用Bash ${!间接展开,正如前面提到的,但是它不支持检索数组下标。


欲进一步阅读或示例,请查看BashFAQ/006关于Indirection的内容。

我们不知道有任何技巧可以在POSIX或Bourne shell中复制该功能,而不需要eval,这可能很难安全地做到。所以,考虑这是一个自担风险的使用黑客。

但是,您应该根据以下注意事项重新考虑使用间接方法。

Normally, in bash scripting, you won't need indirect references at all. Generally, people look at this for a solution when they don't understand or know about Bash Arrays or haven't fully considered other Bash features such as functions. Putting variable names or any other bash syntax inside parameters is frequently done incorrectly and in inappropriate situations to solve problems that have better solutions. It violates the separation between code and data, and as such puts you on a slippery slope toward bugs and security issues. Indirection can make your code less transparent and harder to follow.

对于varname=$prefix_suffix格式,只需使用:

varname=${prefix}_suffix

POSIX兼容的答案

对于这个解决方案,您需要拥有/tmp文件夹的r/w权限。 我们创建一个临时文件保存变量,并利用set内置的-a标志:

$ man套装 ... -a每个创建或修改的变量或函数都被赋予export属性,并标记为可导出到后续命令的环境。

因此,如果我们创建一个包含动态变量的文件,我们可以使用set在脚本中赋予它们生命。

实现

#!/bin/sh
# Give the temp file a unique name so you don't mess with any other files in there
ENV_FILE="/tmp/$(date +%s)"

MY_KEY=foo
MY_VALUE=bar

echo "$MY_KEY=$MY_VALUE" >> "$ENV_FILE"

# Now that our env file is created and populated, we can use "set"
set -a; . "$ENV_FILE"; set +a
rm "$ENV_FILE"
echo "$foo"

# Output is "bar" (without quotes)

解释以上步骤:

# Enables the -a behavior
set -a

# Sources the env file
. "$ENV_FILE"

# Disables the -a behavior
set +a

将两个评分较高的答案结合成一个完整的例子,希望有用且不言自明:

#!/bin/bash

intro="You know what,"
pet1="cat"
pet2="chicken"
pet3="cow"
pet4="dog"
pet5="pig"

# Setting and reading dynamic variables
for i in {1..5}; do
        pet="pet$i"
        declare "sentence$i=$intro I have a pet ${!pet} at home"
done

# Just reading dynamic variables
for i in {1..5}; do
        sentence="sentence$i"
        echo "${!sentence}"
done

echo
echo "Again, but reading regular variables:"
echo $sentence1
echo $sentence2
echo $sentence3
echo $sentence4
echo $sentence5

输出:

你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

同样,但是读取的是常规变量: 你知道吗,我家里有一只宠物猫 你知道吗,我家里有只宠物鸡 你知道吗,我家里有一头宠物牛 你知道吗,我家里有一只宠物狗 你知道吗,我家里有只宠物猪

哇,大部分语法都很糟糕!如果你需要间接引用数组,这里有一个简单语法的解决方案:

#!/bin/bash

foo_1=(fff ddd) ;
foo_2=(ggg ccc) ;

for i in 1 2 ;
do
    eval mine=( \${foo_$i[@]} ) ;
    echo ${mine[@]}" " ;
done ;

对于更简单的用例,我推荐使用高级bash脚本编写指南中描述的语法。