shell demo analyse------ 2

前端之家收集整理的这篇文章主要介绍了shell demo analyse------ 2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

shell demo 小例子


这些shell小程序都是自己上网找的一些和自己平时写的一点,和大家分享一下


  • 我起的名字是createShell,可以加到/bin路径下,以后在写shell脚本时直接使用可以生成一些注释信息
#usage: $0 shellFileName
#!/bin/bash 
#生成文件名,如果没有传入参数就以当前时间为名称
if [ -z "${1}" ];then
    mtime="`date +%T| sed 's/\:/\-/g'`"
    newfile="${mtime}.sh"
else
    newfile="${1}.sh"
fi

#判断该文件是否存在,当不存在就生成文件
if ! grep "^#!" ${newfile} &> /dev/null;then
cat  >> ${newfile} <<EOF
#!/bin/bash
#Author: leo
#Date: `date +"%F %T"` 
#Description:

EOF
fi
vim +6 ${newfile}
  • 得到当前局域网内的电脑开机状态(c类网址)
#!/bin/bash
#Author: leo
#Date: 2017-06-05 22:17:20
#Description: ping ip from 1 to 255
read -p "C NETWORK: " MYNET
PINGNET=`echo ${MYNET} | sed 's/\([0-9.]*\)\.[0-9]*/\1/g' `
let I=1
while [ ${I} -le 254 ]
do
    ping -c1 -W1 ${PINGNET}.${I} &> /dev/null
    [ $? -eq 0 ] && echo "${PINGNET}.${I} online :)" || echo "${PINGNET}.${I} offline :("
let I++
done
  • 打印出输入多个文件中空行的数目
#usage: $0 $1...$n 
#!/bin/bash
#Author: leo
#Date: 2017-06-06 17:39:46
#Description: print blank line count
for i in "$@"
do
    [ -z ${i} ] && echo "${i} is zero " && continue
    COUNT=$(cat ${i} | grep -i "^$" | wc -l)  #统计空行数目 
    # “^$” 正则表达式代表空字符串
    echo "${i} 空格行为 ${COUNT}"
done
#!/bin/bash
#Author: leo
#Date: 2017-06-06 17:46:42
#Description: chmod a+x *.sh

for i in `ls`
do
    [ -f ${i} ] && [ ${i}=~.*\.sh ] && chmod a+x ${i} && echo " ${i} add author ok" || echo "file error"
done
#!/bin/bash
#Author: leo
#Date: 2017-07-06 02:25:49
#Description: move all file of current dir to a dir

#create dir
read -p "please input save dir : " saveDir
mkdir ${saveDir}

#move file to dir
for i in `ls`
do
        [ -f ${i} ] && `mv ${i} ./${saveDir}/`
done

#if dir is null,delete and warn
let count=$(ls ./${saveDir}/ | wc -l)
if [ ${count} -eq 0 ];then
        echo "no file to move"  && `rm -rf ${saveDir}`
fi
  • 打印一些信息
#!/bin/sh
#fun: some information print
clear
echo -e "\033[31m---------------------------1.date-----------------------------------"
date
echo -e "\033[31m---------------------------2.updatime-------------------------------"
uptime
echo -e "\033[31m---------------------------3.Last Logins(5)--------------------------"
last -ai | head -5
echo -e "\033[31m---------------------------4.disk------------------------------------"
#df -h 磁盘使用情况
df -h | grep "[\/]dev[\/]" | xargs | awk '{print "Free/total: " $4"/"$2" "$1}'
echo -e "\033[31m---------------------------5.memory----------------------------------"
#free -mh 内存使用情况
free -mh | grep "Mem" | xargs | awk '{ print "Free/Total: " $4 "/" $2 }'
echo -e "\033[31m---------------------------6.most expensive Processes----------------"
#top进行运行情况
top -b | head -10 | grep "^[\ ]"
echo -e "\033[31m---------------------------7.vmstat-----------------------------------"
#vmstat可以展现给定时间间隔的服务器的状态值,包括服务器的cpu使用率,内存使用,虚拟内存交换情况,IO读写情况
vmstat 2 5 | grep "^[\ ]"
echo -e "\033[39m"
原文链接:https://www.f2er.com/bash/391504.html

猜你在找的Bash相关文章