sed和for循环课后练习

前记:授课老师讲如果脚本不写个5000行,根本连门都入不了,如果写了5000行,可能是刚对脚本有了那么点意思,所以路漫漫兮.....

删除centos7系统/etc/grub2.cfg文件中所有以空白开头的行行首的空白字符
sed -ri.bak 's/^( )+(.*)/\2/g' grub2.cfg
删除/etc/fstab文件中所有以#开头,后面至少跟一个空白字符的行的行首的#和空白字符
cat fstab |sed -nr 's/(^# )+(.*)/\2/p'
在centos6系统/root/install.log每一行行首增加#号
sed -rn 's/(^.*)/#\1/p' fstab
在/etc/fstab文件中不以#开头的行的行首增加#号
sed -rn 's/(^.*)/#\1/p' fstab
处理/etc/fstab路径,使用sed命令取出其目录名和基名
echo /date/fstab |sed -rn 's@(.*/)(.*)$@\1@p'
echo /date/fstab |sed -rn 's@(.*/)(.*)$@\2@p'
利用sed 取出ifconfig命令中本机的IPv4地址
ifconfig | sed -nr '2s/^[^0-9]+([0-9.]+).*/\1/p'
统计centos安装光盘中Package目录下的所有rpm文件的以.分隔倒数第二个字段的重复次数
cd /mnt/AppStream/Packages 
ll |sed -rn 's/.*\.(.*)\.rpm$/\1/p'|sort |uniq -c
    895 i686
   1953 noarch
   2478 x86_64
统计/etc/init.d/functions文件中每个单词的出现次数,并排序(用grep和sed两种方法分别实现)
egrep -o [[:alpha:]]+ functions |sort |uniq -c
sed -r 's/[^[:alpha:]]+/\n/g' functions |sort|uniq -c

egrep -o [[:alpha:]]+ functions |sort |uniq -c|wc -l
452
sed -r 's/[^[:alpha:]]+/\n/g' functions |sort|uniq -c|wc -l
453
将文本文件的n和n+1行合并为一行,n为奇数行
seq 10 | xargs -n2
seq 10|sed 'N;s/\n/ /'
for 循环
#判断/var/目录下所有文件的类型
DIR=/var
cd $DIR
for  Type in `ls` ;do
    if [ -d $Type ];then
        file $Type 
    elif [ -f $Type ];then
        file $Type
    elif [ -L $Type ];then  #除了-h和-L之外,所有与文件相关的测试都取消引用符号链接,此处的文件测试使用file来更好
        file $Type    
    elif [ -b $Type ];then
        file $Type
    elif [ -c $Type ];then
        file $Type 
    elif [ -p $Type ];then
        file $Type 
    elif [ -s $Type ];then
        file $Type
    else 
        echo "$Type is a other file"
    fi

done
添加10个用户user1-user10,密码为8位随机字符
⏱ 22:52:44-root:/date/sh# bash user_add.sh 
 user1 is created, passwd is 704d9e61 
 user2 is created, passwd is 736b2dbb 
 user3 is created, passwd is 90a6a868 
 user4 is created, passwd is d0fc5917 
 user5 is created, passwd is 2fd178e1 
 user6 is created, passwd is cb19a26a 
 user7 is created, passwd is b72e289a 
 user8 is created, passwd is e62ea2d5 
 user9 is created, passwd is dd18d707 
 user10 is created, passwd is 778a6f03 
 
⏱ 22:53:00-root:/date/sh# cat user_add.sh 
mima=`cat /proc/sys/kernel/random/uuid | cut -f1 -d "-"`
for i in user{1..10};do
    useradd $i
        for mima in `cat /proc/sys/kernel/random/uuid | cut -f1 -d "-"`;do
            echo "$mima" |passwd $i --stdin &> /dev/null
        done
    echo " $i is created, passwd is $mima " 
done

#另一种写法,一点点区别,但是加了用户存在是否的判断,我觉得这个更严谨一些
⏱ 00:43:08-root:/date/sh# bash user_add.sh 
user1 is created, passwd is 59bf4f20 
user2 is created, passwd is d52d9235 
user3 is created, passwd is bbc375cb 
user4 is created, passwd is dc4e82d9 
user5 is created, passwd is 2fce17c3 
user6 is created, passwd is 967ddc89 
user7 is created, passwd is 73075dba 
user8 is created, passwd is 50820dc8 
user9 is created, passwd is f0e845ce 
user10 is created, passwd is 11ed236b 

⏱ 00:43:17-root:/date/sh# cat user_add.sh 
#!/bin/bash
mima=`cat /proc/sys/kernel/random/uuid | cut -f1 -d "-"`
for i in user{1..10};do
    if getent passwd $i &>/dev/null ;then    
        echo "User existing"
    else 
        useradd $i
    fi
        for mima in `cat /proc/sys/kernel/random/uuid | cut -f1 -d "-"`;do
            echo "$mima" |passwd $i --stdin &> /dev/null && echo "$i is created, passwd is $mima "
        done    
done


#for j in user{1..10};do userdel -r $j; done


/etc/rc.d/rc3.d目录下分别有多个以K开头和以S开头的文件;分别读取每个文件,以K开头的输出为文件加stop,以S开头的输出为文件名加start,如K34filename stop S66filename start
⏱ 00:17:14-root:/date/test# cat file_modification.sh 
#!/bin/bash
cd /date/test
for i in * ;do
    type=`echo $i | cut -c 1`   #cut 取第一个字符,然后与变量i比较
        if [ "$type" = "K" ];then   #条件为真时执行
            echo "$i stop"
        elif [ "$type" = "S" ];then #上个条件为假时执行
            echo "$i start"
        else    #以上条件均为假时执行
            echo "$i unkown"
        fi
done
⏱ 00:17:12-root:/date/test# bash file_modification.sh 
file_modification.sh unkown
K1 stop
K2 stop
K3 stop
K4 stop
K5 stop
S1 start
S2 start
S3 start
S4 start
S5 start

重点总结:该题使用到了字符串测试,特别注意如果变量在字符串测试中,注意要加""号
test和 [ ]用法
-z STRING 字符串是否为空,没定义或空为真,不空为假,
-n STRING 字符串是否不空,不空为真,空为假 
   STRING   同上
STRING1 = STRING2 是否等于,注意 = 前后有空格
STRING1 != STRING2 是否不等于
> ascii码是否大于ascii码
< 是否小于
[[]] 用法,建议,当使用正则表达式或通配符使用,一般情况使用 [ ]
== 左侧字符串是否和右侧的PATTERN相同
 注意:此表达式用于[[ ]]中,PATTERN为通配符
=~ 左侧字符串是否能够被右侧的正则表达式的PATTERN所匹配
 注意: 此表达式用于[[ ]]中;扩展的正则表达式

你可能感兴趣的:(sed和for循环课后练习)