Bourne Shell 构建并引用变量

发布于 2024-09-18 12:23:23 字数 529 浏览 8 评论 0原文

我有一个运行的 shell,其中预设的环境变量包括:

FOOCOUNT=4
FOO_0=John
FOO_1=Barry
FOO_2=Lenny
FOO_3=Samuel

我无法更改获取此数据的方式。

我想运行一个循环来生成变量并使用内容。

echo "Hello $FOO_count"

然而,这个语法是错误的,这就是我正在寻找的......

count=$FOOCOUNT
counter=0
while [ $counter -lt $count ]
do
#I am looking for the syntax for: <<myContructedVar= $ + 'FOO_' + $counter>>
counter=`expr $counter + 1`
echo "Greeting #$counter: Hello, ${myContructedVar}."
done

非常感谢

I have a shell that runs where the preset env variables include:

FOOCOUNT=4
FOO_0=John
FOO_1=Barry
FOO_2=Lenny
FOO_3=Samuel

I can not change the way I get this data.

I want to run a loop that generates up the variable and uses the contents.

echo "Hello $FOO_count"

This syntax is however wrong and that is what I am searching for...

count=$FOOCOUNT
counter=0
while [ $counter -lt $count ]
do
#I am looking for the syntax for: <<myContructedVar= $ + 'FOO_' + $counter>>
counter=`expr $counter + 1`
echo "Greeting #$counter: Hello, ${myContructedVar}."
done

Thanks very much

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2024-09-25 12:23:23

关键是eval

count=$FOOCOUNT
counter=0
while [ $counter -lt $count ]
do
    myConstructedVar=FOO_$counter
    counter=`expr $counter + 1`
    echo "Greeting #$counter: Hello, `eval echo \${myConstructedVar}`."
done

循环算术是老派的——我编写代码的方式。现代 shell 内置了更多算术 - 但问题被标记为 Bourne shell。

The key is eval:

count=$FOOCOUNT
counter=0
while [ $counter -lt $count ]
do
    myConstructedVar=FOO_$counter
    counter=`expr $counter + 1`
    echo "Greeting #$counter: Hello, `eval echo \${myConstructedVar}`."
done

The loop arithmetic is old school - the way I write the code. Modern shells have more arithmetic built in - but the question is tagged Bourne shell.

末骤雨初歇 2024-09-25 12:23:23

您将需要一个eval 和一个延迟的印记:

$ foo_0=john
$ count=0    
$ name="\$foo_$count"
$ echo $name
$foo_0
$ eval echo "$name"    
john

但除非索引对您来说确实很重要,否则您可能会使用

for i in "$foo_0" "$foo_1" "$foo_2" ... ; do
...
done

并删除命名错误的伪数组。而且,如果您对 foo_x 的数量有上限,则各种 foo 中没有特殊字符(特别是 $ 中没有字符IFS(默认为 ),那么您可以使用 shell 的空参数折叠功能:

$ for i in $foo_0 $foo_1 $foo_2 ; do
> echo '***' $i
> done
*** john

并允许 shell 忽略 unset foo_x

You'll need an eval and a deferred sigil:

$ foo_0=john
$ count=0    
$ name="\$foo_$count"
$ echo $name
$foo_0
$ eval echo "$name"    
john

but unless the index is truly important to you, you might use

for i in "$foo_0" "$foo_1" "$foo_2" ... ; do
...
done

and get rid of the badly named pseudo-array. And, if you have an upper bound on the number of the number of foo_x and there are no special characters in the various foos (in particular no character in $IFS which defaults to <space><tab><return>) then you can use the null-argument collapsing feature of the shell and:

$ for i in $foo_0 $foo_1 $foo_2 ; do
> echo '***' $i
> done
*** john

and allow the shell to ignore unset foo_x

画中仙 2024-09-25 12:23:23

我已经很长时间没有使用过 Bourne shell,但是你尝试过 eval 命令吗?

It's been a very long time since I've done any Bourne shell but have you tried the eval command?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文