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

SHELL训练营--day23_shell练习56-60

2019-01-17 20:39 549 查看
#文件增加内容
#!/bin/bash

n=0

cat 1.txt |while read line
do
n=[$n+1]
if [ $n -eq 5 ]
then
echo $line
echo -e "#This is a test file.\n#Test insert line into this file."
else
echo $line
fi
done

#备份/etc目录
#!/bin/bash
d1=`date +%d`
d2=`date +%y%m%d`

if [ $d1 == "01" ]
then
cd /etc/
tar czf /root/bak/$d2_etc.tar.gz ./
fi

#找出重复的单词
#!/bin/bash
for w in `sed 's/[^a-zA-Z]/ /g' $1`
do
echo $w
done|sort|uniq -c |sort -nr |head

#比较两数大小
#!/bin/bash
if [ $# -ne 2 ]
then
echo "请提供两个参数。"
exit
fi

if_number()
{
if echo $1|grep -q '^-'
then
nu=`echo $1|sed 's/^-//'`
else
nu=$1
fi
n=`echo $nu|sed 's/[0-9.]//g'`
if [ -n "$n" ]
then
echo "$1不是合法数字。"
exit
fi
if echo $nu|grep -q '^\.'
then
echo "$1不是合法数字。"
exit
fi
}

if_number $1
if_number $2

n1=`echo "$1>$2"|bc`
n2=`echo "$1-$2"|bc`
if [ $n1 -eq 1 ]
then
echo "$1 > $2"
elif [ $2 -eq 0 ]
then
echo "$1 = $2"
else
echo "$1 < $2"
fi
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  shell 训练营