shell编程

表达式

A=$((experssion))
A=$[experssion] #中括号前后都要有空格
A=`expr experssion`;#这里运算符需要转义,运算符两边要空格

条件判断

= #字符串比较
-lt	# <
-le # <=
-eq # ==
-gt # >
-ge # >=
-ne # !=

循环

for 变量 in 值1 值2 值3...
do
	代码
done # 结束

for(( 初始值; 控制条件; 变量变换 ))
do
	代码
done

while [ 条件判断 ]
do
	代码
done

read

read(选项)(参数)
-p # 指定读取值时的提示符
-t # 指定等待时间,如果到时未读入,则不再等待

示例:

echo "你好~"
if [ $1 -eq 5 ]
then echo "eq"
elif [ $1 -gt 5 ]
then echo "gt"
else echo "le"
fi
#$* take parameter of input into entirety
for i in "$*"
do
        echo "num is $i"
done
#$@ is used separately

for j in "$@"
do
        echo "num is $j"
done

SUM=0
for(( i=1; i<=100; i++ ))
do
        SUM=$(($SUM+$i))
done
echo $SUM
read -p "input something:" NUM1
echo "your input is $NUM1"

函数

[ funtion ] funname[()]
{
	Action;
	[return int;]
}

你可能感兴趣的:(linux,shell)