简体   繁体   English

在bash脚本中执行循环和变量吗?

[英]While Do loop and variables in a bash script?

I'm a PHP programmer doing some BASH scripting and I'm not sure how global variables are working. 我是一个PHP程序员做一些BASH脚本,我不确定全局变量是如何工作的。

I want to increment the 3 variables for each line in a supplied file. 我想为提供的文件中的每一行增加3个变量。 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? 但是,当我到达底部时,变量仍设置为0.如何访问在WHILE DO循环中递增的变量? I just want to echo them out at the end.. 我只是想在最后回应它们。

From what I understand we're in kornshell 从我的理解,我们在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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM