shell高级
#!/bin/bash
set -o nounset
set -o errexit
判断系统
# 判断内核版本,2开头的内核是低版本
if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
echo "The system is running an old kernel, which is incompatible with this installer."
exit
fi
# 检测系统版本,获取安装命令
if [[ -f /etc/redhat-release ]]; then
release="centos"
systemPackage="yum"
systempwd="/usr/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "debian"; then
release="debian"
systemPackage="apt-get"
systempwd="/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "ubuntu"; then
release="ubuntu"
systemPackage="apt-get"
systempwd="/lib/systemd/system/"
elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
release="centos"
systemPackage="yum"
systempwd="/usr/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "debian"; then
release="debian"
systemPackage="apt-get"
systempwd="/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "ubuntu"; then
release="ubuntu"
systemPackage="apt-get"
systempwd="/lib/systemd/system/"
elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
release="centos"
systemPackage="yum"
systempwd="/usr/lib/systemd/system/"
fi
# 判断系统类型
if grep -qs "ubuntu" /etc/os-release; then
os="ubuntu"
os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
group_name="nogroup"
elif [[ -e /etc/debian_version ]]; then
os="debian"
os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
group_name="nogroup"
elif [[ -e /etc/centos-release ]]; then
os="centos"
os_version=$(grep -oE '[0-9]+' /etc/centos-release | head -1)
group_name="nobody"
elif [[ -e /etc/fedora-release ]]; then
os="fedora"
os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
group_name="nobody"
else
echo "This installer seems to be running on an unsupported distribution.
Supported distributions are Ubuntu, Debian, CentOS, and Fedora."
exit
fi
# 判断系统版本版本
if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then
echo "Ubuntu 18.04 or higher is required to use this installer.
This version of Ubuntu is too old and unsupported."
exit
fi
if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then
echo "Debian 9 or higher is required to use this installer.
This version of Debian is too old and unsupported."
exit
fi
if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then
echo "CentOS 7 or higher is required to use this installer.
This version of CentOS is too old and unsupported."
exit
fi
文件写入
#高级的 写入文件语法
new_client () {
# Generates the custom client.ovpn
{
cat /etc/openvpn/server/client-common.txt
echo "<ca>"
cat /etc/openvpn/server/easy-rsa/pki/ca.crt
echo "</ca>"
echo "<cert>"
sed -ne '/BEGIN CERTIFICATE/,$ p' /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt
echo "</cert>"
echo "<key>"
cat /etc/openvpn/server/easy-rsa/pki/private/"$client".key
echo "</key>"
echo "<tls-crypt>"
sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.key
echo "</tls-crypt>"
} > ~/"$client".ovpn
}
# 普通的写入
cat > /root/1.txt <<EOF
echo test
EOF
root启动
function Msg(){
if [ $? -eq 0 ];then
action "$1" /bin/true
else
action "$1" /bin/false
fi
}
if ! [ $UID -eq 0 ]
then
Msg "请使用超级用户执行本脚本" ; exit 0
fi
ctrl+c终止程序
#!/bin/bash
# 按任意键执行程序,ctrl+c则终止程序
get_char(){
SAVEDSTTY=`stty -g`
stty -echo
stty cbreak
dd if=/dev/tty bs=1 count=1 2> /dev/null
stty -raw
stty echo
stty $SAVEDSTTY
}
echo "Press any key to start...or Press Ctrl+c to cancel"
char=`get_char`
echo "我执行成功了"
字体颜色
yellow(){
echo -e "\033[33m\033[01m$1\033[0m"
}
green(){
echo -e "\033[32m\033[01m$1\033[0m"
}
red(){
echo -e "\033[31m\033[01m$1\033[0m"
}
red "======================================================================="
red "检测到SELinux为宽容状态,为防止申请证书失败,请先重启VPS后,再执行本脚本"
red "======================================================================="
green "======================="
yellow "请输入绑定到本VPS的域名"
green "======================="
脚本生成脚本文件
# 解决相对路径问题
cd `dirname $0`
# 引入外部文件
source ../common/util.sh
# 备份
cp ${SSH_FILE_PATH}{,.bak}
选择功能
随机数
整数随机数
echo $[$RANDOM%19+9]
echo $[$RANDOM%4+3]
echo $[$RANDOM%20+500] #生成500-520的整数
输出帮助
:/usr/bin:/root/bin
usage () {
echo "Usage: $0 [OPTION]"
echo "-d, --dest=name Destination directory. Default is /tmp"
echo "-h, --help Display this help and exit."
echo "-p, --password[=name] Password to use when connecting to server. If password is"
echo " not given it's asked from the tty."
echo "-t Temporary mount point for the snapshot. Default is /mnt."
echo "-u, --user=name User for login if not current user"
exit 1
}
case "$1" in
* )
usage
;;
esac
cat /etc/passwd |cut -f 1 -d : #只显示用户名,cut分割
cat /etc/group |cut -f 1 -d : #只显示组名