--作者:飞翔的小胖猪
--创建时间:2021年3月13日
--修改时间:2021年3月17日
说明
每日上传更新一个shell脚本,周期为60天。如有需求的读者可根据自己实际情况选用合适的脚本,也可在评论区留言提出脚本需求,作者会尽快根据需求编写相关脚本对功能进行实现。
每篇文章包含5个脚本。
总进度:10/12
上一篇脚本链接:https://www.cnblogs.com/Pigs-Will-Fly/p/14497893.html
下一篇脚本链接:https://www.cnblogs.com/Pigs-Will-Fly/p/14592317.html
主要内容
21年3月13日-安装jdk脚本
************************************************************************************************************************************************************************************************************************************
脚本说明
根据用户输入的jdk二进制文件路径与安装路径,安装配置jdk。
文件说明
install_jdk.sh:脚本主体文件
脚本主体
[root@135 46_install_jdk]# cat install_jdk.sh #!/bin/bash #安装jdk install_jdk(){ #检测文件中是否配置有jdk环境变量,如果有JAVA_HOME设置表示可能已经安装了java,存在则退出 java_num=`cat /etc/profile | grep -i java_home |wc -l` if [ ! ${java_num} -eq 0 ];then echo -e "e[31mJAVA_HOME PATH exits........e[0m" exit 22 fi #输入jdk二进制文件路径,判断输入的值是否存在 while : do echo -e "e[31m" read -p "Please intput jdk bin file : " file_name echo -e "e[34mjdk二进制文件路径:e[33m ${file_name:-not}e[0m" if [ -f ${file_name} ];then echo -e "e[32m输入文件名正确。e[0m" break else echo -e "输入的文件路径错误,请核对路径名,绝对路径。" fi echo -e "e[31m" done #键入指定的安装路径,默认为/usr/local/java/jdkv1/ read -p "Please input install dir : " install_dir echo -e "e[34m安装路径:e[33m ${install_dir:-/usr/local/java/jdkv1/}e[0m" if mkdir -p ${install_dir:-/usr/local/java/jdkv1/} &>/dev/null;then echo -e "e[32m安装路径创建成功。${install_dir}e[0m" else echo -e "e[31m安装路径创建失败。${install_dir}e[0m" fi unzip_file_name=`tar -ztvf ${file_name} |head -2 | awk '{print $6}' | awk -F'/' '{print $1}' |uniq` echo -e "e[34m解压目录:./${unzip_file_name}e[0m" mkdir -p ${unzip_file_name} &> /dev/null tar -zxvf ${file_name} -C ./ &> /dev/null #复制所有文件至指定安装目录 cp -rp ./${unzip_file_name}/* ${install_dir:-/usr/local/java/jdkv1/} &>/dev/null #添加环境变量 echo "JAVA_HOME=${install_dir:-/usr/local/java/jdkv1/}" >> /etc/profile echo 'CLASSPATH=.:$JAVA_HOME/lib.tools.jar' >> /etc/profile echo 'PATH=$JAVA_HOME/bin:$PATH' >> /etc/profile echo 'export JAVA_HOME CLASSPATH PATH' >> /etc/profile #执行文件 source /etc/profile } #检测系统安装是否完成 check_status(){ if java -version &>/dev/null && which java &>/dev/null ;then echo -e "e[32m java配置完成。e[0m" else echo -e "e[31m java配置失败。e[0m" exit 66 fi } main(){ #判断系统中是否存在java if which java &>/dev/null;then echo -e "e[33m系统中已存在java软件,不需要配置。e[0m" echo -e "e[34mJAVA信息:e[0m" echo -e "`java -version`" exit 88 fi install_jdk check_status } main
结果
**************************************************************************************************************2021年3月13日脚本结束*****************************************************************************************************************
21年3月14日-Ubuntu网络配置文件脚本
************************************************************************************************************************************************************************************************************************************
脚本说明
用户输入待配置ip、掩码、dns、网关等信息,执行脚本会自动生成网络配置文件格式,读者可以使用重定向等手段直接把文件写入到指定的路径下。
该脚本至适用于Ubuntu操作系统,切记。
文件说明
Ubuntu_net_configure_example.sh:脚本主体文件
脚本主体
[root@135 47_Ubuntu_net_configure_example]# cat Ubuntu_net_configure_example.sh #!/bin/bash #定义一些基础配置信息。 NET_NAME="ens33" DHCP_STATA="yes" ADDRESS='192.168.111.140' DNS1="192.168.111.2" DNS2="114.114.114.114" GATEWAY="192.168.111.2" #展示16及以下的系统网络配置文件模板 display_file_exp_16(){ echo -e "配置文件路径:e[34m/etc/network/interfacese[0m" echo -e "配置文件内容:" if [ ${DHCP_STATA} == "yes" ];then echo -e "e[33mauto ${NET_NAME} iface ${NET_NAME} inet dhcpe[0m " else echo -e "e[33mauto ${NET_NAME} iface ${NET_NAME} inet static address ${ADDRESS} netmask 255.255.255.0 dns-nameservers ${DNS1} dns-nameservers ${DNS2} gateway ${GATEWAY}e[0m " fi } #展示17及以上的系统网络配置文件模板 display_file_exp_17(){ echo -e "配置文件路径:e[34m/etc/netplan/00-installer-config.yamle[0m" echo -e "配置文件内容:" if [ ${DHCP_STATA} == "yes" ];then echo -e "e[33mnetwork: ethernets: ${NET_NAME}: dhcp4: true version: 2e[0m" else echo -e "e[33mnetwork: ethernets: ${NET_NAME}: dhcp4: no addresses: [${ADDRESS}/24] gateway4: ${GATEWAY} nameservers: addresses: [${DNS1},${DNS2}] version: 2e[0m" fi } #检测系统版本,根据版本选择指定的函数 check_version(){ version_id=`cat /etc/lsb-release | awk -F"=" '/DISTRIB_RELEASE=/{print $2}' | awk -F'.' '{print $1}'` if [ ${version_id} -lt 17 ];then echo -e "e[31mSystem os version ${version_id}e[0m" #调用展示16以以下的网络配置 display_file_exp_16 else echo -e "e[31mSystem os version ${version_id}e[0m" display_file_exp_17 fi } #定义主函数 main(){ check_version echo -e "e[0m" } #调用主函数 main
结果
16系统版本执行结果
17以上执行结果
**************************************************************************************************************2021年3月14日脚本结束*****************************************************************************************************************
21年3月15日-安装lvs脚本
************************************************************************************************************************************************************************************************************************************
脚本说明
执行脚本在ubuntu和centos系列操作系统上安装keepalived和ipvsadm软件配置并启动。
读者在使用时请根据实际环境填入虚拟ip、真实节点ip、节点负载端口号信息。
文件说明
install_keepalived_ipvsadm.sh:脚本主体文件
脚本主体
[root@135 48_install_keepalived_ipvsadm]# cat install_keepalived_ipvsadm.sh #!/bin/bash #检查判断系统类型,centos使用yum安装、ubuntu使用apt安装。 check_system_type(){ echo -ne "System tyep: " #判断系统是否为centos或redhat if cat /etc/issue | grep -i kernel &>/dev/null;then echo -e "e[34mCentose[0m" install_soft_c fi #判断系统是否为ubuntu if cat /etc/issue | grep -i Ubuntu &>/dev/null ;then echo -e "e[34mUbuntue[0m" install_soft_u fi } #ubuntu操作系统安装软件 install_soft_u(){ echo -e "e[33m开始进行软件安装......................e[0m" apt clean all &>/dev/null apt update &>/dev/null apt-cache dump &>/dev/null if apt install ipvsadm keepalived -y &>/dev/null;then echo -e "e[32m执行安装成功.e[0m" else echo -e "e[31mapt configure errr or not keepalived/ipvsadm package,Please check configure.e[0m" exit 66 fi } #centos操作系统安装软件 install_soft_c(){ echo -e "e[33m开始进行软件安装......................e[0m" yum clean all &> /dev/null yum repolist &> /dev/null if yum install keepalived ipvsadm -y &>/dev/null;then echo -e "e[32m执行安装成功.e[0m" else echo -e "e[31myum configure errr or not keepalived/ipvsadm package,Please check configure.e[0m" exit 66 fi } #设置net.ipv4.ip_nonlocal_bind = 1 edit_kernel_args(){ echo -e "e[33m开始修改内核参数......................e[0m" sed -i '/net.ipv4.ip_nonlocal_bind/d' /etc/sysctl.conf echo 'net.ipv4.ip_nonlocal_bind = 1' >> /etc/sysctl.conf if sysctl -p | grep -i 'net.ipv4.ip_nonlocal_bind = 1' &>/dev/null;then echo -e "e[32m内核参数修改完成.e[0m" fi } #configure keepalived and ipvsadm configure_file(){ echo -e "e[33m开始配置keepalived及ipvsadm配置......................e[0m" if [ -f /etc/keepalived/keepalived.conf ];then mv /etc/keepalived/keepalived.conf /etc/keepalived/keepalived.conf.bak fi #写入内容到配置文件 echo '! Configuration File for keepalived ' >> /etc/keepalived/keepalived.conf echo ' ' >> /etc/keepalived/keepalived.conf echo 'global_defs { ' >> /etc/keepalived/keepalived.conf echo ' router_id lvs.01 ' >> /etc/keepalived/keepalived.conf echo '} ' >> /etc/keepalived/keepalived.conf echo ' ' >> /etc/keepalived/keepalived.conf echo 'vrrp_instance VI_1 { ' >> /etc/keepalived/keepalived.conf echo ' state MASTER ' >> /etc/keepalived/keepalived.conf echo ' interface ens33 ' >> /etc/keepalived/keepalived.conf echo ' virtual_router_id 51 ' >> /etc/keepalived/keepalived.conf echo ' priority 100 ' >> /etc/keepalived/keepalived.conf echo ' advert_int 1 ' >> /etc/keepalived/keepalived.conf echo ' authentication { ' >> /etc/keepalived/keepalived.conf echo ' auth_type PASS ' >> /etc/keepalived/keepalived.conf echo ' auth_pass 1111 ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo ' virtual_ipaddress { ' >> /etc/keepalived/keepalived.conf echo ' 192.168.111.160/24 ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo '} ' >> /etc/keepalived/keepalived.conf echo ' ' >> /etc/keepalived/keepalived.conf echo 'virtual_server 192.168.111.160 80 { ' >> /etc/keepalived/keepalived.conf echo ' delay_loop 6 ' >> /etc/keepalived/keepalived.conf echo ' lb_algo wrr ' >> /etc/keepalived/keepalived.conf echo ' lb_kind DR ' >> /etc/keepalived/keepalived.conf echo ' #persistence_timeout 50 ' >> /etc/keepalived/keepalived.conf echo ' protocol TCP ' >> /etc/keepalived/keepalived.conf echo ' ' >> /etc/keepalived/keepalived.conf echo ' real_server 192.168.111.135 80 { ' >> /etc/keepalived/keepalived.conf echo ' weight 10 ' >> /etc/keepalived/keepalived.conf echo ' TCP_CHECK { ' >> /etc/keepalived/keepalived.conf echo ' connect_timeout 10 ' >> /etc/keepalived/keepalived.conf echo ' retry 3 ' >> /etc/keepalived/keepalived.conf echo ' delay_before_retry 3 ' >> /etc/keepalived/keepalived.conf echo ' connect_port 6443 ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo ' ' >> /etc/keepalived/keepalived.conf echo ' real_server 192.168.111.129 80 { ' >> /etc/keepalived/keepalived.conf echo ' weight 10 ' >> /etc/keepalived/keepalived.conf echo ' TCP_CHECK { ' >> /etc/keepalived/keepalived.conf echo ' connect_timeout 10 ' >> /etc/keepalived/keepalived.conf echo ' retry 3 ' >> /etc/keepalived/keepalived.conf echo ' delay_before_retry 3 ' >> /etc/keepalived/keepalived.conf echo ' connect_port 6443 ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo ' } ' >> /etc/keepalived/keepalived.conf echo '} ' >> /etc/keepalived/keepalived.conf if systemctl restart systemctl restart keepalived.service &>/dev/null || service keepalived restart &>/dev/null;then echo -e "e[32mkeepalived和ipvsadm安装配置完成,并成功启动。e[0m" else echo -e "e[31m配置文件存在问题,服务无法启动,请检查。e[0m" fi } #定义主函数 main(){ check_system_type edit_kernel_args configure_file } #调用组函数 main
结果
**************************************************************************************************************2021年3月15日脚本结束*****************************************************************************************************************
21年3月16日-判断闰年脚本
************************************************************************************************************************************************************************************************************************************
脚本说明
用户输入年份脚本判断输入的年份是否为闰年,不输入则判断当前年是否为闰年。
文件说明
determine_leap_year.sh:脚本主体文件
脚本主体
[root@135 49_To_determine_leap_year]# cat determine_leap_year.sh #!/bin/bash #输入年份 input_year(){ echo -e "e[34m" read -p "Please enter the year you want to query,The default year is this year: " year_i now_year=`date +%Y` year=${year_i:-$now_year} echo -e "检测年份为:e[33m ${year}" } #判断是否为闰年 #1.能够被400整除 #2.能够被4整除,但是不能被100整除 check_year(){ let y1=${year}%400 let y2=${year}%100 let y3=${year}%4 echo -ne "e[34m结果为:e[32m" #如果能被400整除直接输出是闰年,程序结束 if [ ${y1} -eq 0 ];then echo -e "闰年.e[0m " exit 0 fi #当不能被400整除时,如果能被100整除表示不是闰年,程序结束 if [ ${y2} -eq 0 ];then echo -e "e[31m平年.e[0m " exit 0 fi #当以上条件都不满足时,如果能被4整除表示是闰年,否则为平年 if [ ${y3} -eq 0 ];then echo -e "闰年.e[0m " exit 0 else echo -e "e[31m平年.e[0m " fi } #定义主函数 main(){ input_year check_year } #调用主函数 main
结果
**************************************************************************************************************2021年3月16日脚本结束*****************************************************************************************************************
21年3月17日-质数判断脚本
************************************************************************************************************************************************************************************************************************************
脚本说明
输入一个数字判断是否为质数。
文件说明
prime_Numbers_judgment.sh:脚本主体文件
脚本主体
[root@135 100_shell]# cat prime_Numbers_judgment.sh #!/bin/bash #测试质数脚本 check_number(){ read -p "请输入一个数:" num if [ $num -le 0 ];then #判断这个数是否小于0 echo "您输入的数为负数或0" #echo提示用户 elif [ $num -eq 1 ];then #判断这个数是否为1和2 echo "$num 质数" elif [ $num -eq 2 ];then echo "$num 质数" else #当输入的数是正整数时 for ((i=2;i<$num;i++)) do if [ $(($num%i)) -eq 0 ];then #判断这个数与i取余是否等于0 [ $num -ne $(($num%i)) ] && echo "$num 非质数" && break else if [ $i -gt $(($num/2)) ];then echo "$num 质数" && break fi fi done fi } #定义主函数 main(){ check_number } #调用主函数 main
结果
**************************************************************************************************************2021年3月17日脚本结束*****************************************************************************************************************