深入理解Shell变量:使用、预设与调试技巧
1. 同时读取多个变量
在Shell脚本中,我们可以在一条语句里读取多个变量。以下脚本从数据文件读取第一行,并将单词赋值给指定的变量:
$ cat datafile
the quick brown fox
$ read field1 field2 field3 < datafile
$ echo Field one is $field1
Field one is the
$ echo Field two is $field2
Field two is quick
$ echo Field three is $field3
Field three is brown fox
若输入字段不足,部分变量会为空:
$ echo the quick > datafile
$ read field1 field2 field3 < datafile
$ echo Field one is $field1
Field one is the
$ echo Field two is $field2
Field two is quick
$ echo Field three is $field3
Field three is
不过,像下面这样设置变量是无法达到预期效果的:
echo 1 2 3 4 | read one two three
超级会员免费看
订阅专栏 解锁全文
4

被折叠的 条评论
为什么被折叠?



