控制

控制语句
if 控制语句

case 控制语句


for 控制语句



while 控制语句

unitl 控制语句

break continue
函数

Last updated










Last updated
if [条件 1]; then
# 执行第一段程序
else
# 执行第二段程序
fiif [条件 1]; then
# 执行第一段程序
elif [条件 2]; then
# 执行第二段程序
else
# 执行第三段程序
fi#!/bin/bash
echo "Press y to continue"
read yn
if [ $yn = "y" ]; then
echo "script is running..."
else
echo "stopped!"
ficase $变量名称 in
" 第一个变量内容 " )
# 程序段一
;;
" 第二个变量内容 " )
# 程序段二
;;
* )
# 其它程序段
exit 1
esac#!/bin/bash
echo "This script will print your choice"
case "$1" in
"one" )
echo "your choice is one"
;;
"two" )
echo "your choice is two"
;;
"three" )
echo "Your choice is three"
;;
* )
echo "Error Please try again!"
exit 1
;;
esac#!/bin/bash
echo "Please input your choice:"
read choice
case "$choice" in
Y | y | yes | Yes | YES )
echo "It's right"
;;
N* | n*)
echo "It's wrong"
;;
*)
exit 1
esacfor (( 初始值; 限制值; 执行步阶 ))
do
# 程序段
done#!/bin/bash
declare -i sum
for (( i=1; i<=100; i=i+1 ))
do
sum=sum+i
done
echo "The result is $sum"for var in con1 con2 con3 ...
do
# 程序段
done#!/bin/bash
for i in 1 2 3 4 5 6 7 8 9
do
echo $i
done#!/bin/bash
for name in `ls`
do
if [ -f $name ];then
echo "$name is file"
elif [ -d $name ];then
echo "$name is directory"
else
echo "^_^"
fi
donewhile [ condition ]
do
# 程序段
done#!/bin/bash
declare -i i
declare -i s
while [ "$i" != "6" ]
do
s+=i;
i=i+1;
done
echo "The count is $s"until [ condition ]
do
#程序段
done#!/bin/bash
declare -i i
declare -i s
until [ "$i" = "6" ]
do
s+=i;
i=i+1;
done
echo "The count is $s"breakcontinue函数名()
{
# 命令 ...
}function 函数名()
{
# 命令 ...
}#!/bin/bash
is_it_directory()
{
if [ $# -lt 1 ]; then
echo "I need an argument"
return 1
fi
if [ ! -d $1 ]; then
return 2
else
return 3
fi
}
echo -n "enter destination directory:"
read direct
is_it_directory $direct
echo "the result is $?"