如何写一个shell安装脚本

安装脚本算是shell最常见的一个用途之一了
下面是一个demo

#!/bin/bash

# date 2024/8/21

hostpath=/root/interview/install

#检测目录是否存在,不存在创建它
[ ! -d $hostpath ] && mkdir $hostpath -p 

cat <<END

	1.[ install lamp ]
	2.[ install lnmp ]
 	3.[ exit ]
	pls input the number you want:

END

read num 

# 根据输入的数字做选择

expr $num + 1 &> /dev/null

[ $? -ne 0 ] && { 
	echo number you input must be {1|2|3} 

}

# 根据num做逻辑判断

[ $num -eq 1 ] && {
	echo "正在安装lamp。。。"
	sleep 2
 
	[ -x $hostpath/lamp.sh ] || {
        	echo "the file does not exist or cant be exec "
        	exit 1
	}

} 

[ $num -eq 2 ] && {
	echo "正在安装lnmp。。。"
	sleep 2 
	
	[ -x $hostpath/lnmp.sh ] || {
       		echo "the file does not exist or cant be exec "
        	exit 1
	}

}

[ $num -eq 3 ] && {
	exit 0
}


亮点是格式比较正规
参考来源:
https://www.bilibili.com/video/BV1d34y1j763?p=40&vd_source=6dc445c1be9dad41cefb811dae015c1b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值