linux 控制结构
一.if
注:
格式1、格式2:一个条件一个命令;
格式3:一个条件两个命令;
格式4:两个条件三个命令,注意条件的写法。
例1:
#!/bin/sh
#ifTest
#to show the method of if
echo -e "Enter the first integer:\c"
read FIRST
echo -n "Enter the second integer:"
read SECOND
if [ "$FIRST" -gt "$SECOND" ]
then
echo "$FIRST is greater than $SECOND"
elif [ "$FIRST" -lt "$SECOND" ]
then
echo "$FIRST is less than $SECOND"
else
echo "$FIRST is equal to $SECOND"
fi
执行:
[root@cdh1 sh]# ./test.sh
Enter the first integer:7
Enter the second integer:7
7 is equal to 7
[root@cdh1 sh]# ./test.sh
Enter the first integer:8
Enter the second integer:7
8 is greater than 7
[root@cdh1 sh]#
例2:
#!/bin/bash
#5.sh
#declare a,b ;
a=$1
b=$2
if [ "$a" = "$b" ] ;then
echo "a=b"
else
echo "a!=b"
fi
例3.
read -p "Please input (Y/N): " yn
if [ "$yn" = "Y" ] || [ "$yn" = "y" ]; then
echo "OK, continue"
exit 0
fi
if [ "$yn" = "N" ] || [ "$yn" = "n" ]; then
echo "Oh, interrupt!"
exit 0
fi
echo "I don't know what your choice is" && exit 0
执行
[root@cdh1 sh]# ./2.sh
Please input (Y/N): y
OK, continue
二.case
例1
#!/bin/sh
#caseTest
#to test the method of case
USER=`whoami`
case $USER in
root)echo "You can do all the operations"
;;
Dave)echo "You can do some operations"
;;
*)echo "Sorry,you can not do anything"
;;
esac
执行:
[root@cdh1 sh]# ./test.sh
You can do all the operations
三、for
例子:
#!/bin/sh
#forTest
#to test the method of for
COUNTER=0
for aa in *
do
echo ${aa}
COUNTER=`expr $COUNTER + 1`
done
echo "There are $COUNTER files in `pwd` "
执行
[root@cdh1 sh]# ./test.sh
2
2.sh
3.sh
4.sh
5.sh
function.sh
test.sh
There are 7 files in /opt/data/sh
最新文章
- iOS检查App新版本并更新新版本
- 第二章 编写与设置Servlet
- virtaulbox视图模式常用切换
- Entity FrameWork 延迟加载本质(二)
- WebApi 返回小驼峰式 json 格式,并格式化日期
- hihocoder 1196 高斯消元.二
- EasyUI tab常用
- css和jQuery ,字符串中重新定义单个字符样式背景
- Java Web开发之Servlet、JSP基础
- 1629 B君的圆锥
- mahout的安装、配置及运行java程序
- Android_使用getIdentifier()获取资源Id
- Linux学习之Shell编程基础
- C# WinForm程序添加启动画面
- git之commit
- 莫烦theano学习自修第二天【激励函数】
- npm webpack工具 (监听压缩等)
- 【ASP.NET】 MVC下拉框联动
- HBase笔记之namespace
- php表达式
热门文章
- python socket 网络编程selector用法 (实用)
- DevExpress WinForms使用教程:WinForms Sunburst控件
- Java基础知识补充
- MyEclipse和Eclipse
- HihoCoder - 1483 区间最值
- L306 词汇题
- day 29 socket 理论
- Bug03_SSM整合_mybatis result maps collection already contains value...
- c++、Java、python对应的编译型语言和解释性语言区别详解
- arcgis server备份还原过程