shell scripts学习(四)
2014-01-06 14:50
435 查看
循环(loop)
1. while......do.....done循环
语法格式:
while [ condition ]#continue为真的时候开始循环,假的时候停止循环
do
程序段
done
ex
[root@linux ~]# vim sh12.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
2. until [ continue ] #--------------->continue为假的时候开始循环,真的时候停止循环
do
程序段落
done
ex:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
until [ "$yn" == "yes" ] && [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
3. for......do......done循环
数值方面循环语法格式:
for (( 初始值; 限制值; 执行步阶))
do
程序段
done
ex: ----------->执行从1累加到100
主要程序段:
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is==>$s"
非数值方面的循环语法格式
for variable in content1 content2 content3
do
程序段
done
说明:
第一次循环时,$variable的内容是content1
第二次循环时,$variable的内容是content2
第三次循环时,$variable的内容是content3
ex:
for animal in dog cat elephant
do
echo "There are ""$animal""s..."
done
1. while......do.....done循环
语法格式:
while [ condition ]#continue为真的时候开始循环,假的时候停止循环
do
程序段
done
ex
[root@linux ~]# vim sh12.sh
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
while [ "$yn" != "yes" ] && [ "$yn" != "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
2. until [ continue ] #--------------->continue为假的时候开始循环,真的时候停止循环
do
程序段落
done
ex:
#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH
until [ "$yn" == "yes" ] && [ "$yn" == "YES" ]
do
read -p "Please input yes/YES to stop this program:" yn
done
3. for......do......done循环
数值方面循环语法格式:
for (( 初始值; 限制值; 执行步阶))
do
程序段
done
ex: ----------->执行从1累加到100
主要程序段:
s=0
for (( i=1; i<=100; i=i+1 ))
do
s=$(($s+$i))
done
echo "The result of '1+2+3+...+100' is==>$s"
非数值方面的循环语法格式
for variable in content1 content2 content3
do
程序段
done
说明:
第一次循环时,$variable的内容是content1
第二次循环时,$variable的内容是content2
第三次循环时,$variable的内容是content3
ex:
for animal in dog cat elephant
do
echo "There are ""$animal""s..."
done
相关文章推荐
- 鸟哥的Linux私房菜学习笔记(三)学习 Shell 与 Shell scripts——正规表示法与文件格式化处理
- 鸟哥的Linux私房菜学习笔记(三)学习 Shell 与 Shell scripts——学习 shell scripts
- 鸟哥的Linux私房菜学习笔记(三)学习 Shell 与 Shell scripts——vim程序编辑器
- chapte13:学习shell scripts之(4)循环
- 12-学习Shell-Scripts
- 鸟哥的Linux私房菜——第十六章:学习Shell Scripts
- shell scripts学习(一)
- 学习鸟哥私房菜(七)之 shell scripts
- 鸟哥私房菜--第13章 学习Shell scripts(6)
- 鸟哥私房菜--第13章 学习 Shell Scripts(5)
- shell scripts学习(三)
- 鸟哥私房菜--第13章 学习 Shell Scripts(4)
- shell scripts的学习(五)
- chapte13:学习shell scripts之(1)简单的shell scripts
- chapte13:学习shell scripts之(2)判断式的使用
- 鸟哥私房菜--第13章 学习 Shell Scripts(3)
- shell scripts学习(一)
- shell scripts学习(二)
- 鸟哥linux学习笔记之学习Shell Scripts
- 第十三章 学习Shell Scripts