简体   繁体   English

bash中的变量递增

[英]Variable incrementing in bash

Consider the following script: 请考虑以下脚本:

#!/bin/bash

num=0
cat file | while read line; do
    echo "$line"
    lines[$num]="$line"
    ((num++))
    echo "num = $num"
done

echo "end num = $num"

i=0
while [ $i -lt $num ]; do
    echo "${lines[$i]}"
    ((i++))
done

Normally, it should read the file line by line, store the result in an array, then go through the array and print it line by line. 通常,它应该逐行读取文件,将结果存储在一个数组中,然后通过数组并逐行打印。 The problem is that the variable $num resets somehow after the first loop exits. 问题是变量$ num在第一个循环退出后以某种方式重置。 The output of this script for me is the following (using a file with some random garbage in it): 这个脚本的输出对我来说是(使用带有一些随机垃圾的文件):

dsfkljhhsdfsdfshdjkfgd
num = 1
fdfgdfgdfg
num = 2
dfgdfgdfgdfg
num = 3
dfgdfgdfgdfgdfg
num = 4
dfgdfgdfgdfgdfgd
num = 5
fgdfgdfgdfg
num = 6
dfgdfgdfgdfg
num = 7
dfgdfgdfgdfgdfg
num = 8
dfgdfgdfgdfg
num = 9
dfgdfgdgdgdg
num = 10
dfgdffgdgdgdg
num = 11
end num = 0

Why is this? 为什么是这样? How do I achieve to remember the variable? 如何记住变量? I am using bash 3.1.17 on SUSE Linux 10. 我在SUSE Linux 10上使用bash 3.1.17。

Why? 为什么? It's because this: 这是因为:

cat file | while read line; do
    echo "$line"
    lines[$num]="$line"
    ((num++))
    echo "num = $num"
done

runs the while statement in a separate process, with its own environment, not touching the parent environment. 在单独的进程中运行while语句,具有自己的环境,而不是触及父环境。 You'll find, similarly, that the lines array is not there either. 同样地,你会发现lines数组也不存在。

The following simplified script shows this in action: 以下简化脚本显示了此操作:

#!/bin/bash
export xyzzy=42
echo urk | while read line; do
    xyzzy=999
    echo $xyzzy
done
echo $xyzzy

The output of that script is: 该脚本的输出是:

999
42

because the setting of the variable to 999 is done in the subprocess. 因为变量设置为999是在子进程中完成的。

Bottom line, if you want information to be reflected in the current process (the script), you'll need to do the work in the script or find some other way to get the information out of the sub-process. 最重要的是,如果您希望信息反映在当前流程(脚本)中,您需要脚本中完成工作或找到其他方法从子流程中获取信息。

If you use input redirection rather than starting a sub-process pipeline, it should work as you want. 如果您使用输入重定向而不是启动子流程管道,它应该可以按您的意愿工作。 That's because the while bit is then done in the context of the current process rather than a separate process in a pipeline. 这是因为while位然后在当前进程的上下文中完成,而不是在管道中的单独进程。 For example: 例如:

#!/bin/bash
export xyzzy=42
while read line; do
    xyzzy=999
    echo $xyzzy
done <<EOF
hello
EOF
echo $xyzzy

will produce: 将产生:

999
999

For your specific case, replace: 对于您的具体情况,请替换:

done <<EOF
hello
EOF

with: 有:

done <file

To add to the previous answer: and to avoid that (and the UUOC), you'll need something like this: 要添加到上一个答案:并且为了避免(和UUOC),你需要这样的东西:

while ...; do
  ...
done < file

Simply keep your variables all in the same environment. 只需将变量保存在同一环境中即可。 On line 4, remove "cat file | ". 在第4行,删除“cat file |”。 On line 9, append " < file". 在第9行,追加“<file”。 Your end num will now contain the expected 11, and the lines array will print one element at a time via your second loop. 你的结束数现在将包含预期的11,而lines数组将通过你的第二个循环一次打印一个元素。

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

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