如果我有一个名称为“ read7”的文件,其中包含数字列表,例如:
2
3
4
如何编写带有while循环的bash脚本,以读取文件中的所有元素并对数字求平方并将其作为标准输出发送?
因此,在运行脚本之后,我们应该得到一个输出
4
9
16
如果要使用while
循环,可以说:
while read -r i; do echo $((i*i)); done < read7
对于您的输入,它将发出:
4
9
16
根据您的意见,if the file has words and numbers in it. How do I make it read just the numbers from the file?
。你可以说:
while read -r i; do [[ $i == [0-9]* ]] && echo $((i*i)); done < read7
对于包含以下内容的输入文件:
2
foo
3
4
它会产生:
4
9
16
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句