我想实现一个bash脚本,它从管道命令中读取输入。我想处理输入中的每一行,并在每行上执行一些处理。另外,我想重用传递给脚本的输入。但是,我注意到我无法重用输入。下面是我的脚本:Ubuntu的bash脚本重用输入变量
#!/bin/bash
while read line
do
percentage=$(echo $line | awk 'printf $1')
# perform some processing using the total i.e. percentage/total*100
done
这个脚本是这样被执行:
cat data.txt | grep "status" | ./myscript.sh
的样本数据是:
1 STATUS
2 AUTHORISED
11 SENT
如果脚本修改为这样的事情:
#!/bin/bash
input=$1
total=$(cut -f1 $1)
echo $total
while read $input
do
percentage=$(echo $line | awk 'printf $1')
# perform some processing using the total i.e. percentage/total*100
done
Thi脚本只输出总数,而while循环没有执行。
此任务需要多个管道命令,因为此脚本将被许多事物使用,以便保持通用和可重用。如何在不将内容存储到文件的情况下执行此操作?
样品输入:
1 STATUS
2 AUTHORISED
11 SENT
总计将是14.因此,每个项目即,1/14 * 100,2/14 * 100和11/14 * 100。
您已经使用'削减-f1 $ 1' ,但是你没有通过命令行传递任何东西。所以,这个命令变成了'cut -f1',从而把'stdin'作为文件名。由于所有'stdin'耗尽,'read'在第一个'read'时失败,因此循环从不执行。 – anishsane
此外,你在用'while input $ input'尝试着什么? – anishsane
你是什么意思? –