当前位置: 移动技术网 > 科技>操作系统>Linux > shell脚本练习->1

shell脚本练习->1

2018年09月20日  | 移动技术网科技  | 我要评论

->系统当前状态脚本

 

脚本内容如下:

 1 [root@jumpserver-70 scripts]# cat system.sh 
 2 #!/bin/bash
 3 
 4 #系统信息
 5 system_version=$(hostnamectl | awk -f ':' '/operating system/{print $2}')
 6 system_kernel=$(hostnamectl | awk -f ':' '/kernel/{print $2}')
 7 system_network=$(hostname -i)
 8 system_host=$(hostnamectl |awk -f ':' '/static hostname/{print $2}')
 9 network_out=$(curl -s icanhazip.com)
10 
11 #cpu
12 cpu_load=$(w|awk -f ':' 'nr==1{print $nf}')
13 
14 #内存
15 mem_total=$(free -h | awk '/^mem/{print $2}')
16 mem_use=$(free -h | awk '/^mem/{print $3}')
17 mem_b=$(free -m | awk '/^mem/{print int($3*100/$2)}')
18 
19  
20 
21 echo "-------------------------------system info-----------------------------------------"
22 
23 echo "当前系统的版本:$system_version
24 当前系统的内核:$system_kernel
25 当前系统的虚拟化平台:
26 当前系统的主机名:$system_network
27 当前系统的内网ip:$system_host
28 当前系统的外网ip:$network_out
29 "
30 
31 echo "-------------------------------system cpu-----------------------------------------"
32 
33 echo "当前cpu负载情况 : 1分钟,5分钟,15分钟使用:$cpu_load"
34 
35 echo "-------------------------------system mem---------------------------------------"
36 
37 echo "当前总内存大小:$mem_total
38 当前内存使用率:$mem_use
39 当前内存使用百分比:$mem_b%
40 "
41 echo "-------------------------------system disk---------------------------------------"
42 for ((i=2;i<=8;i++))
43 do
44 df -h |awk 'nr=='$i'{print "磁盘分区:"$nf,"使用了"$(nf-1)}'
45 done
46 
47 echo "--------------------------------------------------------------------------------------"

 

实现效果如下:

 


->查看系统当前内存状态

 1 [root@jumpserver-70 scripts]# cat mem.sh 
 2 #!/bin/bash
 3 
 4 mem_total=$(free -m|grep "^m"|awk '{print $2}')
 5 mem_use=$(free -m|grep "^m"|awk '{print $3}')
 6 mem_b=$((($mem_use*100)/$mem_total))
 7 
 8 
 9 if [ $mem_b -ge 30 ];then
10   echo -e "\033[31m memory is err ${mem_b}% \033[0m" 
11 else
12   echo -e "\033[32m memory is ok ${mem_b}% \033[0m"
13 fi

 

实现效果如下:

[root@jumpserver-70 scripts]# sh mem.sh
memory is ok 9%

 

->查看ip 是否畅通

 1 [root@jumpserver-70 scripts]# cat ping.sh 
 2 #!/bin/bash
 3 
 4 read -p "input ip: " ip
 5 ping -c2 $ip &>/dev/null
 6 
 7 if [ $? -eq 0 ];then
 8   echo "host $ip is ok"
 9 else
10   echo "host $ip is error"
11 fi

 

->创建用户并设置密码

 1 [root@jumpserver-70 scripts]# cat user.sh 
 2 #!/bin/bash
 3 
 4 #1.判断用户输入是否为空
 5 read -p "请输入要创建用户的数量 :" num
 6 if [[ -z $num ]];then
 7   echo "输入的用户名不能为空"
 8   exit 1
 9 fi
10 
11 
12 #2.判断用户输入的是否为数字
13 if [[ ! "$num" =~ ^[0-9]+$ ]];then
14 
15   echo "你输入的不是数字"
16   exit 1
17 fi
18 
19 #3.判断用户输入的是否为空
20 read -p "请输入要创建用户的名称 :" name
21 if [[ -z $name ]];then
22   echo "用户名不能为空值"
23   exit 1
24 fi
25 
26 #4.判断用户输入的是否为数字
27 if [[ ! "$name" =~ ^[a-z]+$ ]];then
28   echo "你输入的不能是小写字母"
29   exit 1
30 fi
31 
32 
33 #5.遍历用户输入的数字
34 for i in $(seq $num);do
35 
36 useradd $name$i # 创建用户
37   echo "123" |passwd --stdin $name$i &>/dev/null #给新创建的用户设置密码123
38   echo "$name$i用户创建成功,密码为:123"
39 
40 done

 

->安装nginx->查看状态

 1 echo "===============================system repo============================="
 2 repos=$(yum repolist |grep nginx|wc -l)
 3 if [ $repos -eq 0 ];then
 4 
 5   echo "没有发现nginx的yum仓库...尝试中"
 6 
 7 cat >/etc/yum.repos.d/nginx.repo <<-eof #使用cat方法导入
 8 [nginx]
 9 name=nginx repo
10 baseurl=http://nginx.org/packages/centos/7/x86_64/
11 gpgcheck=0
12 enabled=1
13 eof
14 
15   yum makecache &>/dev/null #清空缓存
16 
17   repos2=$(yum repolist |grep nginx|wc -l)          #再次yum尝试安装
18 
19 if [ $repos2 -eq 1 ];then
20   echo "yum仓库已经安装完成...."
21 else
22   echo "请检查你的网络环境...."
23   exit 1
24   fi
25 else
26 
27   echo "已存在nginx的repos仓库文件..."
28 
29 fi
30 
31 echo "===============================system nginx install======================="
32 
33 rpm_nginx=$(rpm -qa nginx | wc -l)
34 
35 if [ $rpm_nginx -gt 0 ];then
36   echo "nginx 已经安装....."
37 else
38   echo "尝试安装nginx...."
39   yum install nginx -y &>/dev/null    
40 fi
41 
42 echo "=======================system nginx status ======================"
43 
44 nginx_status=$(systemctl status nginx|grep running|wc -l)
45 if [ $nginx_status -eq 1 ];then
46 
47   echo "nginx已经启动"
48 
49 else
50   echo "nginx尝试启动"
51 
52   pkill httpd &>/dev/null    
53   pkill nginx &>/dev/null
54 
55   systemctl start nginx &>/dev/null
56 
57 if [ $? -eq 0 ];then
58 
59 
60   nginx_status2=$(systemctl status nginx|grep active|awk '{print $2 $3}')
61   echo "nginx启动完成, 当前的状态是: $nginx_status2"
62 
63 
64 else
65   echo "启动失败, 请手动排查......"
66 fi
67 fi

 

->nginx状态管理

 1 [root@jumpserver-70 scripts]# cat nginx_status.sh 
 2 #!/bin/bash
 3 
 4 echo "-----------------------"
 5 echo "1:开启"
 6 echo "2:停止"
 7 echo "3:重载"
 8 echo "4:重启"
 9 echo "5:状态"
10 echo "-----------------------"
11 
12 
13 read -p "请输入您要执行的命令:" command
14 
15 case $command in 
16 1)
17   status_start=$(systemctl status nginx | egrep "running" |wc -l)
18   if [ $status_start -eq 1 ];then
19 
20   echo "nginx已经启动,不需要执行启动操作"
21 else
22   systemctl start nginx &>/dev/null
23   echo "nginx已经启动"
24 fi    
25   ;;
26 2)
27   systemctl stop nginx &>/dev/null
28   echo "nginx已经停止"
29   ;;
30 3)
31 status_start=$(systemctl status nginx | egrep "running" |wc -l)
32 if [ $status_start -eq 1 ];then
33   systemctl reload nginx &>/dev/null
34   echo "nginx已经重载"
35 fi
36   ;;
37 4)    
38   systemctl restart nginx &>/dev/null
39   echo "nginx已经重启"
40   ;;
41 5)
42   systemctl status nginx 
43   ;;
44 *)
45   echo "请您选择正确的命令"
46   exit 1
47 esac

 

->选择php版本安装

 1 [root@jumpserver-70 scripts]# cat php.sh 
 2 #!/bin/bash
 3 
 4 echo '1=(php-5.0)'
 5 echo '2=(php-6.0)'
 6 echo '3=(php-7.0)'
 7 
 8 read -p "请输入要安装的版本:" num
 9 
10 if [ $num -eq 1 ];then
11 
12   echo "正在安装php5.0"
13 
14 elif [ $num -eq 2 ];then
15 
16   echo "正在安装php6.0"
17 
18 elif [ $num -eq 3 ];then 
19 
20   echo "正在安装7.0"
21 else
22   echo "请选择安装版本"
23 fi

 

->根据系统版本安装yum源

 1 [root@oldboyfly /server/scripts]# cat yum.sh 
 2 #!/bin/bash
 3 
 4 os_name=$(cat /etc/redhat-release )
 5 os_version=$(cat /etc/redhat-release |awk -f " " '{print $4}'| awk -f "." '{print $1}')
 6 
 7 if [ $os_version = "(final)" ];then
 8   os_version=$(cat /etc/redhat-release |awk '{print $3}'|awk -f '.' '{print $1}')
 9 
10 fi
11 
12 if [ $os_version -eq 7 ];then
13 
14   echo "这是$os_name的系统,请安装centos7的yum源"
15   wget -o /etc/yum.repos.d/centos-base.repo http://mirrors.aliyun.com/repo/centos-7.repo &>/dev/null
16   echo "$os_name:yum源安装完成"
17 
18 elif [ $os_version -eq 6 ];then
19 
20   echo "这是$os_name系统,请安装centos6的yum源"
21   wget -o /etc/yum.repos.d/centos-base.repo http://mirrors.aliyun.com/repo/centos-6.repo &>/dev/null
22   echo "$os_name:yum源安装完成"
23 
24 elif [ $os_version -eq 5 ];then 
25 
26   echo "这是$os_name系统,请安装centos5的yum源"
27   wget -o /etc/yum.repos.d/centos-base.repo http://mirrors.aliyun.com/repo/centos-5.repo &>/dev/null
28   echo "$os_name:yum源安装完成"
29 
30 else
31   echo "请检查系统的版本"
32 
33 fi

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网