shell 以 【#!】开头 加后面的shell解析器 常用 #!/bin/bash
运算符:+、-、*、/、% 分别对应 加、减、乘、除、取余,举例如下:
s=$[1 + 2]
echo $s // 输出3
$n含义:
$0:代表脚步本身
$1~$9:代表9个参数
如果参数大于10个则需要使用如下格式 ${10}
$*含义:代表所有的参数
$#含义:代表最后一个参数
$@含义:代表所有的参数
If的语法:
if空格[空格 条件表达式 空格]
then
执行命令
fi
或
if空格[空格 条件表达式 空格];then
执行命令
fi
例子:
if [ $1 -eq 1 ]
then
echo "yes this is one"
fi
或
if [ $1 -eq 2 ];then
echo "yes this is two"
fi
case的语法:
case空格 参数 in
1)
程序逻辑(参数是1的情况执行的逻辑)
;;
2)
程序逻辑(参数是2的情况执行的逻辑)
;;
*)
程序逻辑(其他情况执行的逻辑)
;;
esac(退出标示,是case的倒转)
例子:
case $1 in
1)
echo "this is one"
;;
2)
echo "this is two"
;;
*)
echo "unknow value"
;;
esac
for的语法:
for((条件))
do
执行逻辑
done
或者
for 变量 in 参数1 参数2 ……
do
执行逻辑
done
例子:
s=0
for((i=1;i<=100;i++))
do
s=$[$s+$i]
done
echo "the result is = " $s
或
for i in $*
do
echo "the param has $i"
done
echo "************************"
for j in $@
do
echo "the param is $j"
done
while的语法:
while空格[空格 条件 空格]
do
执行逻辑
done
例子:
s=0
i=1
while [ $i -le 100 ]
do
s=$[$s + $i]
i=$[$i + 1]
done
echo "the result is: $s"
read的语法:
read(选项)(参数)
选项:
-p 指定读取值时的提示符
-t 指定读取值时的等待时间(秒)
参数:
指定读取值的变量名
举例:
#!/bin/bash
read -t 7 -p "Please enter your name in 7 seconds" Name
echo "welcome $Name"
网友评论