2019.8.21——shell脚本3


#!/usr/bin/bash
#by yy

read -p "Please input a username: " user
id $user &> /dev/null

if [ $? -ne 0 ] ;then
        echo "no such user: $user"

fi

read -p "Are you sure ? [y/n]: " action

if [ $action = "y" ];then
        userdel -r $user
else
        echo "good!"
fi

case 语句
第一个

#!/bin/bash
read -p "pls int a num:" n
case "$n" in
1)
        echo "变量是1"  
        ;;
2)              
        echo "变量是2" 
        ;;
3)      
        echo "变量是3" 
        ;;
*)
        echo "重新输入1-3"

esac

结果:

[root@localhost tmp]# sh 121.sh 
pls int a num:1  
变量是1
[root@localhost tmp]# sh 121.sh 
pls int a num:4
重新输入1-3

第二个:

#!/usr/bin/bash
#by yy

web1=192.168.48.148
web2=192.168.48.150

cat <<-EOF    //直接显示在屏幕上的东西,实用性,框架比较好看
1. web1
2. web2
EOF

read -p " input your number: " num

case "$num" in    //
1)
        ssh root@$web1    
        ;;
2)
        ssh root@$web2
        ;;
esac

结果:

[root@localhost tmp]# sh 12.sh 
1. web1
2. web2
 input your number: 1
The authenticity of host '192.168.48.148 (192.168.48.148)' can't be established.
ECDSA key fingerprint is SHA256:VLAru20jLTdQbjUsB3+0dLufICz88dz69g/e+3U7yHQ.
ECDSA key fingerprint is MD5:ac:c1:9b:05:47:8b:53:d9:1d:cb:17:84:73:4d:39:53.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.48.148' (ECDSA) to the list of known hosts.
root@192.168.48.148's password: 
Last login: Wed Aug 21 09:35:49 2019 from 192.168.48.1
[root@localhost ~]# exit
logout
Connection to 192.168.48.148 closed.
[root@localhost tmp]# sh 12.sh 
1. web1
2. web2
 input your number: 2
The authenticity of host '192.168.48.150 (192.168.48.150)' can't be established.
ECDSA key fingerprint is SHA256:6/lQ6BgPa7LPgJZaDKnGAat6+l6L0Edj8srZjGauwwE.
ECDSA key fingerprint is MD5:e4:cb:5f:f4:c1:6e:9f:7c:54:43:82:a1:d7:06:c4:74.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.48.150' (ECDSA) to the list of known hosts.
root@192.168.48.150's password: 
Last login: Tue Aug 20 20:23:25 2019 from 192.168.48.1
[root@localhost ~]# 

第三个:

#!/bin/bash

read -p "pls input score to test level: " score

if [ $score -ge 90 ];then
        echo "优秀" 
elif [ $score -ge 80 ];then
        echo "良好"
elif [ $score -ge 70 ];then
        echo "中等"
elif [ $score -ge 60 ] ;then
        echo "及格"
else 
        echo "不及格"
fi 

结果:

[root@localhost tmp]# sh 20.sh 
pls input score to test level: 77
中等

第四个:

cat << EOF
m|M) show memory usages;
d|D) show disk usages;
q|Q) qiut
EOF

read -p "Your choice " choice
case $choice in
m|M)
        free -m
        ;;
d|D)
        df -h
        ;;
q|Q)
        exit
        ;;
*)
        echo "Invalid input"
        ;;
esac

结果:

[root@localhost tmp]# sh 14.sh 
m|M) show memory usages;
d|D) show disk usages;
q|Q) qiut
Your choice m
              total        used        free      shared  buff/cache   available
Mem:           1823         246        1296           8         280        1389
Swap:          2047           0        2047
[root@localhost tmp]# sh 14.sh 
m|M) show memory usages;
d|D) show disk usages;
q|Q) qiut
Your choice d
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  1.6G   16G  10% /
devtmpfs                 901M     0  901M   0% /dev
tmpfs                    912M     0  912M   0% /dev/shm
tmpfs                    912M  8.8M  903M   1% /run
tmpfs                    912M     0  912M   0% /sys/fs/cgroup
/dev/sda1               1014M  143M  872M  15% /boot
tmpfs                    183M     0  183M   0% /run/user/0
[root@localhost tmp]# 

第五个;

#!/usr/bin/bash
#while create user
#by yy

while read user
do
        useradd $user
        if [ $? = 0 ];then
                echo "$user is created"
        fi
done

结果:(批量创建用户(不建议使用,比较麻烦) while循环)

[root@localhost tmp]# sh 15.sh 
kkk
kkk is created
hhh
useradd: group hhh exists - if you want to add this user to that group, use -g.
lll
lll is created
^Z
[6]+  Stopped                 sh 15.sh
[root@localhost tmp]# 

脚本:明确功能,确定语法,添加框架,框架内容

第六个:

#!/bin/bash
for ip in `seq 1 254`
do {    
        ping -c1 192.168.48.$ip >/dev/null 2>/erro.txt
        if [ $? -eq 0 ];then
                echo 192.168.48.$ip up
        else
                echo 192.168.48.$ip down
        fi
}&
done

结果:

[root@localhost tmp]# sh 21.sh 
192.168.48.2 up
192.168.48.148 up
[root@localhost tmp]# 192.168.48.150 up
192.168.48.10 down
192.168.48.29 down
192.168.48.18 down
192.168.48.16 down
。。。。。。

第七个:

#!/bin/bash
echo "准备倒数5秒"
for i in $(seq 5 -1 1)
do
        echo -en "$i";
        sleep 1

done
echo -e "开始"

结果:

[root@localhost tmp]# sh 17.sh 
准备倒数5秒
54321开始
[root@localhost tmp]# 

第八个;

#!/bin/bash
for i in $(cat /root/users.txt)
do
        useradd $i
        echo "123456" | passwd --stdin $i

done
~     

在写一个/root/users.txt  (想添加哪个用户,就可以添加哪个)
guogdong1
gongdong2
gongdong3
gongdong4
gongdong5

结果

[root@localhost tmp]# sh 18.sh 
Changing password for user guogdong1.
passwd: all authentication tokens updated successfully.
Changing password for user gongdong2.
passwd: all authentication tokens updated successfully.
Changing password for user gongdong3.
passwd: all authentication tokens updated successfully.
Changing password for user gongdong4.
passwd: all authentication tokens updated successfully.
Changing password for user gongdong5.
passwd: all authenticat

第九个:

#!/bin/bash
read -p "请输入用户户名的前缀: " a
read -p "请输入用户的数目:" num
if (($num<=10));then
        n=0
        for i in `seq $num`

        do
                if useradd $a$i &>/dev/null
                        then
                        echo "用户的$a$i创建成功!"
                        ((n++))
                        echo "123456" |passwd --stdin $a$i &>/dev/null
                fi

        done
        echo "一共创建了(($n))个用户"
else
        echo "最多只能创建10个用户了"
fi

结果:

[root@localhost tmp]# sh 19.sh 
请输入用户户名的前缀: ggg
请输入用户的数目:7
用户的ggg1创建成功!
用户的ggg2创建成功!
用户的ggg3创建成功!
用户的ggg4创建成功!
用户的ggg5创建成功!
用户的ggg6创建成功!
用户的ggg7创建成功!
一共创建了((7))个用户
[root@localhost tmp]# 


下午

看端口,看pid

netstat -tnlp |grep vsftpd
	参数

//安装netstat,,,安装net-tools即可

练习:判断本机的httpd服务是否正常,如果停止请自动拉起服务

ss -tnlp |grep sshd

查看端口 及tcp

LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=998,fd=3))
LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=998,fd=4))

和lsof的区别,就是这俩是监听服务的,lsof监听的是端口,在不知道端口时候,
直接查服务。

第一个:

#!/bin/bash

echo "1.[install lamp]"
echo "2.[install lnmp]"
echo "3.[exit]"

read -p "ple input tut the num want: " a

case
1)
        if
                [-f lamp.sh];then
                sleep 2
                echo "lamp is installed"
        else
                echo "no lamp.sh"
        fi
        ;;
2)
        if
                [-f lnmp.sh];then
                sleep 2
                echo "lnmp is installed"
        else
                echo "no lnmp.sh"

        fi
        ;;
3)
        exit 0
        ;;
*)
        echo "input error"
        exit 0
esac

补充:记得给lnmp.sh lamp.sh 权限。 要提前写好lnmp.sh lamp.sh脚本

练习:一键部署LAMP lnmp 要求用户可以选择 (背着写)
提前配置好yum源 安装epel-release源
安装完成后,自动启动服务并设置开机自启

第二个:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值