![](/img/trans.png)
[英]while loop with global variable scope issue in shell script with psql
[英]Global Variable in shell script
我想在以下代碼中使z
成為一個全局變量:
#!/bin/bash
z=0;
find $1 -name "*.txt" | \
while read file
do
i=1;
z=`expr $i + $z`;
echo "$z";
done
echo "$z";
最后一個語句總是輸出“0”。 為什么?
翻譯的簡單方法
find ... | while read ...; done
到沒有管道的表單使用進程替換 :
while read ...; done < <(find ...)
可讀性有些受損。
我不知道為什么會這樣,但問題是由管道引起的。
如果你這樣做的話
#!/bin/bash
z=0;
for f in `find $1 -name "*.txt"`
do
i=1;
z=`expr $i + $z`;
echo "$z";
done
echo "$z";
然后$ z不會為零。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.