如何在 shell 中使用 subshell 中的变量
how to use variable from subshell in shell
请你帮我如何在变量号中使用值。这个变量在while循环中取值,在subshell中。所以问题是我如何在 shell 中使用它。
数字=0
#blablabla
cat $WEDI_RC | while read line
do
number=$(echo $line | grep | awk -F'[ ]' '{print }')
echo "in cycle"
echo $number
done
echo "after while"
echo $number
((number++))
echo $number
echo `date +"%T"` $number >>$WEDI_RC
这是因为管道创建了一个子外壳,它有自己的计数。
这个问题肯定已经回答了很多次,所以要获得完整的解释,只需 link 到 bash 常见问题解答:
http://mywiki.wooledge.org/BashFAQ/024
通常的解决方法是从输入文件重定向输入而不是通过管道传输(您甚至可以摆脱无用的 cat
):
while read -r line
do
...
done < "$WEDI_RC"
...
请你帮我如何在变量号中使用值。这个变量在while循环中取值,在subshell中。所以问题是我如何在 shell 中使用它。 数字=0 #blablabla
cat $WEDI_RC | while read line
do
number=$(echo $line | grep | awk -F'[ ]' '{print }')
echo "in cycle"
echo $number
done
echo "after while"
echo $number
((number++))
echo $number
echo `date +"%T"` $number >>$WEDI_RC
这是因为管道创建了一个子外壳,它有自己的计数。
这个问题肯定已经回答了很多次,所以要获得完整的解释,只需 link 到 bash 常见问题解答:
http://mywiki.wooledge.org/BashFAQ/024
通常的解决方法是从输入文件重定向输入而不是通过管道传输(您甚至可以摆脱无用的 cat
):
while read -r line
do
...
done < "$WEDI_RC"
...