tShell 程序设计

1. 设计一个shell程序,在每月第一天备份并压缩/etc目录的所有内容,存放在/root/bak目录里,且文件名为如下形式yymmdd_etc,yy为年,mm为月,dd为日。Shell程序fileback存放在/usr/bin目录下
1)首先编写备份文件的脚本fileback.sh
#!/bin/bash
if [ ! -e "/root/bak" ];   //首先查看/root/bak目录是否存在,如果不存在,则创建目录
then
mkdir /root/bak
fi
cd /root/bak    //将当前目录更改到/root/bak
YY=$(date +%Y)  //提取日期中的年月日
mm=$(date +%m)
DD=$(date +%d)
backupdir="$YY$mm$DD"_etc.tar.gz  //这里必须注意,在$YY$mm$DD上加引号,否则shell会将$DD_etc认为是一个变量,获取不到$DD的值
echo $backupdir
tar zcvf $backupdir /etc/ echo "fileback finished!"
2) 使用Cron表达式
创建包含cron表达式的task文件task.cron
0 0 1 * * /root/fileback.sh
使用一下命令启动定时任务
# crontab task.cron
PS:
使用以下命令查看和删除Cron表:
# crontab -l   //查看Cron表
0 1 * * * /root/fileback.sh
# crontab -r  //删除Cron表
 2. 设计一个Shell程序,在/userdata目录下建立50个目录,即user1~user50,并设置每个目录的权限,其中其他用户的权限为:读;文件所有者的权限为:读、写、执行;文件所有者所在组的权限为:读、执行。
#!/bin/bash
if [ ! -e "/userdata" ];
then
mkdir /userdata
fi
cd /userdata
for((i=1;i<=50;i++)){
mkdir user$i
chmod 754 user$i
}
3. 打印1-99之间的奇数到文件
 方法1:
# seq 1 2 99 >owk.txt //1代表第一个值,2代表增加的步长,99代表最后一个值
方法2:
# seq 100 | awk '{ if($0%2==1) print $0}' > owk.txt
4. 从a.log文件中提取包含"WARNING"或"FATAL", 同时不包含"IGNOR"的行, 然后提取以":"分割的第5个字段.
方法1:
# cat a.log | egrep "WARNING|FATAL" | grep -v "IGNOR" | cut -f5 -d ":"
方法2:
# cat a.log | egrep -i "WARNING|FATAL" | grep -v "IGNOR" | awk -F: '{print $5}'
 5. 有一个文件, 里面有二列, 第一列ip地址, 第二列是时间, 同一个ip可能出现多次, 但时间不同. 现要求写一脚本, 显示出现最多的ip top 10
实例数据:
192.168.1.2 13:10 
192.127.12.1 13.11
192.168.1.2 14:22
192.168.1.1 10:15
1.1.1.1 10:10
1.1.1.1 15:20
1.1.1.1 12:10
cut -f 1 -d " " ip.txt | sort | uniq -c | sort -nr | head -10 | awk '{print $2}'
 运行结果:
1.1.1.1
192.168.1.2
192.168.1.1
192.127.12.1
6. 在shell环境中,如何查看远程Linux系统运行了多少时间?
# ssh uadmin@karenwang "uptime" | awk '{print $3 $4}'
uadmin@karenwang's password: 
45days,
7. 处理一下文件内容,将域名取出并进行计数排数,如处理:
        http://www.baidu.com/index.html
        http://www.baidu.com/1.html
        http://www.baidu.com/2.html
        http://post.baidu.com/index.html
        http://mp3.baidu.com/index.html
        http://www.baidu.com/3.html
        http://post.baidu.com/2.html
得到如下结果:域名的出现次数,域名
              4     www.baidu.com
                2     post.baidu.com
                1     mp3.baidu.com
 方法1:
# cat domain.txt | awk -F/ '{print $3}' | sort -r | uniq -c | sort -nr
4 www.baidu.com 
2 post.baidu.com 
1 mp3.baidu.com
方法2:
# cat domain.txt | sed -e 's/http:\/\///' |sed -e 's/\/.*//' | sort -r | uniq -c | sort -nr
      4 www.baidu.com
      2 post.baidu.com
      1 mp3.baidu.com
 8. 按列合并两个文件,如
file1.txt
1
2
3
file2.txt
hello world c
2 a my
3 d e
合并完成后
1 hello world c
2 2 a my
3 3 d e
# paste file1.txt file2.txt
9. 检验回文字符串
#!/bin/bash
if [ $# -ne 2 ];
then
echo "Usage: $0 filename string_length"
exit -1;
fi
filename=$1
basepattern='/^\(.\)'
count=$(($2/2))
for((i=1;i<$count;i++))
do
basepattern=$basepattern'\(.\)'
done
if [ $(($2%2)) -ne 0 ];
then
basepattern=$basepattern'.'
fi
for((count;count>0;count--))
do
basepattern=$basepattern'\'"$count";
done
basepattern=$basepattern'$/p'
sed -n "$basepattern" $filename
 运行结果:
sh-4.1# cat huiwen.txt 
123321
sh-4.1# ./huiwen.sh huiwen.txt 6
123321
 10. 如果得到随机的字串,长度和字串中出现的字符表可定义并将字串倒序显示,如把0123456789作为基准的字串字符表,产生一个6位的字串642031,打印出的字符串为130246,可使用bash/perl/php/c任一种。
awk -v count=6 'BEGIN {srand(); str="0123456789";len=length(str); for(i=count;i>0; i--) {marry[i]=substr(str,int(rand()*len),1)}for(i=count;i>0;i--) printf( "%c",marry[i]);printf("\n");for(i=0;i<=count;i++)printf("%c",marry[i]);printf("\n")}'
115403
304511
 11. Write a bash script to calculate the frequency of each word in a text file words.txt. For simplicity sake, you may assume:
  • words.txt contains only lowercase characters and space ' ' characters.
  • Each word must consist of lowercase characters only.
  • Words are separated by one or more whitespace characters.
For example, assume that words.txt has the following content:
the day is sunny the the
the sunny is is
Your script should output the following, sorted by descending frequency:
the 4
is 3
sunny 2
day 1

方法1:
sh-4.1# egrep -o "\b[[:alpha:]]+\b" words.txt | awk '{count[$0]++} END{for(ind in count){print ind,count[ind]}}' | sort -nrk 2
the 4
is 3
sunny 2
day 1
方法2:
sh-4.1# cat words.txt | awk '{split($0,array," "); for(i in array){print $i}}' | sort | uniq -c | sort -nr | awk '{print $2,$1}'
the 4
is 3
sunny 2
day 1
 方法3:
sh-4.1# cat words.txt | tr -s ' ' '\n' | sort |uniq -c| sort -nrk 1 | awk -F" " '{print $2,$1}'
the 4
is 3
sunny 2
day 1
 
 


posted on 2015-06-11 13:52  karenwang  阅读(2405)  评论(0编辑  收藏  举报

导航