shell的一些常用的语句(if语句,变量,for、while、until语句、函数调用、脚本调用)
2013-07-18 23:59
1206 查看
1.if语句
echo "please input x"read x
echo "please input y"
read y
if [ $x -gt $y ]; 判断条件的[]中,必须在[只后有空格,]之前有空格。。]只有可以有分号;,也可以没有分号
then
echo "$x dayu $y"
elif [ $x -lt $y ];
then
echo "$x xiayu $y"
else
echo "$x dengyu $y"
fi
2.加法(乘法)
sum=`expr $x + $y` 整数的计算,要加expr,并且在+的两边要有空格echo “the result is $sum”
如果是乘法。`expr $x \* $y`需要“\”进行转译
3.保存前一个命令的返回码
[ “$x” = “$y” ]echo $? ?可以用于保存前一个命令的返回码。正确的返回0,错误的返回1
4.for语句
total=0for ((i=1;i<=100;i++)) for的后面是双括号(())。。一般(())的里面是用于一些计算的
do
total=`expr $total + $i`
done
echo “the result is $total ”
5.while语句
1)Total=0; num=0 在同一行,但是用分号隔开2)total=0 num=0 在同意行,但是没有分隔符 这三种的效果是一样的
3)total=0
num=0 用两行写
while((num<100)) while也是双括号(())
do
num=`expr $num + 1`
total=`expr $total + $num`
done
6.until中,条件为真结束循环
until [ $num -ge 100 ] until是[]的进行判断,这里就不能用<了,要是-gtdo
num=`expr $num + 1`
total=`expr $total + $num`
done
7. for循环里面有if的条件判断
for((i=1;i<=10;i++)) 这对这种的,for里面容易忘记写do和donedo
if((i%2==1))
then
rcho “jishu is $i”
else
echo “oushu is $i”
fi
Done
8. case语句
read xa=$x
case $a in
1)
echo “the num is 1”;; 每一句的最后都是有两个分号;;,这是必须的
2)
echo “the num is 2”; echo “hehhehe”;; 输出两行,并且是换行的,这两句之间用一个分号链接;
3|4)
echo “the num is 3 or 4”;; 一个条件有多个值,这个时候用“|”来隔开
*)
echo “the num is other”;; 对于其他可能出现的条件,用“*”表示
esac
9函数以及调用
add() 函数体{
sum=`expr $x + $y`
echo “the sum is $ sum”
}
echo “please input x”
read x
echo “please input y”
read y
add $x $y 函数调用
10.脚本的调用
在脚本1中如果调用脚本2。直接在脚本1中添加一行“./script2”相关文章推荐
- 70个shell常用操作、 shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Shell脚本中循环语句for,while,until用法
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell脚本下的循环语句for、while、until
- 一文读懂Shell脚本中循环语句for,while,until用法
- SHELL脚本语句 if、for、while、case 图示
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select .
- Shell脚本中循环语句for,while,until用法
- shell 脚本之if、for、while语句
- [shell基础]——if/for/while/until/case 语句
- Shell脚本中循环语句for,while,until用法
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- Shell if语句,case语句,循环for,until,while语句读书笔记
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select
- shell脚本之循环语句for,while,until用法的详解
- shell脚本之while for if 语句和编写计划任务
- Shell 脚本之循环语句(for,while,until)以及case,select,break,continue实例详解
- tar ,cpio打包解压. shell脚本for,while,until循环. rpm包管理,select循环菜单,函数function,yum
- shell脚本练习一(if语句、case语句、for语句、while语句)
- shell编程控制结构:expr、let、for、while、until、shift、if、case、break、continue、函数、select