(1)shell基本知识点(必知)
if [ 1 -ne 1 ];then
…
fi
-eq:等于
-ne:不等于
-le:小于等于
-ge:大于等于
-lt:小于
-gt:大于
(2)普通输出信息重定向到文件
1.非追加模式
例子1:ps -ef | grep redis > redis.log
例子2:echo “hello world!” > redis.log
2.追加模式
例子1:ps -ef | grep redis >> redis.log
例子2:echo “hello world!” >> redis.log
(3)字符串处理
echo ${#name} #长度
echo ${name:1} #截断 {开始位置}
echo ${name:1:4} #截断 {开始位置,长度}
echo ${name//a/b} #将变量内所有的a替换成b(多次替换)
echo ${name/%a/b} #如果从右往左第一个字母为a则替换成b
echo ${name/#a/b} #如果从左往右第一个字母为a则替换成b
echo ${name/a/b} #将变量中从左到右的第一个a替换为b
一。常用命令:
echo 输出内容
read 输入内容
$ 的使用
#!/bin/bash
#2018-09-04 baron
#github: https://github.com/MyBaron/Shell_practice
# 练习一:
# echo 输出内容
# read 读取输入
# $ 的使用
#--------------------------------------------
# $ 是代表变量替换
# 输出PATH变量
echo $PATH
echo '\n Hello World! baron \a \n'
# 定义一个变量
name='helloName'
# '' 和"" 的区别就是 "" 会识别$符号
echo "输出name变量的值:${name}"
echo '单引号是不会识别$的:${name}'
# read 输入 输入的值赋值到fistname这个变量中 -p 是提示
read -p "please enter your firstname " firstname
read -p "and then please enter your lastname " lastname
echo "just a minute..........."
# 引用变量 输出自己输入的内容
echo "your name is" ${firstname}${lastname}
# $() 是使用命令,这里是使用了data命令输出日志
echo "Hellp ${firstname}${lastname} today is $(date +%Y%m%d) "
exit 0
二。功能:检查一下文件是否存在
练习内容:
echo 内容输出
read 内容输入
test 检查条件
命令使用
#!/bin/bash
#2018-09-14 baron
#github: https://github.com/MyBaron/Shell_practice
# 文件测试
# test 命令 -e 是如果文件或目录存在则为真
# && ,com1 && com2,只有当com1 为true时,才会执行com2
# || ,与&& 相反 只有com1 为false时,才会执行com2
echo "the bash is check one file is exist or not "
read -p "请输入你想检查的文件: " file
test -e ${file} && echo "${file}文件存在" || echo "${file}文件不存在"
# 数值比较
# -lt 是小于的意思
num1=100
num2=200
echo "num1=100,num2=200"
test ${num1} -lt ${num2} && echo "num1小于num2" || echo "num1大于num2"
# 字符串
str1="str"
str2="str"
echo "str1=str,str2=str"
test ${str1}=${str2} && echo "相等" || echo "不相等"
#这里的比较'str'="str"
exit 0
三。将文件拷贝到指定目录下
练习内容
输入参数
逻辑判断 If-elif-else
#!/bin/bash
#2018-09-015 baron
#github: https://github.com/MyBaron/Shell_practice
#将文件拷贝到指定目录下
#输入参数
# 执行该脚本命令 sh xx.sh hello.sh 文件名或文件夹名 path
# hello 为输入的参数
# ${n} 为第n个传入参数 从1开始 0是文件名称
# $# 为传入参数的个数
file=${1}
path=${2}
echo "输入的file为${file}"
echo "输入的path为${path}"
echo "输入的参数个数为$#"
# if-elif-else 逻辑"
# if判断语句在[] 里面
# 注意: [] 里面要有空格!!!!!!
# if 配合test语句使用 不需要[]
if [ "${file}" == "" ] && [ "${path}" == "" ]; then
echo "file和path都不能为空"
elif test -e ${file} ; then
echo "将${file}拷贝到${path}"
cp -r -i ${file} ${path}
else
echo "file 文件不存在"
fi
exit 0
四。功能:批量检查ip地址是否被占用
练习内容
while循环
for 循环
seq 指令 用于产生从某个数到另外一个数之间的所有整数
#!/bin/bash
#@author baron
#@data 2018-09-07
#@comtent check long-range server status
network="${1}"
# while循环 循环获取用户输入Ip字段
# 用法:
# 1. 条件与If一样[] 里是判断条件
# 2. do done 里面是判断逻辑,do为开始,done为结束
while [ "${network}" == "" ]
do
read -p "please input what you want to check network ,format is x.x.x " network
done
read -p "please input what you want to start the number " start
read -p "please input what you want to end the number " end
# for循环遍历次数
# seq指令是从某个数到另外一个数之间的所有整数
# 用法
# 1. for xx in xx
# do done 里面是判断逻辑,do为开始,done为结束
#
# 2. 也可以 for ((i=1; i<=100; i ++))
#
#
for sitenu in $(seq "${start}" "${end}" )
do
ping -c 1 "${network}.${sitenu}" &> /dev/null && result=0 || result=1
if [ "${result}" == 0 ]; then
echo "Server ${network}.${sitenu} is UP."
else
echo "Server ${network}.${sitenu} is DOWN."
fi
done
echo "ok,that is checking over"
exit 0
五。功能:检查端口情况
练习内容
输入流
if 判断条件
grep 信息筛选
#!/bin/bash
#@author baron
#@data 2018-09-06
#@content check any port is been taken
echo "Now , I will detect your Linux server's services!"
if [ "${1}" != "" ]; then
echo "The www,ftp,ssh,mail(stmp),and you want to check ${1} will be detected! "
else
echo "The www,ftp,ssh,mail(stmp) will be detected! "
fi
# 设定输出路径
testfile=checkPortResult.txt
# 将netstat结果输出到文件
# > 为输入重定向 例如 xx0 > xx1.txt 意思是将xx0的内容输入到xx1.txt中
netstat -tuln > ${testfile}
# 检测各个端口情况
# 利用grep 检索关键字
testing=$(grep ":80" ${testfile})
if [ "${testing}" != "" ]; then
echo "WWW is running in your system"
fi
testing=$(grep ":22" ${testfile})
if [ "${testing}" != "" ]; then
echo "SSH is running in your system"
fi
testing=$(grep ":21" ${testfile})
if [ "${testing}" != "" ]; then
echo "FTP is running in your system"
fi
testing=$(grep ":25" ${testfile})
if [ "${testing}" != "" ]; then
echo "Mail is running in your system"
fi
if [ "${1}" != "" ]; then
testing=$(grep ":${1}" ${testfile})
if [ "${testing}" != "" ]; then
echo "${1} port is running in your system"
fi
fi
echo "check is over , thank you"
exit 0
六。功能:检查CPU情况
效果图
#!/bin/bash
# baron
# 2019-01-18
# 分析cpu使用情况
# 利用vmstat 分析cpu使用情况
# 检测是否有vmstat命令
if ! which vmstat &> /dev/null ; then
echo "the sy is no vmstat commam"
exit 1
fi
# 创建临时文件
date=$(date "+%Y-%m-%d_%H:%M:%S")
file="cpu_stat_${date}.txt"
echo "正在收集cpu情况,请等候"
vmstat 2 3 >${file}
# 睡眠三秒
sleep 3s
set US
set SY
R=0
# 循环处理数据
for NR in $(seq 3 5)
do
US1=$(vmstat |awk 'NR==3{print $13}')
SY1=$(vmstat |awk 'NR==3{print $14}')
R1=$(vmstat |awk 'NR==3{print $1}')
if [ $R1 -gt $R ]; then
R=${R1}
fi
US=`expr ${US} + ${US1} `
SY=`expr ${SY} + ${SY1} `
done
# 删除临时文件
rm -r ${file}
US=`expr ${US} / 3 `
SY=`expr ${SY} / 3 `
echo "最大的进程数是:${R}"
echo "平均的CPU占用率是:`expr ${US} + ${SY}`%"
echo "用户平均CPU占用率:${US}%"
echo "系统平均CPU占用率:${SY}%"
echo "this is all"
exit 0
七。功能:检查CPU情况
使用free 命令获取内存情况
#!/bin/bash
# baron
# 2019-01-21
# 分析内存使用情况
# 利用free 分析内存使用情况
# 检测是否有free命令
if ! which free &> /dev/null ; then
echo "the sy is no free commam"
exit 1
fi
echo "正在检查内存使用情况"
TOTAL=$(free -m |awk '/Mem/{print $2}')
USE=$(free -m |awk '/Mem/{print $3}')
FREE=$(free -m |awk '/Mem/{print $4}')
CACHE=$(free -m |awk '/Mem/{print $6}')
echo -e "当前使用情况: \n内存总大小:${TOTAL}M \n内存使用量:${USE}M \n可用剩余:${FREE}M\n磁盘缓存大小:${CACHE}M"
exit 0
八。利用awk切割后,输出所有字符串
#!/bin/bash
# baorn
# 2019-01-26
# awk 将输出所有分割的字符串
# 将echo的以空格切割然后遍历输出
# NF是awk的参数,代表切割后个数
# print $i 是输出切割后的第几列 $0是整行
val=`echo "第一 第二 第三 第四"|awk '{for(i=1;i<=NF;i++){print $i}}'`
echo $val
- 1.确定某一进程是否存
ps -fe|grep processString |grep -v grep
if [ $? -ne 0 ]
then
echo "start process....."
else
echo "runing....."
fi
#processString为确定唯一进程的查询条件 此脚本用于确定某一进程是否存在
- 2.按行读取文件并输出
#!/bin/bash
bak=$IFS
if [ $# -ne 1 ];then
echo "Usage $0 filename"
exit
fi
if [ ! -f $1 ];then
echo "the $1 is not a file"
exit
fi
IFS=$'\n'
for i in `cat $1`
do
echo $i
done
IFS=$bak
#运行命令sh demo.sh 文件名
删除指定目录下特定的文件(例如:文件夹/suns下有 文件夹1 文件1.xml 。。。如果文件夹1里面的文件个数小于2 则删除文件夹1及文件1.xml)
#!/bin/bash
#定义变量指定文件夹
dir="/suns"
#循环读取文件夹/suns下的文件夹(排除xml文件)
for i in `ls $dir|grep -v "xml"`;do
#判断文件夹下的文件个数
count=`ls $dir/$i | wc -w`
#控制台输出文件夹及其文件夹下的文件个数
echo "目录:$dir/$i下的文件个数"$count
#如果文件夹内文件个数小于2
if [[ $count -lt 2 ]];then
#输出文件夹及目录
echo $dir/$i
#删除文件夹
rm -r $dir/$i
#删除xml文件
rm -f $dir/${i}.xml
fi
done
查看:https://www.jianshu.com/p/d4b29f701e19