虽然Do循环和变量在bash脚本? [英] While Do loop and variables in a bash script?

查看:117
本文介绍了虽然Do循环和变量在bash脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个PHP程序员做一些bash脚本编程,我不知道如何全局变量都在工作。

欲增加每行的3个变量中提供的文件。然而,当我深究,变量仍设置为0。我如何访问是在WHILE中增加变量DO循环?我只是想呼应出来末..

据我了解我们在kornshell

 #!/斌/庆典排版-i I = 0
排版-i T1 = 0
排版-i T2 = 0
SED 1D $ 1 | \\而读字词1字词2 WORD3 word4的word5

        I = I + 1
        T1 = T1 + $ word4
        T2 = T2 + $的word5
DONE回声$我
回声$ T1
回声$ T2
退出0


解决方案

BASH FAQ条目#24:我设置变量在一个循环中。为什么他们突然消失循环终止后?或者说,我为什么不能管数据读?

I'm a PHP programmer doing some BASH scripting and I'm not sure how global variables are working.

I want to increment the 3 variables for each line in a supplied file. However, when I get to the bottom, the variables are still set at 0. How do I access the variables that are incremented within the WHILE DO loop? I just want to echo them out at the end..

From what I understand we're in kornshell

#!/bin/bash

typeset -i i=0
typeset -i t1=0
typeset -i t2=0


sed 1d $1 |  \

while read word1 word2 word3 word4 word5 
do
        i=i+1
        t1=t1+$word4
        t2=t2+$word5
done

echo $i
echo $t1
echo $t2


exit 0

解决方案

BASH FAQ entry #24: "I set variables in a loop. Why do they suddenly disappear after the loop terminates? Or, why can't I pipe data to read?"

这篇关于虽然Do循环和变量在bash脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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