经常要把虚拟机上的系统搞崩,总是各种报错,有次yum安装gcc程序都报错,一旦折腾半天解决不了,只好选择最笨却最有效的方法——还原vrm虚拟机的快照!可是还原快照到干净系统,就导致之前做的喜欢的配置又都没了 还要去慢慢设置开机图案、别名以及各种环境变量比较麻烦,于是就费了点时间,写了下面这个脚本,每次都可以一键实现让新系统恢复自己当初个性化的各种设置。

  先传效果图:
在这里插入图片描述

#!/bin/bash
read -p "please input a name for the address:" USERDIR
#输入用户名,方便在/etc目录下存放生成的配置文件


[ -a /etc/$USERDIR ] && [ -d /etc/$USERDIR ] ||{ \rm -rf /etc/$USERDIR; mkdir /etc/$USERDIR; }
#mkdir /etc/$USERDIR
#生成配置文件目录,若已存在则直接使用此目录


cat > /etc/$USERDIR/motd << END

oooooooooooo              oooooooooo.               ooooooooooooo oooo                     oooooooooo.                         .  
8'   888   '8              '888'   'Y8b              8'   888   '8 '888                     '888'   'Y8b                      .o8  
     888       'ooooo.      888     888  .ooooo.          888       888 .oo.    .ooooo.      888     888  .ooooo.   .oooo.o .o888oo
     888      d88' '88b     888oooo888' d88' '88b         888       888P"Y88b  d88' '88b     888oooo888' d88' '88b d88(  "8   888  
     888      888   888     888    '88b 888ooo888         888       888   888  888ooo888     888    '88b 888ooo888 '"Y88b.    888  
     888      888   888     888    .88P 888    .o         888       888   888  888    .o     888    .88P 888    .o o.  )88b   888 .
    o888o     'Y8bod8P'    o888bood8P'  'Y8bod8P'        o888o     o888o o888o 'Y8bod8P'    o888bood8P'  'Y8bod8P' 8"'888P'   "888"

END
#生成开机欢迎动画,可以随意修改



#创建/etc/$USERDIR/login.sh配置文件
[ -a /etc/$USERDIR ] && [ -d /etc/$USERDIR ]&& [ -w /etc/$USERDIR ]  ||{ \rm -rf /etc/$USERDIR; mkdir /etc/$USERDIR; }
#确认文件目录存在且可用,开始写配置文件。
#因为水平有限,不知道怎么把命令以及变量通过脚本生成到新脚本里
#只能采用最笨的方法——一行一行的写入,希望各位大佬可以指点一二。

echo '#!/bin/bash '>/etc/$USERDIR/login.sh
echo 'cat /etc/$USERDIR/motd'>>/etc/$USERDIR/login.sh
echo 'date=`date "+%F %T"`'>>/etc/$USERDIR/login.sh
echo 'head="System information as of: $date"'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo 'kernel=`uname -r`'>>/etc/$USERDIR/login.sh
echo 'hostname=`echo $HOSTNAME`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#Cpu load'>>/etc/$USERDIR/login.sh
echo 'load1=`cat /proc/loadavg | awk '"'"'{print $1}'"'"'`'>>/etc/$USERDIR/login.sh
echo 'load5=`cat /proc/loadavg | awk '"'"'{print $2}'"'"'`'>>/etc/$USERDIR/login.sh
echo 'load15=`cat /proc/loadavg | awk '"'"'{print $3}'"'"'`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#System uptime'>>/etc/$USERDIR/login.sh
echo 'uptime=`cat /proc/uptime | cut -f1 -d.`'>>/etc/$USERDIR/login.sh
echo 'upDays=$((uptime/60/60/24))'>>/etc/$USERDIR/login.sh
echo 'upHours=$((uptime/60/60%24))'>>/etc/$USERDIR/login.sh
echo 'upMins=$((uptime/60%60))'>>/etc/$USERDIR/login.sh
echo 'upSecs=$((uptime%60))'>>/etc/$USERDIR/login.sh
echo 'up_lastime=`date -d "$(awk -F. '"'"'{print $1}'"'"' /proc/uptime) second ago" +"%Y-%m-%d %H:%M:%S"`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#Memory Usage'>>/etc/$USERDIR/login.sh
echo 'mem_usage=`free -m | awk '"'"'/Mem:/{total=$2} /buffers\/cache/ {used=$3} END {printf("%3.2f%%",used/total*100)}'"'"'`'>>/etc/$USERDIR/login.sh
echo 'swap_usage=`free -m | awk '"'"'/Swap/{printf "%.2f%",$3/$2*100}'"'"'`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#Processes'>>/etc/$USERDIR/login.sh
echo 'processes=`ps aux | wc -l`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#User'>>/etc/$USERDIR/login.sh
echo 'users=`users | wc -w`'>>/etc/$USERDIR/login.sh
echo 'USER=`whoami`'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#System fs usage'>>/etc/$USERDIR/login.sh
echo 'Filesystem=$(df -h | awk '"'"'/^\/dev/{print $6}'"'"')'>>/etc/$USERDIR/login.sh
echo ''>>/etc/$USERDIR/login.sh
echo '#Interfaces'>>/etc/$USERDIR/login.sh
echo 'INTERFACES=$(ip -4 ad | grep '"'"'state '"'"' | awk -F":" '"'"'!/^[0-9]*: ?lo/ {print $2}'"'"'|grep e)'>>/etc/$USERDIR/login.sh
echo 'echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"'>>/etc/$USERDIR/login.sh
echo 'echo "$head"'>>/etc/$USERDIR/login.sh
echo 'echo "----------------------------------------------"'>>/etc/$USERDIR/login.sh
echo 'printf "Kernel Version:\t%s\n" $kernel'>>/etc/$USERDIR/login.sh
echo 'printf "HostName:\t%s\n" $hostname'>>/etc/$USERDIR/login.sh
echo 'printf "System Load:\t%s %s %s\n" $load1, $load5, $load15'>>/etc/$USERDIR/login.sh
echo 'printf "System Uptime:\t%s "days" %s "hours" %s "min" %s "sec"\n" $upDays $upHours $upMins $upSecs'>>/etc/$USERDIR/login.sh
echo 'printf "Memory Usage:\t%s\t\t\tSwap Usage:\t%s\n" $mem_usage $swap_usage'>>/etc/$USERDIR/login.sh
echo 'printf "Login Users:\t%s\t\t\tWhoami:\t\t%s\n" $users $USER'>>/etc/$USERDIR/login.sh
echo 'printf "Processes:\t%s\n" $processes'>>/etc/$USERDIR/login.sh
echo 'printf "\n"'>>/etc/$USERDIR/login.sh
echo 'printf "Filesystem\tUsage\n"'>>/etc/$USERDIR/login.sh
echo 'for f in $Filesystem'>>/etc/$USERDIR/login.sh
echo 'do'>>/etc/$USERDIR/login.sh
echo '    Usage=$(df -h | awk '"'"'{if($NF=="'"'"''"'"''"'"'$f'"'"''"'"''"'"'") print $5}'"'"')'>>/etc/$USERDIR/login.sh
echo '    echo -e "$f\t\t$Usage"'>>/etc/$USERDIR/login.sh
echo 'done'>>/etc/$USERDIR/login.sh
echo 'printf "\n"'>>/etc/$USERDIR/login.sh
echo 'printf "Interface\tMAC Address\t\tIP Address\n"'>>/etc/$USERDIR/login.sh
echo 'for i in $INTERFACES'>>/etc/$USERDIR/login.sh
echo 'do'>>/etc/$USERDIR/login.sh
echo '    MAC=$(ip ad show dev $i | grep "link/ether" | awk '"'"'{print $2}'"'"')'>>/etc/$USERDIR/login.sh
echo '    IP=$(ip ad show dev $i | awk '"'"'/inet / {print $2}'"'"')'>>/etc/$USERDIR/login.sh
echo '    printf $i"\t\t"$MAC"\t$IP\n"'>>/etc/$USERDIR/login.sh
echo 'done'>>/etc/$USERDIR/login.sh
echo 'echo "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"'>>/etc/$USERDIR/login.sh
echo 'echo'>>/etc/$USERDIR/login.sh

#生成脚本设置常用命令符以及别名及命令历史
cat > /etc/profile.d/env.sh << END
PS1="\e[1;36m[\u@\h \W]\\\\$\e[0m"
HISTTIMEFORMAT="%F %T "
HISTCONTROL=ignoreboth
alias rm='mkdir -p /date/delete/`date +%F`;\mv -t /date/delete/`date +%F` -f'
bash /etc/$USERDIR/login.sh
END

sed -i 's/^alias rm/#&/' ~/.bashrc
#取消用户定义的alias rm。

#生成VIM的格式脚本
cat > ~/.vimrc << END
#!/bin/bash
""新建.c,.h,.sh,.java文件,自动插入文件头 
autocmd BufNewFile *.cpp,*.[ch],*.sh,*.java exec ":call SetTitle()" 
""定义函数SetTitle,自动插入文件头 
func SetTitle()
""如果文件类型为.sh文件 
    if &filetype == 'sh'
    call setline(1,"\##########################################################################") 
    call append(line("."), "\# File Name:                  ".expand("%"))
    call append(line(".")+1,"\# Author:                       Name ")
    call append(line(".")+2,"\# QQ:                          xxxxxxxxx") 
    call append(line(".")+3,"\# mail:                       xxxxxxxxxx@xxx.com") 
    call append(line(".")+4,"\# Description:                The test script") 
    call append(line(".")+5,"\# Created Time:               ".strftime("%F %H:%M:%S"))
    call append(line(".")+6,"\# Copyright(C):             ".strftime("%Y")." All rights reserved")
    call append(line(".")+7,"\##########################################################################") 
    call append(line(".")+8,"\#!/bin/bash")
    call append(line(".")+9,"")
    else 
    call setline(1, "/*************************************************************************") 
    call append(line("."), "         > File Name: ".expand("%")) 
    call append(line(".")+1, "       > Author:   Name") 
    call append(line(".")+2, "       > Mail: xxxxxxxxxx@xxx.com") 
    call append(line(".")+3, "       > Created Time: ".strftime("%c")) 
    call append(line(".")+4, "************************************************************************/") 
    call append(line(".")+5, "")
endif
    if &filetype == 'cpp'
    call append(line(".")+6, "#include<iostream>")
    call append(line(".")+7, "using namespace std;")
    call append(line(".")+8, "")
endif
    if &filetype == 'c'
    call append(line(".")+6, "#include<stdio.h>")
    call append(line(".")+7, "")
endif
    if &filetype == 'java'
    call append(line(".")+6,"public class ".expand("%"))
    call append(line(".")+7,"")
endif
""新建文件后,自动定位到文件末尾
endfunc
autocmd BufNewFile * normal G
END



mkdir -p /etc/yum.repos.d/backup
\mv -f /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
#禁用已有其他epel源,并做备份。



umount /dev/cdrom
#[ `cat /etc/fstab|grep -o iso` ]||sed -i '/iso/c\' /etc/fstab   
                                                          #解挂其他光盘


CDUUID=`blkid|sed -nr '/sr0/s/.*\bUUID="([^"]+).*"/\1/p'`
CDTYPE=`blkid|sed -nr '/sr0/s/.*\bTYPE="([^"]+).*"/\1/p'`
[ `cat /etc/fstab|grep -o iso` ]||mkdir -p  /cdrom        #创建光盘目录
[ `cat /etc/fstab|grep -o iso` ]||echo -e "UUID=$CDUUID              /cdrom                  $CDTYPE defaults       0 0" >>/etc/fstab
                                                       #修改fstab文件,方便以后自动挂载光盘
#[ `cat /etc/fstab|grep -o iso` ]|| mount -o ro /dev/sr0  /cdrom   #挂载光盘
#mount -a   #挂载光盘
mount -o ro /dev/sr0  /cdrom   #挂载光盘


#生成常用yum源和epel源,这里选用阿里云的
cat > /etc/yum.repos.d/cdrom.repo << END
[cdrom]
name=cdrom-repo
baseurl=file:///cdrom
gpgcheck=0
enabled=1
END
cat > /etc/yum.repos.d/aliyun.repo << "END"
[aliyun]
name=aliyun-epel
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7
enabled=1
END
#新建本地及阿里云epel源.

#修改网卡配置,根据个人情况修改
cat > /etc/sysconfig/network-scripts/ifcfg-ens33 << END
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=29126291-418f-4a08-b33e-c5cfa659d9b8
DEVICE=ens33
ONBOOT=yes
IPADDR=172.18.32.7
PREFIX=16
GATEWAY=172.18.0.1
DNS1=114.114.114.114
END


systemctl restart network.service
#重启网络服务,让刚才修改的配置生效

mkdir -p /date/apps

[ -z `cat ~/.bash_profile|grep apps` ] && echo 'PATH=/date/apps:$PATH' >>~/.bash_profile
#修改PAT变量,方便以后装软件。

  水平有限,都是简单代码,留存记录,方便日后查看整理改进。


一个低调的男人