如何在循环内部访问按循环顺序命名的变量?

发布于 2024-07-24 14:23:27 字数 1438 浏览 6 评论 0原文

我试图了解是否可以在循环中创建一组基于另一个变量(使用 eval)进行编号的变量,然后在循环结束之前调用它。

作为一个例子,我编写了一个名为 Question 的脚本(第一个命令是显示变量 $tab 的内容是什么)

(23:32:12\[[email protected])
[~/bin]$ listQpsk 40|grep -w [1-4]
40 SMANHUBAQPSK1          1    1344      1195        88
40 SMANHUBAQPSK1          2    1668      1470        88
40 SMANHUBAQPSK1          3    1881      1539        81
40 SMANHUBAQPSK1          4    1686      1409        83


(23:18:42\[[email protected])
[~/bin]$ cat question
#!/usr/bin/bash
tab=`listQpsk 40|grep -w [1-4]`
seq=1
num=4
until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
done
echo $count1
echo $count2
echo $count3
echo $count4

当我运行这个脚本时,我得到

(23:32:23\[[email protected])
[~/bin]$ ./question 
1195
1471
1538
1409

的正是我所期望的,但是有没有办法移动echo 命令位于until 循环内部,以便循环的一部分回显刚刚创建的变量的值。 类似于:

until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
echo "$count$seq"
done

PS:抱歉,如果我的格式不对...第一次在这里发帖,我只知道 Reddit 上的 Markdown。

I'm trying to understand if it's possible to create a set of variables that are numbered based on another variable (using eval) in a loop, and then call on it before the loop ends.

As an example I've written a script called question (The fist command is to show what is the contents of the variable $tab)

(23:32:12\[[email protected])
[~/bin]$ listQpsk 40|grep -w [1-4]
40 SMANHUBAQPSK1          1    1344      1195        88
40 SMANHUBAQPSK1          2    1668      1470        88
40 SMANHUBAQPSK1          3    1881      1539        81
40 SMANHUBAQPSK1          4    1686      1409        83


(23:18:42\[[email protected])
[~/bin]$ cat question
#!/usr/bin/bash
tab=`listQpsk 40|grep -w [1-4]`
seq=1
num=4
until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
done
echo $count1
echo $count2
echo $count3
echo $count4

When I run this I get

(23:32:23\[[email protected])
[~/bin]$ ./question 
1195
1471
1538
1409

Which is exactly what I would expect, but is there a way to move the echo commands inside of the until loop so that part of the loop is echoing the value of the variable that was just created. Something like:

until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
echo "$count$seq"
done

PS: Sorry if my formatting is off...first time posting here, and I only know markdown from reddit.

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

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

发布评论

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

评论(4

沩ん囻菔务 2024-07-31 14:23:27

使用间接:

until [[ $seq -gt $num ]];do
    var="count$seq"
    eval $var=$(echo "$tab"|awk -v seq=$seq '$3==seq {print $5}') # awk loves to do grep's job
    let seq+=1    # another way
    echo "${!var}"    # indirection
done

不需要再次评估。

Use indirection:

until [[ $seq -gt $num ]];do
    var="count$seq"
    eval $var=$(echo "$tab"|awk -v seq=$seq '$3==seq {print $5}') # awk loves to do grep's job
    let seq+=1    # another way
    echo "${!var}"    # indirection
done

No need for another eval.

青朷 2024-07-31 14:23:27

不完全回答你的问题,但是......你知道 bash 有数组变量吗?

seq=1
num=4
until [[ $seq -gt $num ]];do
count[$seq]=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
done
echo ${count[*]}

或者没有数组:

seq=1
num=4
until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
eval echo \$count$seq
seq=$(($seq+1))
done

Not exactly answering your question, but... did you know bash has array variables?

seq=1
num=4
until [[ $seq -gt $num ]];do
count[$seq]=`echo "$tab"|grep -w $seq|awk '{print $5}'`
seq=$(($seq+1))
done
echo ${count[*]}

Or without arrays:

seq=1
num=4
until [[ $seq -gt $num ]];do
eval count$seq=`echo "$tab"|grep -w $seq|awk '{print $5}'`
eval echo \$count$seq
seq=$(($seq+1))
done
盛夏已如深秋| 2024-07-31 14:23:27

是的,像这样(至少在 bash 中):

$ count1=xyz
$ seq=1
$ value=`eval echo \$\{count$seq\}`
$ echo $value

Yes, like this (in bash at least):

$ count1=xyz
$ seq=1
$ value=`eval echo \$\{count$seq\}`
$ echo $value
温柔嚣张 2024-07-31 14:23:27

尝试一下:

varName=a
for i in 1 2 3
do
   eval $varName$i=stuff
   eval var=\$varName$i
   echo $var
done

某些版本的 shell 还允许您使用 ${!var} 间接引用变量

Try this:

varName=a
for i in 1 2 3
do
   eval $varName$i=stuff
   eval var=\$varName$i
   echo $var
done

some versions of shell also let you use ${!var} to indirectly reference variables

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