shell脚本练手

数据的简单处理

expr:数值的基本运算
$(())可以扩展一些基本的算术功能

shell脚本练手_第1张图片

编写脚本检查服务器运行状态

#!/bin/bash
if [ $# -ge 1 ];then
        systemctl status $1 > /dev/null
        if [ $? -eq 0 ];then
                echo "service is running"
        else
                systemctl start $1
        fi
else
        echo "执行脚本的格式"
        echo "sh $0 服务名"
fi

脚本如下:
shell脚本练手_第2张图片
shell脚本练手_第3张图片

apache服务脚本

#!/bin/bash
case $1 in
        start)
                systemctl $1 httpd
                ps aux | grep httpd
                echo "httpd start "
        ;;
        stop)
                systemctl $1 httpd
                ps aux | grep httpd
                echo "httpd stop"
        ;;
        status)
                systemctl $1 httpd
        ;;
        *)
                echo "use method is $0 start|stop|status"
esac

shell脚本练手_第4张图片
shell脚本练手_第5张图片

读取信息

#!/bin/bash
read -p "please input name:" NAME
read -p "please input age:" AGE
read -p "please input sex:" SEX
cat<

shell脚本练手_第6张图片
shell脚本练手_第7张图片

判断用户在系统中是否存在,是否有家目录

#!/bin/bash
read -p "input a username:" name
if grep $name /etc/passwd;then
        echo "the user $name exists on this system"
elif ls -d /home/$name;then
        echo "the user $name not exists on this system"
        echo "the user $name has a home directory"
else
        echo "the user $name not exists on this system"
        echo "the user $name has not  a home directory"
fi

shell脚本练手_第8张图片
shell脚本练手_第9张图片

日志目录的清空

#!/bin/bash
#clear /var/log/messages

#判断当前是root用户
if [ $USER != "root" ];then
        echo "you have no power"
        exit 10
fi
#判断文件是否存在
if [ ! -f /var/log/messages ];then
        echo "file is not exist"
        exit 12
fi
#保留最近1行的日志内容
tail -1 /var/log/messages > /var/log/mesg.tmp

#日志清理
> /var/log/messages

#cat /dev/null > /var/log/messages

mv /var/log/mesg.tmp /var/log/messages
echo "Logs clean up"

shell脚本练手_第10张图片
执行脚本之前的/var/log/messages
shell脚本练手_第11张图片
执行脚本之后的/var/log/messages
shell脚本练手_第12张图片

学生成绩的判断

#!/bib/bash
read -p "please input grades : " grade
if [ $grade -ge 0 ] && [ $grade -le 59 ];then
        echo "补考在等你"
elif [ $grade -ge 60 ] && [ $grade -le 70 ];then
        echo "你刚及格,小样"
elif [ $grade -ge 71 ] && [ $grade -le 85 ];then
        echo "你好厉害"
elif [ $grade -ge 86 ] && [ $grade -le 100 ];then
        echo "优秀的少年"
else
        echo "你想多了"
fi

shell脚本练手_第13张图片
shell脚本练手_第14张图片

etc目录的备份

#!/bin/bash
baknamefile=`date +%Y-%m-%d-%H-%M-%S`
bakdir=/etcbak
srcdir=/etc
[ -e $bakdir ] || mkdir $bakdir
tar zcvf ${bakdir}/${baknamefile}-etc.tar.gz /etc/
echo "=========================="
ls -lh ${bakdir}/${baknamefile}-etc.tar.gz
echo "back etc is ok!"                 

shell脚本练手_第15张图片
shell脚本练手_第16张图片

简易操作文件

#!/bin/bash
cat << eof
*******************
1.backup
2.copy
3.quit
*******************
eof
read -p "input your choice:" op
case $op in
        1|backup)
        echo "backup is running"
        ;;
        2|copy)
        echo "copy is running"
        ;;
        3|quit)
        exit 10
        ;;
        *)
        echo error
esac

shell脚本练手_第17张图片
shell脚本练手_第18张图片

你可能感兴趣的:(shell)