您的位置:首页 > 运维架构 > Shell

SHELL训练营--day19_shell练习36-40

2019-01-11 23:50 776 查看
#一个 数字的行
#!/bin/bash
while read line
do
n=`echo $line |sed 's/[^0-9]//g'|wc -L`
if [ $n -eq 1 ]
then
echo $line
fi
done < 1.txt

#日志切割归档
#!/bin/bash
cd /data/logs
log=1.log
mv_log()
{
[ -f $1 ] && mv $1 $2
}

[ -f $log.5 ] && rm -f $log.5

for i in `seq 4 -1 1`
do
j=$[$i+1]
mv_log $log.$i $log.$j
done
mv_log $log $log.1

#查找在线IP
#!/bin/bash

for i in `seq 1 254`
do
if ping -c 2 -W 2 192.168.0.$i &>/dev/null
then
echo "192.168.0.$i 在线。"
else
echo "192.168.0.$i 不在线。"
fi
done

#检查脚本错误
#!/bin/bash
sh -n $1 2> /tmp/sh.err
if [ $? -ne 0 ]
then
cat /tmp/sh.err
read -p "请输入 q/Q 退出脚本。" c
if [ -z "$c" ]
then
vim $1
exit 0
fi

if [ $c == "q" ] || [ $c == "Q" ]
then
exit 0
else
vim $1
fi
else
echo "脚本 $1 没有语法错误。"
fi

#格式化数字
#!/bin/bash
n=`echo$1|wc -L`
for d in `echo $1|sed 's/./& /g'`
do
n2=$[$n%3]
if [ $n2 -eq 0 ]
then
echo -n " ,$d"
else
echo  -n  "$d"
fi
n=$[$n-1]
done |sed 's/^,//'
echo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell