分页: 1 / 1

[问题]如何将句子赋值给一个变量!

发表于 : 2008-05-11 16:40
coldweb
大家好,我新学SHELL在这向大家请教个问题:
比如说我有个名字叫FILE的文件,它里面的内容如下:
This is a test line1
This is a test line2
This is a test line3
This is a test line4
This is a test line5
This is a test line6
我现在想把FILE文件里的每行整行都赋值给line这个变量
我写了如下命令:
for line in `cat FILE`
do
echo $line
done
exit
可是执行了之后却是显示每个单词为一行,请问要怎么赋值????
This
is
a
test
line1
This
is
a
test
line2
This
is
a
test
line3
This
is
a
test
line4
This
is
a
test
line5
This
is
a
test
line6

发表于 : 2008-05-11 16:47
eexpress
for的时候,就已经把`cat FILE`的结果解开了的。所以line读入的,都是按照空格分开的参数了。

发表于 : 2008-05-11 16:53
thword

代码: 全选

while read line
do
echo $line
done<FILE 

发表于 : 2008-05-11 17:08
coldweb
谢谢各位,呵呵,又学到了 ,不断向各位取经!

发表于 : 2008-05-12 2:12
coldweb
好贴

发表于 : 2008-05-12 12:27
xiechy
改IFS也可以

代码: 全选

OldIFS="$IFS";
IFS=$'\n'
....
IFS="$OLDIFS"