shell脚本练习题

统计词频

写一个 bash 脚本以统计一个文本文件 words.txt 中每个单词出现的频率。

为了简单起见,你可以假设:

  • words.txt只包括小写字母和 ' ' 。
  • 每个单词只由小写字母组成。
  • 单词间由一个或多个空格字符分隔。
  • 示例:

假设 words.txt 内容如下:

the day is sunny the the
the sunny is is
你的脚本应当输出(以词频降序排列):

the 4
is 3
sunny 2
day 1

cat t.txt | xargs -n1 | sort | uniq -c | awk '{print $2 " " $1}' | sort -r -k2

用xargs去除换行符的影响,sort和uniq -c来去除重复,最后逆序输出再排序

转置文件

给定一个文件 file.txt,转置它的内容。

你可以假设每行列数相同,并且每个字段由 ' ' 分隔.

示例:

假设 file.txt 文件内容如下:

name age
alice 21
ryan 30
应当输出:

name alice ryan
age 21 30

awk '{ for(i=1;i<=NF;i++){ if(NR == 1){ a[i] = $i; } else { a[i] = a[i]" "$i; } } }END{ for(i=1;i<=NF;i++){ print a[i]; } }' file.txt

利用awk变量输出

输出文件第十行

给定一个文本文件 file.txt,请只打印这个文件中的第十行。

示例:

假设 file.txt 有如下内容:

Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
你的脚本应当显示第十行:

Line 10

awk 'NR==10' file.txt
awk 'BEGIN{i=0} {i++;if(i==10)print} ' file.txt
awk '{if(NR==10)print}' file.txt
tail -n+10 file.txt|head -1 tail -n +10表示从第10行开始输出
sed -n 10p file.txt -n表示只输出匹配行,p表示Print

排序

用shell处理以下内容
1、按单词出现频率降序排序!
2、按字母出现频率降序排序!

zimu=`echo 'the squid project provides a number of resources toassist users design,implement and sup
port squid installations. Please browsethe documentation and support sections for more infomation'|sed -r 's#[.,]# #g'`
echo $zimu|xargs -n1|awk '{array[$1]++}END{for(j in array)print j,array[j]}'|sort -k2 -rn|column -t
echo $zimu|grep -o "[a-Z]"|sort|uniq -c |sort -nr |column -t
column -t用来将输出排列更整齐

匹配md5

已知下面的字符串是通过RANDOM随机数变量md5sum|cut-c 1-8截取后的结果,请破解这些字符串对应的md5sum前的RANDOM对应数字?

21029299

00205d1c

a3da1677

1f6d12dd

#!/bin/bash
a=(21029299
00205d1c
a3da1677
1f6d12dd)
for n in {0..32767}
do
        random=`echo $n|md5sum|cut -c1-8`
        for((i=0;i<=${#a[@]};i++))
        do
          if [ "$random" == "${a[i]}" ];then
          echo "$n" "${a[i]}"
        fi
        done
done

使用for循环在/oldboy目录下批量创建10个html文件

mkdir ./oldboy && cd ./oldboy && for i in `seq 10`;do a=`echo $RANDOM|md5sum|tr "0-9" "j-z"|cut -c1-10`;touch ${a}_oldboy.html;done

请用至少两种方法实现

将以上文件名中的oldboy全部改成oldgirl(用for循环实现),并且html改成大写。

rename的方法

cd /oldboy && rename _oldboy.html _oldgirl.HTML *

for循环

cd /oldboy
for i in `find /oldboy -type f -name  "*_oldboy.html"`
do
b=`echo $i|cut -c 1-19`
mv ${b}oldboy.html ${b}oldgirl.HTML
done
cd /oldboy
for i in `find /oldboy -type f -name  "*_oldboy.html"`
do
b=`echo $i |sed -r 's#(.*)oldboy.html#1#g'`
mv ${b}oldboy.html ${b}oldgirl.HTML
done

写一个脚本,实现判断10.0.0.0/24网络里,当前在线用户的IP有哪些

IP=10.0.0.
for j in {1..254}
do
ping -c 1 -w 2s $IP$j &>/dev/null

if [ $? -eq 0 ];then
        action "$IP$j " 
else
        action "$IP$j" /bin/false
fi
done

请用至少两种方法实现!循环、数组方式

for循环打印下面这句话中字母数不大于6的单词(昆仑万维面试题)。

I am oldboy teacher welcome to oldboy training class.

#!/bin/sh
for i in I am oldboy teacher welcome to oldboy training class.
do
        if [ ${#i} -lt 6 ];then
        echo $i
        fi
done
echo "I am oldboy teacher welcome to oldboy training class."|xargs -n1|awk '{if(length<6)print}'
echo "I am oldboy teacher welcome to oldboy training class."|awk '{for(i=1;i<=NF;i++)if(length($i)<6)print $i}'

计算从1加到100之和

seq 100|awk '{i=i+$1}END{print i}'

监控web站点目录下所有文件

是否被恶意篡改(文件内容被改了)
web站点目录(/var/html/www)

find /var/html/www/ -type f -name "*.thml"|xargs md5sum >/tmp/a.log
md5sum -c /tmp/a.log

以脚本传参以及read读入的方式比较2个整数大小

#!/bin/bash
read -p "Please input two Number: " -a Arr_str
echo ${Arr_str[*]} | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit 
if [ ${#Arr_str[*]} -eq 2 ];then
   if [ ${Arr_str[0]} -eq ${Arr_str[1]} ];then
        echo "${Arr_str[0]} == ${Arr_str[1]}"
   elif [ ${Arr_str[0]} -gt ${Arr_str[1]} ];then
        echo "${Arr_str[0]} > ${Arr_str[1]}"
   else
        echo "${Arr_str[0]} < ${Arr_str[1]}"
   fi
else
    echo "Please input two Number" 
fi
echo $1 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit
echo $2 | grep -E "^[0-9 ]{1,}$" &>/dev/null || exit
if [ $# -eq 2 ];then
   if [ $1 -eq $2 ];then
        echo "$1 == $2"
   elif [ $1 -gt $2 ];then
        echo "$1 > $2"
   else
        echo "$1 < $2"
   fi
else
    echo "Please input two Number" 
fi
原文地址:https://www.cnblogs.com/likaiming/p/11221830.html