shell脚本入门(3)

  1. 使用case实现成绩优良差的判断
#!/bin/bash
while :
do
        read -p "请输入一个数值,在0-100之间:" num
case $num in
[0-9]|[1-5][0-9])
        echo "差"
        ;;
[6-7][0-9])
        echo "良"
       ;;
[8-9][0-9]|100)
        echo "优"
        ;;
q)
        exit
        ;;
*)
        echo "您输入的成绩无效$num"
esac
done
  1. for创建20用户
    用户前缀由用户输入
    用户初始密码由用户输入
    例如:test01,test10
#!/bin/bash
read -p "请输入用户的前缀:" pre
read -p "请输入用户的密码:" passwd
for i in `seq -w 20`
do
    user=$pre$i
        if id $user &>/dev/null
        then
            echo $user已经存在 

    else
            useradd $user
        if [ $? -eq 0  ];then
            echo "user create successful,ready set password"
        else
            echo "user create failed"
            exit
        fi      
            echo ${passwd} |passwd $user --stdin &>/dev/null
        if [ $? -eq 0 ];then
            echo "password add successsful"
        else
            echo "failed"
        fi
    fi
done
  1. for ping测试指网段的主机
    网段由用户输入,例如用户输入192.168.2 ,则ping 192.168.2.10 — 192.168.2.20
    UP: /tmp/host_up.txt
    Down: /tmp/host_down.txt
#!/bin/bash
read -p "请输入网段192.168.2: " net
string=192.168.2
if [ "$net" != "$string" ]
then
        echo "请输入正确网段192.168.2: "
        exit
fi
for i in {10..20}
do
        {
        if ping -c2 $net.$i &>/dev/null
        then
                echo "$net.$i is up" >> /tmp/host_up.txt
        else
                echo "$net.$i is down">> /tmp/host_down.txt
        fi
        }&
done
wait
echo "测试完成"

  1. 双网关自动切换

ip route del 删除默认网关

ip route add default via 192.168.2.168 dev eth0 设置网关

#!/bin/bash
gw1=192.168.122.26
gw2=192.168.122.174
while :
do
    ip r del default
    ip r add default via $gw1
    while ping -c1 $gw1 &>/dev/null
    do
        sleep 2
    done

    ip r del default via $gw1
    ip r add default via $gw2
    while ping -c1 $gw2 &>/dev/null
    do
        sleep 2
    done
done &

5.使用case做一个小型的计算器,要求数字和运算符由用户去输入

#!/bin/bash
read -p "请输入第一个数:" num1
read -p "请输入第二个数:" num2
read -p "请输入运算符:" i
case $i in
+)
        sum=$[$num1+$num2];;
-)
        sum=$[$num1-$num2];;
\*)
        sum=$[$num1*$num2];;
\*\*)
        sum=$[$num1**$num2];;
/)
        sum=$[$num1/$num2];;
*)
        exit
esac
echo "$sum"

6.判断/tmp/是否存在文件aa.txt,如果存在则判断文件的权限是否为777,如果不是请修改,如果是请输
出“权限为777”,否则创建文件,并判断文件和/root/install.log文件是否为硬连接文件,如果是输出“yes
hard link”,如果不是输出“no”

#!/bin/bash
if [ -f /tmp/aa.txt ]
then
    priv=`ls -l /tmp/aa.txt | cut -c 2-10`
    if [ "$priv" = "rwxrwxrwx" ]
    then
        echo "权限为777"
    else
        chmod 777 /tmp/aa.txt
    fi
else
    touch /tmp/aa.txt
    if [ /tmp/aa.txt -ef /root/install.log ]
    then
        echo "yes hard link"
    else
        echo "no"
    fi
fi

7.有大马,中马和小马共100匹,现在有100块瓦需要马来驼,大马可以驼3块,中马可以驼两块,小马两
匹马能驼一块,请使用shell脚本求出来大马,中马和小马各有多少匹。

#!/bin/bash
read -p "请输入马的总数:" ma
read -p "请输入瓦的总数:" wa

for ((x=0;x<=$ma;x++))
do
                for ((y=0;y<=$ma;y++))
                do
                        for ((z=0;z<=$ma;z++))
                        do
[ $ma -eq $[x+y+z] ] && [ $wa -eq $[x*3+y*2+z/2] ] && echo -e "$x\t$y\t$z" 
                        let z++
                        done
                done
done

8.客户端使用跳板机自动连接后端的真实机(无需密码认证),要求不允许使用root用户登陆

#!/usr/bin/env bash
#捕捉信号,1 2 3 20都忽略
trap "" 1 2 3 20
while :
do
cat <<-EOF
    1.web1
    2.web2
    3.mysql1
EOF
read -p "input number[1-3]: " num
case $num in
1)
    ssh alice@192.168.122.26
    ;;
2)
    ssh alice@192.168.122.174
    ;;
3)
    ssh alice@192.168.122.168
    ;;
q)
    exit
    ;;
esac 
done

9.使用case配置多系统yum源,如果当前是centos6的系统就配置centos6的yum源,如果是centos7的系统就配置centos7的yum源

#!/bin/bash
dir=/etc/yum.repos.d/backup
if [ ! -d $dir ];then
    mkdir $dir
fi
mv /etc/yum.repos.d/*.repo $dir &>/dev/null

if cat /etc/redhat-release |grep 7 &>/dev/null
then
    echo "为Centos7的系统配置yum源"
    wget -O /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
fi
if cat /etc/redhat-release |grep 6 &>/dev/null
then
    echo "为Centos6的系统配置yum源"
    wget -O /etc/yum.repos.d/163.repo http://mirrors.163.com/.help/CentOS6-Base-163.repo
fi
yum clean all
yum makecache

10.批量实现主机密码的修改,各个主机密码不相同

#!/usr/bin/bash
IFS=' \n'
if [ ! -f /root/.ssh/id_rsa ];then
    ssh-keygen -P "" -f ~/.ssh/id_rsa
fi
for i in `cat ./ip_pass.txt`
do
    ip=`echo $i |awk '{print $1}'`
    /usr/bin/expect <<-EOF
    set timeout 30
    spawn ssh-copy-id $ip
    expect {
        "(yes/no)?" { send "yes\r";exp_continue }
        "password:" { send "centos\r" }
    }
    expect eof
    EOF
    pass=`echo $i |awk '{print $2}'`
    ssh root@$ip "echo $pass |passwd root --stdin"
done
IFS='$ \t\n'

猜你喜欢

转载自blog.csdn.net/qq_42989565/article/details/82191043