控制流结构
来源:互联网 发布:java闭包是什么 编辑:程序博客网 时间:2024/05/16 19:04
1.if语句
if条件1
then
命令1
elif条件2
then
命令2
else
命令3
fi
------------------
if条件
then命令
fi
eg:
#!/bin/bash
#if test
#this is a comment line
if [ "10" -lt "12" ];then
#yes 10 is less than 12
echo "yes,10 is less than 12"
else
echo "no"
fi
注意:if语句必须以fi终止
"10"前一个空格,“12”后也有一个空格。这个条件都是通过test命令来指定。条件表达为test expression或者[expression]
条件表达式中的比较函数
man test
NAME
test - check file types andcompare values
SYNOPSIS
test EXPRESSION
[ EXPRESSION ]
[ OPTION
DESCRIPTION
Exit with the statusdetermined by EXPRESSION.
--help display this help andexit
--version
outputversion information and exit
EXPRESSION is true or falseand sets exit status. It is one of:
( EXPRESSION )
EXPRESSION is true
! EXPRESSION
EXPRESSION is false
EXPRESSION1 -a EXPRESSION2
bothEXPRESSION1 and EXPRESSION2 are true
EXPRESSION1 -o EXPRESSION2
eitherEXPRESSION1 or EXPRESSION2 is true
[-n] STRING
thelength of STRING is nonzero
-z STRING
thelength of STRING is zero
STRING1 = STRING2
thestrings are equal
STRING1 != STRING2
the strings are not equal
INTEGER1 -eq INTEGER2
INTEGER1is equal to INTEGER2
INTEGER1 -ge INTEGER2
INTEGER1is greater than or equal to INTEGER2
INTEGER1 -gt INTEGER2
INTEGER1is greater than INTEGER2
INTEGER1 -le INTEGER2
INTEGER1is less than or equal to INTEGER2
INTEGER1 -lt INTEGER2
INTEGER1is less than INTEGER2
INTEGER1 -ne INTEGER2
INTEGER1is not equal to INTEGER2
FILE1 -ef FILE2
FILE1and FILE2 have the same device and inode numbers
FILE1 -nt FILE2
FILE1 isnewer (modification date) than FILE2
FILE1 -ot FILE2
FILE1 isolder than FILE2
-b FILE
FILEexists and is block special
-c FILE
FILEexists and is character special
-d FILE
FILEexists and is a directory
-e FILE
FILEexists
-f FILE
FILEexists and is a regular file
-g FILE
FILEexists and is set-group-ID
-h FILE
FILEexists and is a symbolic link (same as -L)
-G FILE
FILEexists and is owned by the effective group ID
-k FILE
FILEexists and has its sticky bit set
-L FILE
FILEexists and is a symbolic link (same as -h)
-O FILE
FILEexists and is owned by the effective user ID
-p FILE
FILEexists and is a named pipe
-r FILE
FILEexists and is readable
-s FILE
FILEexists and has a size greater than zero
-S FILE
FILEexists and is a socket
-t [FD]
filedescriptor FD (stdout by default) is opened on a terminal
-u FILE
FILEexists and its set-user-ID bit is set
-w FILE
FILEexists and is writable
-x FILE
FILEexists and is executable
eg.
#!/bin/bash
#if test
#this is a comment line
echo "Enter your filename:"
read myfile
if [ -e $myfile ]
then
if [ -s $myfile ];then
echo "$myfile exist and size greaterthan zero"
else
echo "$myfile exist but size iszero"
fi
else
echo "file no exist"
fi
[test@szbirdora 1]$ sh iftest.sh
Enter your filename:
11
11 exist but size is zero
2.case语句
case语句为多选择语句。
case值 in
模式1)
命令1
;;
模式2)
命令2
;;
esac
eg.
#!/bin/bash
#case select
echo -n "enter a number from 1 to 3:"
read ans
case $ans in
1)
echo "you select 1"
;;
2)
echo "you select 2"
;;
3)
echo "you select 3"
;;
*)
echo "`basename $0`:this is not between 1 and3">2
exit;
;;
esac
3.for循环
for循环一般格式:
for变量名 in 列表 (列表以空格作为分割)
do
命令1
命令2
done
eg:
#!/bin/bash
#forlist1
for loop in 1 2 3 4 5
do
echo $loop
done
4.until循环
until条件
do
命令1
命令2
...
done
条件测试发生在循环末尾,所以循环至少可以执行一次。
5.
while循环
while命令 (可以是一个命令也可以是多个,做条件测试)
do
命令1
命令2
...
done
注意:如果从文件中读入变量<filename要放到done后
6.break和continue控制
break跳出,continue跳过
- Shell控制结构--流控制
- 控制流结构
- 控制流结构
- shell控制流结构
- 控制流结构
- 控制流结构
- 控制流结构
- Shell控制流结构笔记
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- 控制结构
- shell_04控制流结构_笔记
- shell脚本中的控制流结构
- 控制流结构--shell编程笔记
- shell脚本中的控制流结构
- bat脚本自动备份代码,并压缩
- pthread_mutex_init做了什么呢?
- NSDictionary 使用(六)
- android intent 传递list或者对象
- cocos2d-x lua 中使用protobuf并对http进行处理
- 控制流结构
- Android在新进程中启动 Service 的流程原理分析
- gvim linux ubuntu nautilus 右键菜单插件介绍
- Spring属性占位符PropertyPlaceholderConfigurer的使用 ${}
- 【转】C#解决MDI窗体闪屏的方法
- cocos2d 中 scene(), create(), init() 调用关系
- 例程:如何使用PX2硬解码H.264裸码流 [CODE_PX2]Decode_RAW_H264_FI
- Magento Registry (Mage::register)使用方法
- Linux 把多个文件指定到同一目录下