没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Linux Shell编程(9)- 流程控制语句(三)循环语句
---
## 一、for循环
### 1. 语法
#### **语法1**
```
for 变量 in 值1 值2 值3 ... 值n
do
程序
done
```
#### **语法2**
```
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
```
### 2. 应用
#### **批量解压缩脚本**
* for_tar.sh
```
#!/bin/bash
cd /tmp/tmp
for i in $(ls *.tar.gz)
do
tar -zxf $i & > /dev/null
done
```
* 为 for_tar.sh脚本添加执行权限
```
[root/tmp]# chmod +x for_tar.sh
```
* 运行脚本前
```
[root/tmp]# ll tmp/
总用量 20K
-rw-r--r-- 1 root root 167 6月 5 11:13 file1.tar.gz
-rw-r--r-- 1 root root 119 6月 5 11:12 file2.tar.gz
-rw-r--r-- 1 root root 120 6月 5 11:12 file3.tar.gz
-rw-r--r-- 1 root root 119 6月 5 11:12 file4.tar.gz
-rw-r--r-- 1 root root 120 6月 5 11:12 file5.tar.gz
```
* 运行脚本后
```
[root/tmp]# ./for_tar.sh
[root/tmp]# ll tmp/
总用量 40K
-rw-r--r-- 1 root root 43 6月 5 11:13 file1
-rw-r--r-- 1 root root 167 6月 5 11:13 file1.tar.gz
-rw-r--r-- 1 root root 5 6月 5 11:11 file2
-rw-r--r-- 1 root root 119 6月 5 11:12 file2.tar.gz
-rw-r--r-- 1 root root 5 6月 5 11:12 file3
-rw-r--r-- 1 root root 120 6月 5 11:12 file3.tar.gz
-rw-r--r-- 1 root root 5 6月 5 11:12 file4
-rw-r--r-- 1 root root 119 6月 5 11:12 file4.tar.gz
-rw-r--r-- 1 root root 5 6月 5 11:12 file5
-rw-r--r-- 1 root root 120 6月 5 11:12 file5.tar.gz
```
#### **批量创建用户**
* for_useradd.sh
```
#!/bin/bash
#批量添加指定数量的用户实例
read -t 30 -p "input user name:" name
read -t 30 -p "input password:" pass
read -t 30 -p "input user number:" num
#检查输入内容是否都为非空
if [ -z "$name" -o -z "$pass" -o -z "$num" ]
then
echo "Error,must be input name,passw
点击阅读更多
资源评论
会飞的黄油
- 粉丝: 22
- 资源: 303
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功