Shell 学习(七、循环语句的学习(for和while))
2012-10-17 14:36
453 查看
//============================================
#!/bin/bash
for a in `seq 1 2 10`
do
echo $a
done
a初始值为1, 然后 a=a+2的操作, 一直到 a<=10
---------------------
for((i=1;i<=10;i=i+2))
do
echo $i
done
for((i=1;i<=10;i++))
[17rumen@localhost ~]$ ./my_07.sh
1
3
5
7
9
//===============================================
统计文件数目
#!/bin/bash
i=0
for name1 in `ls /etc`
do
echo $name1
i=`expr $i + 1`
done
echo $i
//==============================================
#!/bin/bash
a=0
while [ $a -le 10 ]
do
((a=a+1))
if [ $a -eq 5 ]
then
continue
elif [ $a -eq 8 ]
then
break
fi
echo $a
done
-------------------
[17rumen@localhost ~]$ ./my_07.sh
1
2
3
4
6
7
//==========================================
下面是一个录入客户资料的shell脚本
#!/bin/bash
while true
do
echo "登记客户资料(c继续,q退出):"
read choice
case $choice in
c)
echo "请输入客户名字:"
read name1
echo "请输入客户年龄:"
read age1
echo "姓名:"${name1}" - 年龄:"${age1} >>customer.txt
;;
q)
exit
;;
esac
done
--------------------
>> 和 > 区别
>>customer.txt 追加保存到customer.txt文件中, 如果文件不存在会自动创建。
>customer.txt 就会重新写入, 覆盖原有的数据
#!/bin/bash
for a in `seq 1 2 10`
do
echo $a
done
a初始值为1, 然后 a=a+2的操作, 一直到 a<=10
---------------------
for((i=1;i<=10;i=i+2))
do
echo $i
done
for((i=1;i<=10;i++))
[17rumen@localhost ~]$ ./my_07.sh
1
3
5
7
9
//===============================================
统计文件数目
#!/bin/bash
i=0
for name1 in `ls /etc`
do
echo $name1
i=`expr $i + 1`
done
echo $i
//==============================================
#!/bin/bash
a=0
while [ $a -le 10 ]
do
((a=a+1))
if [ $a -eq 5 ]
then
continue
elif [ $a -eq 8 ]
then
break
fi
echo $a
done
-------------------
[17rumen@localhost ~]$ ./my_07.sh
1
2
3
4
6
7
//==========================================
下面是一个录入客户资料的shell脚本
#!/bin/bash
while true
do
echo "登记客户资料(c继续,q退出):"
read choice
case $choice in
c)
echo "请输入客户名字:"
read name1
echo "请输入客户年龄:"
read age1
echo "姓名:"${name1}" - 年龄:"${age1} >>customer.txt
;;
q)
exit
;;
esac
done
--------------------
>> 和 > 区别
>>customer.txt 追加保存到customer.txt文件中, 如果文件不存在会自动创建。
>customer.txt 就会重新写入, 覆盖原有的数据
相关文章推荐
- Shell脚本之for、while循环语句和case分支语句
- shell脚本学习9——循环指令:for、while、until
- shell脚本之循环语句for,while,until用法的详解
- Shell脚本中循环语句for,while,until用法
- shell脚本下的循环语句for、while、until
- PL/SQL语句学习之使用while、loop和for三种循环打印数字的1-10
- Shell中的循环语句for、while、until实例讲解
- python学习之语句:循环语句(for,while)
- 【Python学习笔记】for和while循环语句
- Objective-C学习笔记(十)——循环语句for和do-while的使用
- Objective-C学习笔记(十)——循环语句for和do-while的使用
- Shell中的循环语句for、while、until实例讲解
- shell基础之三:循环语句for,while,until
- Shell脚本中循环语句for,while,until用法
- shell基础学习中的字符串操作、for循环语句示例
- shell 中的for、while循环及if语句
- Shell_循环语句(for、while、case)深入讲解
- Shell脚本中循环语句for,while,until用法
- C++中的while循环和for循环语句学习教程
- Linux学习之Shell--第四天:Shell流程控制-循环语句for