k
kate
V1
2022/05/24阅读:61主题:默认主题
shell基本语法
1 分支结构
#!/bin/bash
# author: kate
# go: talk is cheap,show me the code.
if test ${1} == ${2}
then
echo "参数1 等于 参数2"
elif test ${1} -gt ${2}
then
echo "参数1 大于 参数2"
elif test ${1} -lt ${2}
then
echo "参数1 小于 参数2"
else
echo "没有符合的条件!"
fi
2 循环结构
#!/bin/sh
# author: kate
# go: talk is cheap,show me the code.
for item in 1 2 3 4 5
do
echo "the value is ${item}"
done
#!/bin/sh
# author: kate
# go: talk is cheap,show me the code.
int=2
while(( $int<=5 ))
do
echo ${int}
let "int++"
done
echo '按下<CTRL-D>退出'
echo -n '输入你最喜欢的网站名:'
while read FILM
do
echo "是的! ${FILM} 是一个好网站"
done
无限循环
while :
do
command
done
while true
do
command
done
for(( ; ; ))
#!/bin/bash
# author: kate
# go: talk is cheap,show me the code.
a=0
until [ ! $a -lt 10]
do
echo $a
a=`expr $a+1`
done
#!/bin/sh
echo '输入1到4之间的整数:'
echo '你输入的数字为:'
read aNum
case ${aNum} in
1) echo '你选择了1'
;;
2) echo '你选择了2'
;;
3) echo '你选择了3'
;;
4) echo '你选择了4'
;;
*) echo '你没有输入1到4之间的整数!'
;;
ease
3 函数
#!/bin/sh
# author: kate
# go: talk is cheap,show me the code.
funWithParam() {
echo "第一个参数为 ${1} !"
echo "第一个参数为 ${2} !"
echo "第一个参数为 ${10} !"
echo "第一个参数为 ${11} !"
echo "参数总数有 $# !"
echo "作为一个字符串输出所有参数 $* !"
echo "所有参数 $@ !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73
作者介绍
k
kate
V1