#wei@123!/usr/bin/bash
#https://blog.csdn.net/binglumeng/article/details/53221976
#https://blog.csdn.net/qq_18297675/article/details/52693464
#https://blog.csdn.net/freexploit/article/details/626660
#https://blog.csdn.net/xmyzlz/article/details/8593228
echo "hello world"
#常量
sss="hello you"
echo $sss

#printf打印
printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg  
printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234 
printf "%-10s %-8s %-4.2f\n" 杨过 男 48.6543 
printf "%-10s %-8s %-4.2f\n" 郭芙 女 47.9876 


#分片
string="12321312321fdsgdgwrewq123safdsadfa"
echo "aaa ${string:1:5}"

#while
echo ""
#变量
let s=123
m=120
while [ $s -gt 120 ]
do
    echo "====$s"
    s=$(($s -1))
done


#if
if [ $s -le 150 ]; then echo "变量s现在是$s, 比150小!"
fi


if [ -f "hh.txt" ]
then
   echo "hh.txt存在!"
elif [ $s -lt 150 ]
then 
   touch hh123.txt
   echo "我创建了一个新的hh123.txt"
   #echo和>>,>提供了重定向的功能
   echo "echo和>>,>提供了重定向的功能"
   echo $s >> hh123.txt
fi


#定义数组
shuzu=(1222 123 32231 111 'u' 666)
echo "shuzu的第二个元素是:" ${shuzu[1]}
echo "shuzu的元素:" ${shuzu[*]}
echo "shuzu的元素:" ${shuzu[@]}
echo "shuzu的长度是:" ${#shuzu[*]}


#for循环
#数***算的形式
for ((i=1;i<10;i++));
do
    echo $(expr $i \* 3 + 1)
done

#浮点运算
for ((i=1;i<10;i++));
do
    #echo $($i * 3.2 + 1 | bc)
    #echo $($i * 3.2 + 1) | bc
    #注意运算符号
    echo $i \* 3.2 + 1 | bc
done

#for in 形式
for time in morning noon afternoon evening
do
    echo "This time is $time!"
done

a=100
b=100.998
c=671
#浮点运算的方式,在最外层$()是把其当做一个整体,这样会有问题,浮点运算的话使用|bc
#然后将整个运算的结果,对其求取浮点运算结果,就如同下面的这种方式
echo $a + $b|bc
#如下的运算有错误
#echo $(($a +$b))|bc
#整数运算
echo $((a + c))

echo "hello world!"
echo "10 / 3 * 2" | bc
echo "scale =8; 10 / 3 * 2" | bc

#until
echo "until的使用"
until [ $a -gt 105 ]
do
    echo $a
    a=$(($a + 1))
    if [ $a -eq 103 ]
    then
        echo "$a等于103,跳出本次循环"
        a=$(($a + 1))
        continue
    fi
done


#使用for循环,来遍历文件夹
for file in /home/*
do
    echo $file
done 

#函数
func(){
    echo "调用 第一个函数func(), 输出first function!"
}
func

#函数参数的使用
funcWithParamter(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第五个参数为 $5 !"
    echo "作为一个字符串输出所有参数 $* !"
}
funcWithParamter 1 2 3 88 97 99 123 -9 111

#函数的返回值
funcWithReturn(){
    echo "输入的两个数字进行相加运算"
    echo "输入第一个数字"
    read num1
    echo "输入第二个数字"
    read num2
    printf "第一个数字是%s ,第二个数字是%s \n" $num1 $num2
    return $(($num1 + $num2))
}
funcWithReturn 
echo result :$?

funcWithParam2(){
   #echo "first param $1"
   #echo "first param $2"
   #echo "first param $3"
   #多个参数, 通过回显来赋值
   echo $(($1 + $2 + $3))
}
res3=$(funcWithParam2 1 2 9)
echo $res3
if [[ $res3 -gt 10 ]]
then
    echo "hello world"
fi

#直接取到funcWithReturn的结果
if $funcWithReturn
then
    echo "调用了funcWithReturn函数,结果大于2"
fi