shell脚本自动化编程lili
for循环
1.语法:
for (( 初始值;循环控制条件;变量变化 ))
do
程序
done
if流程控制
语法:
if [ 条件判断式 ]
then
程序
fi
条件判断式之间必须有空格。
if [ 条件判断式 ]
then
程序
elif [ 条件判断式 ]
then
程序
fi
elif也是如果的意思
case语句
基本语法
case $变量名 in
“值1”)
程序
;;
“值2”)
程序
;;
*)
程序
;;
esac
for in
for 变量 in 值1 值2 值3
do
程序
done
while 语法
while [ 条件判断式 ]
do
程序
done
例题
从1加到100的值
#!/bin/bash
i=1
s=0
while [ $i -le 100 ]
do
s=$[$i+$s]
i=$[$i+1]
done
echo $s
read读取控制台输入
基本语法:
read (选项)(参数)
选项
-p:指定读取值时提示
-t:指定读取值时等待时间(秒)
示例:
#!/bin/bash
read -t 6 -p "input your name " NAME
echo $NAME
~
函数
系统函数
basename基本语法
basename [string/pathname] [suffix]
功能描述:(basename命令,会删除所有的前缀包括最后一个‘/’,然后将字符串显示出来。)
截取最后一个目录名称
例
asename /home/liuxiaoyao/Dessktop/demo/shell_5 hell_5
s
dirname基本用法
dirname /home/liuxiaoyao/Dessktop/demo/shell_5
shell_5
自定义函数
基本语法
[ function ] funname [()]
{
Action;
[return int;] $?
}
funname
例题:
计算两个输入参数的和
#!/bin/bash
function sum ()
{
s=0
s=$(($1+$2))
echo "${s}"
}
read -p "please input the number1:" n1;
read -p "please input the number2:" n2;
sum $n1 $n2;