shell:for和while用法
2012-07-20 10:40
417 查看
写法一:
----------------------------------------------------------------------------
#!/bin/bash
while read line
do
echo $line
done < file(待读取的文件)
----------------------------------------------------------------------------
写法二:(并发脚本慎用,grep不能输出全部匹配的信息)
----------------------------------------------------------------------------
#!/bin/bash
cat file(待读取的文件) | while read line
do
echo $line
done
----------------------------------------------------------------------------
写法三:
----------------------------------------------------------------------------
for line in `cat file(待读取的文件)`
do
echo $line
done
----------------------------------------------------------------------------
说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
aaaa
bbbb fff ggg
cccc dddd
$ cat file | while read line; do echo $line; done
aaaa
bbbb fff ggg
cccc dddd
$ for line in $(<file); do echo $line; done
aaaa
bbbb
fff
ggg
cccc
dddd
----------------------------------------------------------------------------
#!/bin/bash
while read line
do
echo $line
done < file(待读取的文件)
----------------------------------------------------------------------------
写法二:(并发脚本慎用,grep不能输出全部匹配的信息)
----------------------------------------------------------------------------
#!/bin/bash
cat file(待读取的文件) | while read line
do
echo $line
done
----------------------------------------------------------------------------
写法三:
----------------------------------------------------------------------------
for line in `cat file(待读取的文件)`
do
echo $line
done
----------------------------------------------------------------------------
说明:
for逐行读和while逐行读是有区别的,如:
$ cat file
aaaa
bbbb fff ggg
cccc dddd
$ cat file | while read line; do echo $line; done
aaaa
bbbb fff ggg
cccc dddd
$ for line in $(<file); do echo $line; done
aaaa
bbbb
fff
ggg
cccc
dddd
相关文章推荐
- Shell脚本中循环语句for,while,until用法
- Shell脚本中循环语句for,while,until用法
- shell基础之四:函数、死循环及for,while特殊用法
- Shell脚本中循环语句for,while,until用法
- Shell语句 for、while、case、if、输出重定向、grep查询用法
- shell脚本之循环语句for,while,until用法的详解
- Shell脚本中的for,while用法
- 一文读懂Shell脚本中循环语句for,while,until用法
- shell脚本编程循环之for/while/untill循环
- shell学习-9-if-case-for-while-until-break
- Shell for用法
- javascript 循环语句 while、do-while、for-in、for用法区别
- Shell Script Examples: case, count, for, if, while and User input
- Shell 语法 if 、 case 、for 、 while、 until 、select 、repeat、子函数
- tar ,cpio打包解压. shell脚本for,while,until循环. rpm包管理,select循环菜单,函数function,yum
- for,while,do...while,switch,break,continue,return的用法
- shell 控制语句(if,for,while,case)
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- javascript 循环语句 while、do-while、for-in、for用法区别
- Shell API SHBrowseForFolder的用法