shell script empowerment
chmod 777 file.sh
1, for circulation print array
arr1=(20 21 23 24 25)
arr2=(a b c d e f g)
for i in ${arr1[*]};do
echo -e $i "\c"
done
echo
for i in ${arr2[@]};do
echo -e $i "\c"
done
echo
# -e:转义,"\c":不换行
# for循环遍历数组
# ; 为条件结束符
# do 相当于左花括号
# done 相当于又花括号
2、if、elif、else和if test
arr1=(20 21 23 24 25)
arr2=(a b c d e f g)
if ((${arr1[0]}==$[10]));then
echo "arr[0]=10"
elif test ${arr1[0]} -eq $[20];then
echo "arr[0]=20"
elif test ${arr1[0]} -eq 30;then
echo "arr[0]=30"
else
echo "arr[0]=-1"
fi
# then 相当于左花括号,然后没有右花括号
# fi为if的字母倒序,表示if语句执行结束
3, while the cycle and break
# while循环
n=20
while (($n>10));do
echo -e $n "\c"
((n--))
done
echo
# while true和break
while true;do
echo -e "$n" "\c"
((n--))
if ((n==0));then
echo "break"
break
fi
done
4, function
# 声明函数
sum(){
echo "This is a method!"
n=0
for i in 1 2 3;do
((n+=i))
done
return $n
}
# 执行函数sum
sum
# $? 表示函数返回值
echo $?