shell高级
#!/bin/bashset -o nounsetset -o errexit
判断系统
# 判断内核版本,2开头的内核是低版本if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; thenecho "The system is running an old kernel, which is incompatible with this installer."exitfi# 检测系统版本,获取安装命令if [[ -f /etc/redhat-release ]]; thenrelease="centos"systemPackage="yum"systempwd="/usr/lib/systemd/system/"elif cat /etc/issue | grep -Eqi "debian"; thenrelease="debian"systemPackage="apt-get"systempwd="/lib/systemd/system/"elif cat /etc/issue | grep -Eqi "ubuntu"; thenrelease="ubuntu"systemPackage="apt-get"systempwd="/lib/systemd/system/"elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; thenrelease="centos"systemPackage="yum"systempwd="/usr/lib/systemd/system/"elif cat /proc/version | grep -Eqi "debian"; thenrelease="debian"systemPackage="apt-get"systempwd="/lib/systemd/system/"elif cat /proc/version | grep -Eqi "ubuntu"; thenrelease="ubuntu"systemPackage="apt-get"systempwd="/lib/systemd/system/"elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; thenrelease="centos"systemPackage="yum"systempwd="/usr/lib/systemd/system/"fi# 判断系统类型if grep -qs "ubuntu" /etc/os-release; thenos="ubuntu"os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')group_name="nogroup"elif [[ -e /etc/debian_version ]]; thenos="debian"os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)group_name="nogroup"elif [[ -e /etc/centos-release ]]; thenos="centos"os_version=$(grep -oE '[0-9]+' /etc/centos-release | head -1)group_name="nobody"elif [[ -e /etc/fedora-release ]]; thenos="fedora"os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)group_name="nobody"elseecho "This installer seems to be running on an unsupported distribution.Supported distributions are Ubuntu, Debian, CentOS, and Fedora."exitfi# 判断系统版本版本if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; thenecho "Ubuntu 18.04 or higher is required to use this installer.This version of Ubuntu is too old and unsupported."exitfiif [[ "$os" == "debian" && "$os_version" -lt 9 ]]; thenecho "Debian 9 or higher is required to use this installer.This version of Debian is too old and unsupported."exitfiif [[ "$os" == "centos" && "$os_version" -lt 7 ]]; thenecho "CentOS 7 or higher is required to use this installer.This version of CentOS is too old and unsupported."exitfi
文件写入
#高级的 写入文件语法new_client () {# Generates the custom client.ovpn{cat /etc/openvpn/server/client-common.txtecho "<ca>"cat /etc/openvpn/server/easy-rsa/pki/ca.crtecho "</ca>"echo "<cert>"sed -ne '/BEGIN CERTIFICATE/,$ p' /etc/openvpn/server/easy-rsa/pki/issued/"$client".crtecho "</cert>"echo "<key>"cat /etc/openvpn/server/easy-rsa/pki/private/"$client".keyecho "</key>"echo "<tls-crypt>"sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.keyecho "</tls-crypt>"} > ~/"$client".ovpn}# 普通的写入cat > /root/1.txt <<EOFecho testEOF
root启动
function Msg(){if [ $? -eq 0 ];thenaction "$1" /bin/trueelseaction "$1" /bin/falsefi}if ! [ $UID -eq 0 ]thenMsg "请使用超级用户执行本脚本" ; exit 0fi
ctrl+c终止程序
#!/bin/bash# 按任意键执行程序,ctrl+c则终止程序get_char(){SAVEDSTTY=`stty -g`stty -echostty cbreakdd if=/dev/tty bs=1 count=1 2> /dev/nullstty -rawstty echostty $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/binusage () {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 : #只显示组名
