【发布时间】:2014-04-25 04:44:35
【问题描述】:
我正在使用我创建的数字在 bash 中创建一个创建 histoplot 的程序。这些数字是这样存储的(其中第一个数字是文件的一行上有多少个单词,第二个数字是给定文件中一行上的这个单词数量出现了多少次。)
1 1
2 4
3 1
4 2
这应该产生:
1 #
2 ####
3 #
4 ##
但是我得到的输出是:
1 #
2 #
3 #
4 #
但是 for 循环没有识别出我的变量“hashNo”是一个数字。
#!/bin/bash
if [ -e $f ] ; then
while read line
do
lineAmnt=${line% *}
hashNo=${line##* }
#VVVV Problem is this line here VVVV
for i in {1..$hashNo}
#This line ^^^^^^^ the {1..$hashNo}
do
hashes+="#"
done
printf "%4s" $lineAmnt
printf " $hashes\n"
hashes=""
done < $1
fi
如果我将 hashNo 替换为数字(例如 4 在我的输出中产生 4 个哈希),则代码可以工作,但它需要能够随每一行更改(文件中的所有行都不会包含相同数量的字符) .
感谢您的帮助:D
【问题讨论】:
-
很多很多问题的重复。
标签: linux bash for-loop terminal