# [ ] 中间的变量左右保留各保留一个空格
if [ -d $0 ]; then
echo "this is dir"
elif [ -f $0 ]; then
echo "this is file"
else
echo "Nothing"
fi
# seq : sequence: shell的序列,全闭区间
for i in `seq 1 10`
do
echo "current num is $i"
done
等同于:
for ((i=1; i<11; i++)); do echo "current num is $i"; done
while ((condition))
do
echo "run shell"
done
foo() {
echo "Hello Shell"
for f in `ls ../`
do
echo $f
done
}
# 调用函数不需要()
foo
注意:
定义函数时,变量名前面加 function 和 不加 function 都可
cat << EOF
请输入要执行的操作编号:【1-4】
==========================
【1】系统更新
【2】安装软件
【3】安装Redis
【4】安装Nginx
=========================
EOF
# 获取要执行的操作编号
if [[ -n $1 ]]; then
input=$1
echo "执行操作: $1"
else
read -p "请选择: " input
fi
case $input in
1) system_update;;
2) install_software;;
3) install_redis;;
4) install_nginx;;
*) exit;;
esac