shell编程学习

#!/bin/bash
:<<EOF
多行注释
EOF

hello="Hello"
echo "===================================${hello} World! \"hello\"长度="${#hello}===================================

# 数组测试
array_test=(1 2 "3")
echo '打印一个元素:'${array_test[1]},'打印所有元素(@或*):'${array_test[@]}

# 参数测试
if [ $# -lt 2 ];then
    echo "参数个数少于2!!!"
    exit;
fi
echo "参数个数\$#=$#-----\$0=$0-----\$1=$1-----\$2=$2"
for i in "$@"; do printf '$'$i'='$i','; done
for i in "$*"; do echo -----'作为一个字符串输出所有参数:$*='$i; done

if [ -d '/home/oozie/zhaiyc' ]; then echo 'is dir'; else echo 'not dir'; fi

read -p "请5秒内输入6位密码:" -n 6 -t 5 -s password
echo -e "\nText is $password"

i=1;j=1
if [ $i -gt $j ];then
    echo '>'
elif [ $i -eq $j ];then
    echo '='
else
    echo '<'
fi

#for i in 1 2
for((i=1;i<=5;i++));do
    echo "这是第 $i 次调用";
done

i=1
while(( $i<=2 ))
do
    echo 'While测试输出:'$i
    #let "i++"
    i=`expr $i + 1`  # 看清不是'(单引号)
done

aNum=3
case $aNum in
    1|2|3)  echo '你选择了 1-3'
    ;;
    4)  echo '你选择了 4'
    ;;
    *)  echo '你没有输入 1 到 4 之间的数字'
    ;;
esac

funWithParam(){
    echo "第一个参数为 $1 !"
    echo "第二个参数为 $2 !"
    echo "第十个参数为 $10 !"
    echo "第十个参数为 ${10} !"  # 注意,$10 不能获取第十个参数,获取第十个参数需要${10}。当n>=10时,需要使用${n}来获取参数。
    echo "第十一个参数为 ${11} !"
    echo "参数总数有 $# 个!"
    echo "作为一个字符串输出所有参数 $* !"
}
funWithParam 1 2 3 4 5 6 7 8 9 34 73

猜你喜欢

转载自blog.csdn.net/zyc_996/article/details/83149653