为什么我不能这样AWK语句的输出保存到一个变量 [英] why can I not save the output of this awk statement to a variable

查看:580
本文介绍了为什么我不能这样AWK语句的输出保存到一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前想做一个变量名,将包含另一个变量的

I am currently trying to make a variable name that would consist of another variable

while [ "$countf" -le 9 ]; do
    vname=$( echo fcp"$countf" )
    $vname=$( awk -F, -vs="\$fc$countf" '{for (i=1;i<=NF;i++)if($i~"^"s"$"){print i;exit;}}{print "not found"}' <<< $first_line )
    countf=$(( countf + 1 ))
done

虽然当我去执行,包括code,线沿线的一些脚本输出如下:

although when I go to execute the the script that includes the code, something along the lines of the following is outputted:

fcp1=not: command not found

FCP1作为VNAME变量的内容。我试过几个不同的解决方案,但没有得到任何东西作为尚未工作,现在,如果有人能指出我在做什么错了,虽然我真的AP preciate它,谢谢。

fcp1 being the content of the vname variable. I've tried several different solutions but have not gotten anything to work yet as of right now, if someone could point out what I am doing wrong though I would really appreciate it, thanks.

推荐答案

您已经犯了一个错误,而不是

You've made a mistake, instead of

$vname=$(... )

你应该使用:

vname=$(... )

您不能在分配的这样的左边用 $

You can't use $ in the left of assignation like this.

一个解决方法是使用声明如果你想要做的间接变量引用的:

A workaround is to use declare if you want to do indirect variable references :

$ x=var
$ declare $x=test
$ echo $var
test

注意

作为讨论这个THEAD提到,不要使用评估来做到这一点。 评估的常见错别字。请参见 http://mywiki.wooledge.org/BashFAQ/048

As mentioned in discussion in this thead, don't use eval to do this. eval is a common misspelling of evil. See http://mywiki.wooledge.org/BashFAQ/048

这篇关于为什么我不能这样AWK语句的输出保存到一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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