循环退出后,局部变量 [英] Local variables after loop exit

查看:90
本文介绍了循环退出后,局部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在退出循环之后具有局部变量的一些问题。 ,变量最大结束值为0,尽管低于code:

I am having some issues with local variables after exiting a loop. The variable max ends up with the value 0, despite the code below:

max=0
cat tmp|while read line
do
temp=$(echo $line|tr -d "\n"|wc -c)
if [ $temp -gt $max ];then

max=$temp
echo $max
fi
done
echo -n tmp $max

cat tmp
12345
123456

这是我收到的输出:

And this is the output I receive:

5
6
 tmp 0

我不明白为什么最大退出循环后为0,而循环中找到正确的值。

I don't understand why max is 0 after exiting the loop, while inside the loop it finds the correct values.

推荐答案

一个管启动一个新的子shell,用它自己的环境和可变空间。使用< TMP 在循环结束吧。

A pipe starts a new subshell, with its own environment and variable space. Use < tmp at the end of the loop instead.

这篇关于循环退出后,局部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆