栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

基础shell脚本练习

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

基础shell脚本练习

1.break函数

#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         break.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
for i in {1..10};do
    if [ $i -eq 5 ];then break ;fi
    echo $i
done

2.颜色字体脚本

#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         colure.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \033[1;32m"
    SETCOLOR_FAILURE="echo -en \033[1;31m"
    SETCOLOR_WARNING="echo -en \033[1;33m"
    SETCOLOR_NORMAL="echo -en E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "
    elif [ $2 = "failure" -o $2 = "1"  ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

#[ $# -eq 0 ] && echo "Usage: `basename $0` {success|failure|warning}"

color hello 333

3.continue函数

#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2021-12-29
#FileName:          continue.sh
#URL:               https://www.hanweize.cn
#Description:       The test script
#Copyright (C):     2021 All rights reserved
#********************************************************************
for i in {1..10};do
    if [ $i -eq 5 ];then continue;fi
    echo $i
done

4.case函数

#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         case.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
sum=0
PS3="请点菜(1-6): "
select MENU in 北京烤鸭 佛跳墙 小龙虾 羊蝎子 火锅 点菜结束;do
case $REPLY in
1)
    echo $MENU 价格是 ¥100
    let sum+=100
    ;;
2)
    echo $MENU 价格是 ¥88
    let sum+=88
    ;;
3)
    echo $MENU价格是 ¥66
    let sum+=66
    ;;
4)
    echo $MENU 价格是 ¥166
    let sum+=166
    ;;
5)
    echo $MENU 价格是 ¥200
    let sum+=200
    ;;
6)
    echo "点菜结束,退出"
    break
    ;;
*)
    echo "点菜错误,重新选择"
    ;;
esac
done
echo "总价格是: $sum"

5.系统初始化脚本

#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
color () {
    RES_COL=60
    MOVE_TO_COL="echo -en \033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \033[1;32m"
    SETCOLOR_FAILURE="echo -en \033[1;31m"
    SETCOLOR_WARNING="echo -en \033[1;33m"
    SETCOLOR_NORMAL="echo -en E[0m"
    echo -n "$1" && $MOVE_TO_COL
    echo -n "["
    if [ $2 = "success" -o $2 = "0" ] ;then
        ${SETCOLOR_SUCCESS}
        echo -n $"  OK  "
    elif [ $2 = "failure" -o $2 = "1"  ] ;then
        ${SETCOLOR_FAILURE}
        echo -n $"FAILED"
    else
        ${SETCOLOR_WARNING}
        echo -n $"WARNING"
    fi
    ${SETCOLOR_NORMAL}
    echo -n "]"
    echo
}

#关闭SELinux
disable_selinux () {
    sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
}
#关闭防火墙
disable_firewall (){
    systemctl disable --now firewalld
    color "防火墙已经关闭" 0
}
#支持光盘,/misc/cd对应就是光盘内容
set_cdrom () {
    yum -y install autofs
    systemctl enable --now autofs
}
#配置yum 仓库
yum_config (){
    mkdir /etc/yum.repos.d/backup
    mv  /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/
    cat > /etc/yum.repos.d/base.repo < /etc/profile.d/env.sh < 
#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
disable_selinux(){
    sed -i.bak 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
    echo "SElinux已禁用,重新启动后才可生效"
}
disable_firewall(){
    systemctl disable --now firewalld &> /dev/null
    echo "防火墙已禁用"
}
set_ps1() {
    echo "PS1='[e[1;35m][u@h W]\$[e[0m]'" > /etc/profile.d/reset.sh
    echo "提示符已修改成功,请重新登录生效"
}
set_eth(){
     sed -i.bak  '/GRUB_CMDLINE_LINUX=/s#"$# net.ifnames=0"#' /etc/default/grub
     grub2-mkconfig -o /boot/grub2/grub.cfg &> /dev/null
     echo "网络名称已修改成功,请重新启动才能生效"
}

PS3="请选择相应的编号(1-6): "
MENU='
禁用SELinux
关防火墙 
修改提示符 
修改网卡名
以上全实现
退出
'
select M in $MENU ;do
case $REPLY in 
1)
    disable_selinux
    ;;
2)
    disable_firewall
    ;;
3)
    set_ps1
    ;;
4)  
    set_eth
    ;;
5)  
    disable_selinux
    disable_firewall
    set_ps1
    set_eth
    ;;
6)
    break
    ;;
*)
    echo "请输入正确的数字"
esac
done

6.扫描同网段扫描可ping通主机

#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

NET=10.0.0
cat /dev/null >  hosts.txt
for i in {1..254};do
    if ping -c1 -W1 $NET.$i &> /dev/null ;then
        echo $NET.$i is up | tee -a hosts.txt
    fi
done

7.扫描主机端口

#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

i=1
host=10.0.0.7

while [ $i -le 65535 ];do
    if nc -z $host $i  &> /dev/null ;then
        echo $i | tee -a  port.txt
    fi
    let i++
done
#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

NET=10.0.0
cat /dev/null > hosts2.txt
for((i=1;i<=254;i++));do
    if ping -c1 -W1 $NET.$i &> /dev/null ;then
        echo $NET.$i is up | tee -a hosts2.txt
    fi
done

8.sum函数

#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

sum=0
for i in {1..100};do
    if [ $[i%2] -eq 0 ];then continue;fi
    let sum+=i
done
echo $sum
#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

sum=0
while read student ;do
    age=`echo $student|cut -d" " -f2`
    let sum+=age
done < students.txt
echo $sum

9.sleep函数

#!/bin/bash
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************
hello() {
    echo hello
    sleep 1
    hello
}

hello

10.创建用户脚本

#!/bin/bash
#
#********************************************************************
#Author:            hanweize
#QQ:                822020480
#Date:              2022-05-09
#FileName:         reset.sh
#URL:               www.hanweize.cn
#Description:      The test script
#Copyright (C):     2022 All rights reserved
#********************************************************************

while [ "$1" ] ;do
    useradd $1 && echo $1 is created
    shift
done

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/869664.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号