无法读取,这是一个while循环中从存储变量,出来的时候while循环 [英] Can't read variable that was stored from within a while loop, when out of the while loop

查看:102
本文介绍了无法读取,这是一个while循环中从存储变量,出来的时候while循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能为我的生活为什么我不能读while循环外postPrioity。
我试图出口postPrioity =500,仍然没有奏效。

任何想法?

- 或者在计划文本 -

 #!/斌/庆典
猫/files.txt|而读namesInFile;做
postPrioity =500
#此一所示的$ postPrioityvarible,如'500'
回声weeeeeeeeee ---> $ postPrioity< --- 1
DONE
#这个人来了为$ postPrioity varible。哎呀
回声weeeeeeeeee ---> $ postPrioity< --- 2

OUTPUT:(我只有在files.txt 3文件名)

  weeeeeeeeee ---> 500℃; --- 1
weeeeeeeeee ---> 500℃; --- 1
weeeeeeeeee ---> 500℃; --- 1
weeeeeeeeee ---> < --- 2


解决方案

管道运营商创建一个子shell,请参阅BashPitfalls BashFAQ 。解决办法:不要使用,也没用反正

 #!/斌/庆典
postPriority = 0
而读namesInFile

    postPrioity = 500
    回声weeeeeeeeee ---> $ postPrioity< --- 1
完成< /files.txt
回声weeeeeeeeee ---> $ postPrioity< --- 2

I can't for the life of me see why I can not read the postPrioity outside the while loop. I tried "export postPrioity="500"" still didn't work.

Any ideas?

-- or in plan text --

#!/bin/bash
cat "/files.txt" | while read namesInFile; do	
			postPrioity="500"
			#This one shows the "$postPrioity" varible, as '500'
			echo "weeeeeeeeee ---> $postPrioity <--- 1"
done
			#This one comes up with "" as the $postPrioity varible. GRRR
			echo "weeeeeeeeee ---> $postPrioity <--- 2"

OUTPUT: (I only have 3 file names in files.txt)

weeeeeeeeee ---> 500 <--- 1
weeeeeeeeee ---> 500 <--- 1
weeeeeeeeee ---> 500 <--- 1
weeeeeeeeee --->  <--- 2

解决方案

The pipe operator creates a subshell, see BashPitfalls and BashFAQ. Solution: Don't use cat, it's useless anyway.

#!/bin/bash
postPriority=0
while read namesInFile
do   
    postPrioity=500
    echo "weeeeeeeeee ---> $postPrioity <--- 1"
done < /files.txt
echo "weeeeeeeeee ---> $postPrioity <--- 2"

这篇关于无法读取,这是一个while循环中从存储变量,出来的时候while循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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