学以致用二十二-----写一个基本环境设置的脚本

每次安装完系统后,需要配置一些基础的环境。因此试着写了一个脚本,以后再装了新的操作系统后,可以用这个脚本来实现配置。

功能:1、更改主机名

           2、关闭禁用防火墙

           3、关闭sellinux

           4、增加时间戳

           5、关闭编辑或者输出错误的警告音

代码如下:

  1 #!/usr/bin/bash
  2 #2018-10-18
  3 #lion
  4 #setup base environment include hosname selinux firewalld 
  5 selinux_file=/etc/selinux/config
  6 Color_text()
  7 {
  8    echo -e "e[1;$2m$1e[0m"
  9 }
 10 Echo_red()
 11 {
 12    echo $(Color_text "$1" "31") 
 13 }
 14 Echo_green()
 15 {
 16    echo $(Color_text "$1" "32") 
 17 }
 18 Echo_yellow()
 19 {
 20    echo $(Color_text "$1" "33") 
 21 }
 22 Echo_blue()
 23 {
 24    echo $(Color_text "$1" "34") 
 25 }
 26 update_hostname()
 27 {
 28    read -p "input new hostname:" host_name
 29    echo $host_name > /etc/hostname
 30    #echo -e "Hostname updated successful
"  
 31    Echo_green "Hostname updated successful
"
 32 }
 33 
 34 close_firewall()
 35 {
 36    systemctl stop firewalld.service
 37    systemctl disable firewalld.service &> /dev/null
 38    systemctl status firewalld.service &> /dev/null
 39    [ $? -ne 0 ] &&  Echo_green "firewall closed successful
"
 40 }
 41 disable_selinux()
 42 {
 43    status=$(cat ${selinux_file} | grep '^SELINUX' | awk -F'=' 'NR==1{print $2}')
 44    if [ "${status}" != "disabled" ] ; then
 45         sed -i "s/SELINUX=${status}/SELINUX=disabled/g" /etc/selinux/config
 46    else
 47        Echo_yellow "selinux has diabled
"
 48    fi
 49 }
 50 add_timestamp()
 51 {
 52     histtime=$(grep 'HISTTIMEFORMAT' /etc/profile)
 53     [ -z "${histtime}" ]  && echo 'export HISTTIMEFORMAT="%F %T `whoami` "' >> /etc/profile && source /etc/profile  || Echo_green "timesstamp has added
"
 54 }
 55 #vim_check()
 56 #{
 57 #    vim_version=$(vi --vesion | grep 'IMProved' | awk '{print $5}')
 58 #    if [ ${vim_version} < 8 ];then
 59 #        yum remove vi  
 60 #        . vim_install
 61 #    else
 62 #        #echo -e "vim version is already 8,nothing to do
"
 63 #        Echo_yellow "vim version is already 8,nothing to do
"
 64 #    fi
 65 #}
 66 
 67 off_warning_voice()
 68 {
 69     echo "setterm -blength 0" >> /etc/bashrc
 70     sed -i '/bell-style/s/^#//g' /etc/inputrc
 71 }
 72 menu()
 73 {
 74     clear
 75     Echo_blue "		Check base envionment"
 76     echo -e "	1.Update hostname"
 77     echo -e "	2.Close firewalld"
 78     echo -e "	3.Disable selinux"
 79     echo -e "	4.Add timetamp"
 80     echo -e "	5 warining voice"
 81     echo -e "	0.Exit menu"
 82     read -p "Enter an option:" choice
 83    # echo -en "Enter an option:"
 84    # read -n 1 choice
 85 }
 86 
 87 while [ 1 ]
 88 do
 89     menu
 90     case $choice in
 91         0)
 92             break
 93             ;;
 94         1)
 95             update_hostname
 96             ;;
 97         2)
 98             close_firewall
 99             ;;
100         3)
101             disable_selinux
102             ;;
103         4)
104             add_timestamp
105             ;;
106         5)
107             off_warning_voice
108             ;;
109         *)
110             clear
111             Echo_red "invalid option
"
112      esac
113      #echo -en "	Hit any key to continue:"
114      #read -n 1 line
115      read -p "Hit any key to continue:"
116 done

写此次代码的目的:

1、 加强对函数调用的学习

2、while 循环 , 

      while [ 1 ]    表示无限循环,注意[ 1 ]  中括号里两边是有空格的,shell脚本对空格控制挺严格

3、学会菜单的使用及case的用法,已esac结尾

4、read的用法

      read -n 1 line 表示只接受一个字符串输入,如果超出则马上输出,(由于输出的效果不佳,已注释)

5、字体颜色的函数调用

 Color_text()
  {
     echo -e "e[1;$2m$1e[0m"
  }

6、加强 if语句的练习

7 、 [ -z  条件 ]  是否为空

8、 status=$(cat ${selinux_file} | grep '^SELINUX' | awk -F'=' 'NR==1{print $2}')

       复习 awk的用法  -F'='  以=为分隔符,默认是空格为分隔符

        NR==1{print $2}

        NR,表示awk开始执行程序后所读取的数据行数.此处表示打印出第一行,第二列

9、  复习sed的用法

       sed -i "s/SELINUX=${status}/SELINUX=disabled/g" /etc/selinux/config

       sed -i ‘s/old/new/g’    替换

       sed -i '/bell-style/s/^#//g' /etc/inputrc  先匹配bell-style这行,然后把行首的#替换为空,等价于删除行首的#

         /s/^#//g

10、 systemctl disable firewalld.service &> /dev/null

        &> 输入输出不打印出来,重定向到/dev/null里

11、echo -e   处理特殊字符,使其生效

       echo -n  不换行输出

       echo -ne 两者结合

以前对shell脚本不理解,或者是看到别人写的脚本,只能看懂一部分,因此对shell脚本总有点抵触或者发忪的情绪在里面,通过这段时间对shell脚本的加强练习,以及自己去写脚本,不断尝试和修改,并借鉴其他脚本好的用法,终于对shell脚本有了全新的认识

并且对shell脚本的兴趣也越发浓厚。

多看,多练习,多总结,生活源于一点一滴的积累

       

    

  

原文地址:https://www.cnblogs.com/liongong/p/9819953.html