QQ登录

只需一步,快速开始

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 1963|回复: 0

shell的数组该怎么赋值?

[复制链接]
发表于 2009-12-16 15:41:44 | 显示全部楼层 |阅读模式
有一个shell如下:
#!/bin/bash

declare -a text_lines

line_count=0

IFS=$'n'
cat test.txt |
while read str_line;
do
        echo "${str_line}"
        text_lines[${line_count}]="${str_line}"
        ((line_count++))
done

num_lines=${#text_lines[@]}

echo "num_lines=$num_lines"

for (( indx=0 ; indx<=$num_lines ; indx++ ))
do
                echo -e "<$indx>t${text_lines[$indx]}"
done  

exit 1

test.txt的内容如下:
1. main()
2. {printf("this is first program");
3. }

运行结果如下:
1. main()
2. {printf("this is first program");
3. }
num_lines=0
<0>
可见数组并没有赋值成功,这是怎么回事啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

GMT+8, 2024-4-25 12:11 , Processed in 0.063579 second(s), 15 queries .

© 2021 Powered by Discuz! X3.5.

快速回复 返回顶部 返回列表