shell demo 小例子
- 我起的名字是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"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。