shell高级

  1. #!/bin/bash
  2. set -o nounset
  3. set -o errexit

判断系统

  1. # 判断内核版本,2开头的内核是低版本
  2. if [[ $(uname -r | cut -d "." -f 1) -eq 2 ]]; then
  3. echo "The system is running an old kernel, which is incompatible with this installer."
  4. exit
  5. fi
  6. # 检测系统版本,获取安装命令
  7. if [[ -f /etc/redhat-release ]]; then
  8. release="centos"
  9. systemPackage="yum"
  10. systempwd="/usr/lib/systemd/system/"
  11. elif cat /etc/issue | grep -Eqi "debian"; then
  12. release="debian"
  13. systemPackage="apt-get"
  14. systempwd="/lib/systemd/system/"
  15. elif cat /etc/issue | grep -Eqi "ubuntu"; then
  16. release="ubuntu"
  17. systemPackage="apt-get"
  18. systempwd="/lib/systemd/system/"
  19. elif cat /etc/issue | grep -Eqi "centos|red hat|redhat"; then
  20. release="centos"
  21. systemPackage="yum"
  22. systempwd="/usr/lib/systemd/system/"
  23. elif cat /proc/version | grep -Eqi "debian"; then
  24. release="debian"
  25. systemPackage="apt-get"
  26. systempwd="/lib/systemd/system/"
  27. elif cat /proc/version | grep -Eqi "ubuntu"; then
  28. release="ubuntu"
  29. systemPackage="apt-get"
  30. systempwd="/lib/systemd/system/"
  31. elif cat /proc/version | grep -Eqi "centos|red hat|redhat"; then
  32. release="centos"
  33. systemPackage="yum"
  34. systempwd="/usr/lib/systemd/system/"
  35. fi
  36. # 判断系统类型
  37. if grep -qs "ubuntu" /etc/os-release; then
  38. os="ubuntu"
  39. os_version=$(grep 'VERSION_ID' /etc/os-release | cut -d '"' -f 2 | tr -d '.')
  40. group_name="nogroup"
  41. elif [[ -e /etc/debian_version ]]; then
  42. os="debian"
  43. os_version=$(grep -oE '[0-9]+' /etc/debian_version | head -1)
  44. group_name="nogroup"
  45. elif [[ -e /etc/centos-release ]]; then
  46. os="centos"
  47. os_version=$(grep -oE '[0-9]+' /etc/centos-release | head -1)
  48. group_name="nobody"
  49. elif [[ -e /etc/fedora-release ]]; then
  50. os="fedora"
  51. os_version=$(grep -oE '[0-9]+' /etc/fedora-release | head -1)
  52. group_name="nobody"
  53. else
  54. echo "This installer seems to be running on an unsupported distribution.
  55. Supported distributions are Ubuntu, Debian, CentOS, and Fedora."
  56. exit
  57. fi
  58. # 判断系统版本版本
  59. if [[ "$os" == "ubuntu" && "$os_version" -lt 1804 ]]; then
  60. echo "Ubuntu 18.04 or higher is required to use this installer.
  61. This version of Ubuntu is too old and unsupported."
  62. exit
  63. fi
  64. if [[ "$os" == "debian" && "$os_version" -lt 9 ]]; then
  65. echo "Debian 9 or higher is required to use this installer.
  66. This version of Debian is too old and unsupported."
  67. exit
  68. fi
  69. if [[ "$os" == "centos" && "$os_version" -lt 7 ]]; then
  70. echo "CentOS 7 or higher is required to use this installer.
  71. This version of CentOS is too old and unsupported."
  72. exit
  73. fi

文件写入

  1. #高级的 写入文件语法
  2. new_client () {
  3. # Generates the custom client.ovpn
  4. {
  5. cat /etc/openvpn/server/client-common.txt
  6. echo "<ca>"
  7. cat /etc/openvpn/server/easy-rsa/pki/ca.crt
  8. echo "</ca>"
  9. echo "<cert>"
  10. sed -ne '/BEGIN CERTIFICATE/,$ p' /etc/openvpn/server/easy-rsa/pki/issued/"$client".crt
  11. echo "</cert>"
  12. echo "<key>"
  13. cat /etc/openvpn/server/easy-rsa/pki/private/"$client".key
  14. echo "</key>"
  15. echo "<tls-crypt>"
  16. sed -ne '/BEGIN OpenVPN Static key/,$ p' /etc/openvpn/server/tc.key
  17. echo "</tls-crypt>"
  18. } > ~/"$client".ovpn
  19. }
  20. # 普通的写入
  21. cat > /root/1.txt <<EOF
  22. echo test
  23. EOF

root启动

  1. function Msg(){
  2. if [ $? -eq 0 ];then
  3. action "$1" /bin/true
  4. else
  5. action "$1" /bin/false
  6. fi
  7. }
  8. if ! [ $UID -eq 0 ]
  9. then
  10. Msg "请使用超级用户执行本脚本" ; exit 0
  11. fi

ctrl+c终止程序

  1. #!/bin/bash
  2. # 按任意键执行程序,ctrl+c则终止程序
  3. get_char(){
  4. SAVEDSTTY=`stty -g`
  5. stty -echo
  6. stty cbreak
  7. dd if=/dev/tty bs=1 count=1 2> /dev/null
  8. stty -raw
  9. stty echo
  10. stty $SAVEDSTTY
  11. }
  12. echo "Press any key to start...or Press Ctrl+c to cancel"
  13. char=`get_char`
  14. echo "我执行成功了"

字体颜色

  1. yellow(){
  2. echo -e "\033[33m\033[01m$1\033[0m"
  3. }
  4. green(){
  5. echo -e "\033[32m\033[01m$1\033[0m"
  6. }
  7. red(){
  8. echo -e "\033[31m\033[01m$1\033[0m"
  9. }
  10. red "======================================================================="
  11. red "检测到SELinux为宽容状态,为防止申请证书失败,请先重启VPS后,再执行本脚本"
  12. red "======================================================================="
  13. green "======================="
  14. yellow "请输入绑定到本VPS的域名"
  15. green "======================="

脚本生成脚本文件

  1. # 解决相对路径问题
  2. cd `dirname $0`
  3. # 引入外部文件
  4. source ../common/util.sh
  5. # 备份
  6. cp ${SSH_FILE_PATH}{,.bak}

选择功能

随机数

整数随机数

  1. echo $[$RANDOM%19+9]
  2. echo $[$RANDOM%4+3]
  3. echo $[$RANDOM%20+500] #生成500-520的整数

输出帮助

  1. :/usr/bin:/root/bin
  2. usage () {
  3. echo "Usage: $0 [OPTION]"
  4. echo "-d, --dest=name Destination directory. Default is /tmp"
  5. echo "-h, --help Display this help and exit."
  6. echo "-p, --password[=name] Password to use when connecting to server. If password is"
  7. echo " not given it's asked from the tty."
  8. echo "-t Temporary mount point for the snapshot. Default is /mnt."
  9. echo "-u, --user=name User for login if not current user"
  10. exit 1
  11. }
  12. case "$1" in
  13. * )
  14. usage
  15. ;;
  16. esac

cat /etc/passwd |cut -f 1 -d : #只显示用户名,cut分割
cat /etc/group |cut -f 1 -d : #只显示组名


瓦雀