# 30s 等待时间
read -t 30 -p "Upgrade operation will interrupt the network service. Stop or migrate virtual machines first. The system will upgrade the vSwitch. Continue? [Y/N]:"
switch-case语法
read reply
case $reply in
Y | y) echo "yes" ;;
N | n) echo "no" ;;
* ) echo "Input error" ;;
esac
读取键盘输入
read type
if [ "$type" = 'yes' ]
then
echo "xxx"
elif [ "$type" != 'no' ]
then
echo "xxx"
fi
循环语句
# a. for
for item in xxx; do
echo "$item"
done
# b. while
while [ condition ]; do
echo ""
done
# c. until
until condition
do
echo "xxx"
done
// 1. 声明数组:
arr=(1 2 3 4)
arr=([0]=1 [1]=2 [2]=3)
// 2. 遍历数组:
for item in ${arr[*]}; do
echo $item;
done
//3. 追加数组:
a. arr=(${arr[*]} 5)
b. arr+=(6)
// 4. 删除数组元素
unset arr[0]
//5. 数组长度:
echo ${#array[*]} 或者 echo ${#arr[@]}
//6. 数组切片
echo ${arr[@]:0:2}
shell脚本执行的参数
命令行参数 set选项 说明
sh -n <script> set -o noexec / set -n 只检查语法错误,不执行命令
sh -v <script> set -o verbose / set -v 在执行命令之前回显他们
sh -x <script> set -o xtrace / set -x 在处理完命令后回显他们
sh -u <script> set -u nounset / set -u 如果使用了未定义的变量,就给出出错信息