bash 之for循环
2016-01-31 10:41
525 查看
bash 之for循环
bash中,for也是一个关键字,我们可以用type命令验证一下:BeaverdeMacBook-Pro:opt Beaver$ type for for is a shell keyword
for循环的一个简单例子:
#! /bin/env bash for u in doctor who am i; do echo $u done
输出:
doctor
who
am
i
列表的生成我们可以用shell中的*,如:
#! /bin/env bash for file in * ; do stat $file done
输出:
BeaverdeMacBook-Pro:bashlearing Beaver$ bash 1.sh
16777220 8820797 -rw-r--r-- 1 Beaver staff 0 55 "Jan 31 10:26:58 2016" "Jan 31 10:26:56 2016" "Jan 31 10:26:56 2016" "Jan 31 10:06:15 2016" 4096 8 0 1.sh
列表的数据项也可以是另一个命令执行的结果,如我们需要打印当前所有登陆用户的当前工作目录:
#! /bin/env bash for user in $(who |cut -f1 -d " " ); do lsof -u $user -a -c bash |grep cwd done
我们可以利用break、continue控制循环。
相关文章推荐
- erlang shell 命令中文使用说明
- Xshell常用命令
- 自己写的一些shell脚本
- Shell脚本编程概述(三):函数和数组
- Ubuntu安装使用clustershell
- Linux环境变量profile bashrc
- adb命令和adb shell命令
- shell脚本
- 在adb shell中直接使用sqlite3命令操作数据库
- 理解 bashrc 和 profile
- xshell 5连接linux服务器的技巧
- shell环境变量配置文件-L64
- 统计文件中单词的个数---Shell及python版
- Windows PowerShell
- shell变量中再包含变量的情况!
- Linux Shell编程进阶案例实战(三)
- Linux Shell零基础案例实战(二)
- linux shell编程零基础入门
- 企业Shell实战-MySQL分库分表备份脚本
- shell脚本(输出每个内存条的信息)