将脚本输出放入循环bash中的变量
我有以下bash脚本,希望从中运行其他脚本并捕获结果:将脚本输出放入循环bash中的变量,bash,loops,variables,Bash,Loops,Variables,我有以下bash脚本,希望从中运行其他脚本并捕获结果: #!/bin/bash while read line; do echo "exit" | out=`python file.py` if [[ $out == *"WORD"* ]]; then echo $line >> out.txt fi done<$1 #/bin/bash 读行时;做 echo“exit”| out=`python file.py` 如果[[$o
#!/bin/bash
while read line; do
echo "exit" | out=`python file.py`
if [[ $out == *"WORD"* ]]; then
echo $line >> out.txt
fi
done<$1
#/bin/bash
读行时;做
echo“exit”| out=`python file.py`
如果[[$out==*“WORD”*];然后
echo$line>>out.txt
fi
已完成保留python执行
外部循环,因为它不依赖于任何循环变量:
#!/bin/bash
# initialize output file
> out.txt
# execute python script
out=$(echo "exit" | python file.py)
# loop
while read -r line; do
[[ "$out" == *"WORD"* ]] && echo "$line" >> out.txt
done < "$1"
#/bin/bash
#初始化输出文件
>out.txt
#执行python脚本
out=$(echo“exit”| python file.py)
#环路
而read-r行;做
[[“$out”==*“WORD”*]&&echo“$line”>>out.txt
完成<“$1”
在我添加的许多地方似乎也缺少引用。管道在子shell中运行,因此在父shell中无法看到其中的变量赋值。应该是:
out=$(echo exit | python file.py)
现在整个管道都在命令替换中,但变量赋值在原始shell中
echo "exit" | out=`python file.py`
应该是这样的(将file.py
的输出分配给out的结果发送“exit”-看起来很奇怪):
或(将“退出”作为输入发送到file.py,并将输出分配给out
):
取决于您试图实现的目标。echo“exit”| out=`python file.py`
应该类似于echo“exit”&&out=`python file.py`
或out=`echo“exit”| python file.py`
取决于您试图实现的目标,而echo exit
显然需要python脚本读取。
echo "exit" && out=`python file.py`
out=`echo "exit" | python file.py`