shell入门之流程控制语句 分类: 学习笔记 linux ubuntu 2015-07-10 16:38 89人阅读 评论(0) 收藏
2023-09-27 14:27:41 时间
1.case
脚本:
#!/bin/bash
#a test about case
case $1 in
"lenve") echo "input lenve";;
"hello") echo "input hello";;
[a-zA-Z]) echo "It's a letter";;
[0-9]) echo "It's a number";;
esac
执行效果:
2.while
脚本(注意=两端不能有空格):
#!/bin/bash
#a test about while
a=1
while [ $a -lt 10 ]
do
echo "hello world!${a}"
a=`expr $a + 1`
done
输出:
3.until循环类似于while循环,不同的是until是判断条件为false时才会执行
#!/bin/bash
#a test about until
a=11
until [ $a -lt 10 ]
do
echo "hello world!${a}"
a=`expr $a + 1`
done
这是一个无限死循环,输出从hello world11到hello world无穷大。
4.break与continue
continue脚本
#!/bin/bash
#a test about continue
a=1
while [ $a -lt 10 ]
do
if [ $a -eq 5 ]
then
a=`expr $a + 1`
continue
else
echo "hello world!${a}"
fi
a=`expr $a + 1`
done
结果:
break脚本:
#!/bin/bash
#a test about break
a=1
while [ $a -lt 10 ]
do
if [ $a -eq 5 ]
then
a=`expr $a + 1`
break
else
echo "hello world!${a}"
fi
a=`expr $a + 1`
done
运行结果:
5.shift指令,参数左移,每执行一次,参数序列顺次左移一个位置,$#的位置减1。此指令可用来分别处理每个参数,移出去的参数不可再用。
一个求和的例子:
#!/bin/bash
#a test about shift
if [ $# -le 0 ]
then
echo "there is no parameters"
exit 0
fi
sum=0
while [ $# -gt 0 ]
do
sum=`expr $sum + $1`
shift
done
echo $sum
千万注意=两端不能有空格
运行结果:
版权声明:本文为博主原创文章,未经博主允许不得转载。若有错误地方,还望批评指正,不胜感激。
相关文章
- Linux 3.14 待机流程分析
- Linux下Oracle的sqlplus中上下左右退格键无法使用
- silent install oracle 11.2.0.1 x86_64 for linux
- Linux/CentOS各种服务框架的搭建完整流程
- Linux 核间IPI调度触发响应流程
- Linux下utf-8 BOM 的检查和删除
- linux文件系统工作流程解析——busybox学习笔记
- Linux 上 Nginx 获得最佳性能的 8 种方法
- 【Linux操作系统】程序的编译和动静态链接
- Linux - shell脚本练习
- 第14章 Linux开机详细流程
- Linux-024-Centos Nginx 代理配置:不同端口代理不同服务
- linux驱动开发流程和方法
- linux下编译安装curl
- linux云服务器锐速破解
- Linux 下Discuz论坛的搭建
- Linux 开机启动流程
- 基于Linux的视频传输系统(上大学时參加的一个大赛的论文)
- 【Linux】:Linux下创建用户和删除用户
- linux主机间复制文件
- linux中tar命令用法