Shell基础 以及生产案例

Linux系统——shell脚本编程基础介绍

什么是shell?

它是一个命令解释器,在linux/unix操作系统的最外层,负责直接与用户对话,把用户的输入解释给操作系统,并处理各种操作输出的结果,输出到屏幕返回给用户,可以是交互式与非交互式的方式进行会话。

shell脚本介绍:

当命令或语句不在命令行执行,而是通过一个程序文件执行时,这类文件就被称为shell脚本或shell程序文件,类似于WIN系统下面的批处理程序

shell脚本语言的种类

Bourneshell(包括shksh bash)

Cshell (csh)

在redhat和centos系统中,最常用的是bash

脚本规范

1、第一行一般都是指定由哪个程序来执行解释脚本中的命令内容
2、加上作者信息与版本信息和功能
3、脚本以.sh为扩展名
4、脚本注释
5、良好的书写习惯

脚本执行

1、bash -n name.sh        //检查否语法错误
2、bash -x name.sh        //逐句检查语法错误
3、bash name.sh           //执行脚本

生产案例

在这里插入图片描述

1、
[11:36:21 root@centos8 script :cat systeminfo.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-24
#FileName:		systeminfo.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
COLOR="\033[1;31m"
END="\033[0m"
echo -e  "HOSTNAME:        $COLOR`hostname`$END"
echo -e  "IPADDR:          $COLOR`ifconfig|grep -Eo '([0-9]{3}\.){3}[0-9]{3}'|head -1`$END"
echo -e  "OS_VERSION:      $COLOR`cat /etc/redhat-release`$END"
echo -e  "KERNEL:          $COLOR`uname -r`$END"
echo -e  "CPU:            $COLOR`lscpu|grep 'Model name'|tr -s ' '|cut -d: -f2`$END"
echo -e  "MEMORY:          $COLOR`free -h|grep 'Mem'|tr -s ' ' %|cut -d% -f2`$END"
echo -e  "DISK:            $COLOR`lsblk|grep 'sda\b'|tr -s ' ' %|cut -d% -f4`$END"
2、
[11:36:21 root@centos8 script :cat backup.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-24
#FileName:		backup.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
echo -e "\033[1;32mStarting backup...\033[0m"
sleep 2
cp -av /etc/ /data/etc`date +%F`/
echo -e "\033[1;32mBackup is finished\033[0m"
3、
[12:27:33 root@centos8 script :cat disk.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-24
#FileName:		disk.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
echo -e "\033[1;32m---MAXIMUM UTILIZATION OF DISK-------\033[0m"
echo -e `df| grep '^\/dev/'|tr -s ' ' %|cut -d% -f5|sort -nr|head -1`
4、
[12:35:25 root@centos8 script :cat link.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-24
#FileName:		link.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
echo -e `ss -nt|grep 'ESTAB'|tr -s ' ' :|cut -d: -f4|sort|uniq -c|sort -nr`

在这里插入图片描述

1、
[11:36:10 root@centos8 script :cat argsn.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		argsn.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
[[ $# -lt 1 ]] && echo "至少应该给一个参数!" || echo "文件中有 `grep '^[[:space:]]*$' $1|wc -l`个空白行"
2、
[11:38:50 root@centos8 script :cat  hostping.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		hostping.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
ping -c1 -w1 $1 & > /dev/null && echo "$1 is up" ||echo "$1 is down"
3、
[11:24:14 root@centos8 script :cat checkdisk.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		checkdisk.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
DISK=`df|grep -E '/dev/sd'|tr -s ' ' %|cut -d% -f5|sort -nr|head -1`
INODE=`df -i|grep -E '/dev/sd'|tr -s ' ' %|cut -d% -f5|sort -nr|head -1`
[ $DISK -gt 80 -o $INODE -gt 80 ] && echo "wall space will full"
4、
[11:53:10 root@centos8 script :cat per.sh 
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		per.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
[ ! -r $1 -a ! -w $1 ] && echo "$1是不可读并且不可写"
5、
[14:22:00 root@centos8 script :cat excute.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		excute.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
read -p "请输入一个文件: " FILE
[[ $FILE =~ ^.*\.sh$ ]] && (chmod a+x $FILE ;echo "$FILE is .sh") || echo "$FILE is not .sh"
6、
[14:34:02 root@centos8 script :cat nologin.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		nologin.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
#!/bin/bash
[ -f /etc/nologin ] && echo “nologin”|| (touch /etc/nologin;echo “nologin”)
[14:35:42 root@centos8 script :cat login.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		login.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
#!/bin/bash
[ -f /etc/nologin ] && ( rm -f /etc/nologin;echo “login”)||echo “login”

在这里插入图片描述

1、
[16:15:56 root@centos8 script :cat createuser.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		createuser.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#*******************************************************************

read -p "请输入一个用户" user
id $user &> /dev/null
if [[ $? -eq 0 ]] ;then
	echo "用户已经存在"
else
	useradd $user &> /dev/null
	echo "已添加$user用户"
fi
2、
[19:03:11 root@centos8 script :cat yesorno.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-26
#FileName:		yesorno.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
read -p "Please input (yes or no) :"  yn
if [ $yn == "Y" ] || [ $yn == "y" ];then
	echo "OK,coniue"
elif [ $yn == "N" ] || [ $yn == "n" ];then
	echo "Oh,interinput!"
else
	echo "You must choice y or n "
fi
3、
[10:46:53 root@centos8 script :cat filetype.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-31
#FileName:		filetype.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************
read -p "请输入一个文件路径:" dir
filetype=`ls -l /etc/passwd|cut -c 1`
case $filetype in
 -)
 	echo "Genera file"
	;;
 d)
 	echo "Dir file"
	;;
 l)
 	echo "Link file"
	;;
 *)
 	echo "Other file"
	;;
esac
4、
[10:52:31 root@centos8 script :cat checkint.sh
#!/bin/bash
#
#********************************************************************
#Author:		zhangxiurong
#Email: 	 	ZXIUR_98@163.com
#Date: 			2019-12-31
#FileName:		checkint.sh
#Description:		The test script
#Copyright (C): 	2019 All rights reserved
#********************************************************************

val=$1
int_re="^[0-9]+$"
if [[ $val =~ $int_re ]] ; then
    echo "yes"
    else
        echo "no"
	fi
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值