问题描述
#!/bin/bash
for ((var=0; var<20; var++))
do
echo " Number is: $(grep 'Multiple_Frame = echo **$var**' 20mrf.txt | wc -l)" >>statisic.txt
done
这shell程序不能产生正确的结果,这可能错误变量的原因在第二个grep命令返回。
This shell program cannot produce correct result which maybe the reason of wrong variable returning in the second grep command.
我怎么可以grep第二回声句子中的变量?根据无功变化给grep不同的东西?
How can I grep a variable within the second echo sentence? to grep different things according to the var changing?
非常感谢!
推荐答案
正如其他人所指出的,问题是变量的单引号prevent扩张。然而,使用 $()
允许您使用双引号:
As others have stated, the problem is that the single quotes prevent expansion of the variable. However, using $()
allows you to use double quotes:
echo " Number is: $(grep "Multiple_Frame = echo **$var**" 20mrf.txt | wc -l)" >>statisic.txt
虽然我怀疑这样的事情是你的意思:
although I suspect something like this is what you meant:
echo " Number is: $(grep "Multiple_Frame = $var" 20mrf.txt | wc -l)" >>statisic.txt
您也应该知道,的grep
有一个选项可以输出在计数这样你就可以省略厕所
:
You should also be aware that grep
has an option to output the count so you can omit wc
:
echo " Number is: $(grep -c "Multiple_Frame = $var" 20mrf.txt)" >>statisic.txt
这篇关于如何到grep在shell程序中的变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!