最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Linux Basic 6:vim,sed,for语句

运维笔记admin61浏览0评论

Linux Basic 6:vim,sed,for语句

Linux Basic 6:vim,sed,for语句

第六周作业内容:

1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;

# cp /etc/rc.d/rc.sysinit /tmp
# vim /tmp/rc.sysinit
vim末行模式,输入
:%s/^[[:space:]]/#&/g

2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;

# cp /boot/grub/grub.conf /tmp
# sed 's@^[[:space:]]\+@@' /etc/grub.conf
# grub.conf generated by anaconda
#
# Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that
#          all kernel and initrd paths are relative to /boot/, eg.
#          root (hd0,0)
#          kernel /vmlinuz-version ro root=/dev/mapper/vg_mycentosl68-lv_root
#          initrd /initrd-[generic-]version.img
#boot=/dev/sda
default=0
timeout=5
splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz
hiddenmenu
title CentOS 6 (2.6.32-642.el6.x86_64)
root (hd0,0)
kernel /vmlinuz-2.6.32-642.el6.x86_64 ro root=/dev/mapper/vg_mycentosl68-lv_root rd_NO_LUKS rd_LVM_LV=vg_mycentosl68/lv_swap crashkernel=auto rd_NO_MD rd_LVM_LV=vg_mycentosl68/lv_root LANG=zh_CN.UTF-8  KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet
initrd /initramfs-2.6.32-642.el6.x86_64.img

3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符

# sed 's/^#[[:space:]]\+//' /tmp/rc.sysinit

4、为/tmp/grub.conf文件中前三行的行首加#号;

# vim /tmp/grub.conf
vim末行模式,输入
:1,+2s/^/#&/g
# sed -e "1,3s/^/#/" grub.conf
## grub.conf generated by anaconda
##
## Note that you do not have to rerun grub after making changes to this file
# NOTICE:  You have a /boot partition.  This means that

5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;

# vim /etc/yum.repos.d/CentOS-Media.repo
末行模式,输入
:/enabled=0/s/0/1/g
:/gpgcheck=0/s/0/1/g

6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202

# crontab -e
0 */4 * * * /bin/cp -r /etc /backup/etc-'date +%Y%m%d%H%M'/

7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830

# crontab -e
0 0 * * 2,4,6 /bin/cp /var/log/messages /backup/messages_logs/messages-$(date '+%Y%m%d')

8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中

# mkdir /stats && touch /stats/memory.txt
# crontab -e
0 */2 * * * /bin/grep '^S'/proc/meminfo >> /stats/memory.txt

9、工作日的工作时间内,每两小时执行一次echo "howdy"

# crontab -e
0 9-18/2 * * 1-5 /bin/echo "howdy"

脚本编程练习

10、创建目录/tmp/testdir-当前日期时间;

# vim /temp/testmkdir.sh
#!/bin/bash
#
tt=`date '+%Y-%m-%d_%H:%M'` && mkdir "/tmp/testdir-$tt" && ls -d /tmp/testdir*
# bash /temp/testmkdir.sh
/tmp/testdir-2016-09-02_19:38:29

11、在此目录创建100个空文件:file1-file100

# vim /tmp/test11.sh
#!/bin/bash
#
for n in {1..100};do[ -d "/tmp/file$n" ] &> /dev/null || mkdir "/tmp/file$n"
done
# bash /tmp/test11.sh
# ls -d /tmp/file*
/tmp/file1    /tmp/file18  /tmp/file27  /tmp/file36  /tmp/file45  /tmp/file54 /tmp/file63  /tmp/file72  /tmp/file81  /tmp/file90
/tmp/file10   /tmp/file19  /tmp/file28  /tmp/file37  /tmp/file46  /tmp/file55 /tmp/file64  /tmp/file73  /tmp/file82  /tmp/file91
/tmp/file100  /tmp/file2   /tmp/file29  /tmp/file38  /tmp/file47  /tmp/file56  /tmp/file65  /tmp/file74  /tmp/file83  /tmp/file92
/tmp/file11   /tmp/file20  /tmp/file3   /tmp/file39  /tmp/file48  /tmp/file57/tmp/file66  /tmp/file75  /tmp/file84  /tmp/file93
/tmp/file12   /tmp/file21  /tmp/file30  /tmp/file4   /tmp/file49  /tmp/file58 /tmp/file67  /tmp/file76  /tmp/file85  /tmp/file94
/tmp/file13   /tmp/file22  /tmp/file31  /tmp/file40  /tmp/file5   /tmp/file59 /tmp/file68  /tmp/file77  /tmp/file86  /tmp/file95
/tmp/file14   /tmp/file23  /tmp/file32  /tmp/file41  /tmp/file50  /tmp/file6/tmp/file69  /tmp/file78  /tmp/file87  /tmp/file96
/tmp/file15   /tmp/file24  /tmp/file33  /tmp/file42  /tmp/file51  /tmp/file60/tmp/file7   /tmp/file79  /tmp/file88  /tmp/file97
/tmp/file16   /tmp/file25  /tmp/file34  /tmp/file43  /tmp/file52  /tmp/file61/tmp/file70  /tmp/file8   /tmp/file89  /tmp/file98
/tmp/file17   /tmp/file26  /tmp/file35  /tmp/file44  /tmp/file53  /tmp/file62/tmp/file71  /tmp/file80  /tmp/file9   /tmp/file99

12、显示/etc/passw d文件中位于第偶数行的用户的用户名;

# vim /tmp/test12.sh
#!/bin/bash
#
sed -n 'p;n' /etc/passwd | cut -d: -f1
# bash !$
root
daemon
lp
shutdown
mail
operator
gopher
nobody
usbmuxd
rtkit
vcsa
rpcuser
haldaemon
apache
postfix
sshd
admin
slackware
hadoop
abc
asdd
gentoo
testbash
nologin
user1
user98
user11
user13
user15
user17
user19

13、创建10用户user10-user19;密码同用户名;

# vim /tmp/test13.sh
#!/bin/bash
#
for x in {10..19};doif ! id user$x &> /dev/null;thenuseradd user$x   fiecho "user$x" | passwd --stdin user$x &> /dev/null
done
# bash /tmp/test13.sh
# grep "^user1[0-9]" /etc/passwd
user1:x:3016:3018::/home/user1:/bin/bash
user90:x:3017:3030::/home/user90:/bin/bash
user98:x:3018:3031::/home/user98:/bin/bash
user10:x:3019:3020::/home/user10:/bin/bash
user11:x:3020:3021::/home/user11:/bin/bash
user12:x:3021:3022::/home/user12:/bin/bash
user13:x:3022:3032::/home/user13:/bin/bash
user14:x:3023:3023::/home/user14:/bin/bash
user15:x:3024:3024::/home/user15:/bin/bash
user16:x:3025:3025::/home/user16:/bin/bash
user17:x:3026:3026::/home/user17:/bin/bash
user18:x:3027:3027::/home/user18:/bin/bash
user19:x:3028:3028::/home/user19:/bin/bash
# grep "^user1[0-9]" /etc/shadow |cut -d: -f1,2
user10:$6$pTZJNXMe$K8/wlDIBuHlXDmMZV89FWCNXCb3lpgbDqNYeZ19xReO3MwVaCxVEUCsojbSEzOqpbkTh1wUZQ39Yy$
user11:$6$t/gEqH.L$4g2MczV22cWfVD03z7rV7g8dmk/u8DKODAU5XmOVYwDMGFypo3zN8xHzJjcaE0oxdCayi/9EKikgF$
user12:$6$apvOHbyV$7qWYd7R.KJ4zRyNUEjcLFzrECKsdEiWVP1PaqzdBkoMdPQiwoT2u1mRq8KPYIN4xDvDTpJiz/Efyl$
user13:$6$kv8uPAa9$DGvK5hhp4xVr.nPVTI1mAMzYRvoEwxXef1HLaStQnshq9x3jUrBJTgLLTbOCjy2Zxa43XxgpWebwc$
user14:$6$1R4cY8Be$mKfSf3aFBHIh2W4JgzjT5Iio2Ge2Y00fv5/oEkx/6QwO/P95fhKP70RxRiQil9/bKCSI1gH7YooCX$
user15:$6$B0uStd5w$Br4EjVrpLImx.Emv4Y8IH.vg.uf9.t4tzNaiQJYoHSQJvSaFpZ/ygncON4qXK8U2wp/rhElnlh8j5$
user16:$6$WF3ODFsZ$rk6.dR79P/wCPwq45k1C.m5WJrSosrcytL50eh.1uCXl..joir/zVwY5BkyeV6Iujonhw7LW0Oh34$
user17:$6$QMSzh8Wr$qLTNqaK/viIytlZ6OgdhFgdYzVkZCHgcviFwt8w6ILnCNchwgqEO8G/lj.P462yfGCVnU2OxcBXjy$
user18:$6$qgjPui4i$1qrveMxEBo3oLXWGWuWSnUUMp0S0IVGdDWwsXWUdI9lvnGvyD6vKXa9051qXyuE0CaRcUtSnhbVsp$
user19:$6$EhlB9BFU$BpOUPP8ghCsP.i8nLhSQEPzTxtgE2p/s9FgpeplS6G9myVjbJhpdWHcCPJTvr63iQkZo10l5lYEpi$

14、在/tmp/创建10个空文件file10-file19;

# vim /tmp/test14.sh
#!/bin/bash
#
for createfile in {10..19};doif [ ! -e "/tmp/file$createfile" ];thentouch "/tmp/file$createfile"fi
done
ls -l /tmp/file1[0-9]
# bash /tmp/test14.sh
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file10
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file11
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file12
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file13
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file14
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file15
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file16
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file17
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file18
-rw-r--r--. 1 root root 0 9月   2 16:58 /tmp/file19

15、把file10的属主和属组改为user10,依次类推。

# vim /tmp/test15.sh
#!/bin/bash
#
for i in {10..19};doif ! grep "^user$i" /etc/group >& /dev/null;thengroupadd "user$i"fiif ! id "user$i" &> /dev/null;thenuseradd -g user$i user$ifichown user$i:user$i "/tmp/file$i"
done
[root@myCentOSl68 ~]# bash /tmp/test15.sh | ls -l /tmp/file1[0-9]
-rw-r--r--. 1 user10 user10 0 9月   2 16:58 /tmp/file10
-rw-r--r--. 1 user11 user11 0 9月   2 16:58 /tmp/file11
-rw-r--r--. 1 user12 user12 0 9月   2 16:58 /tmp/file12
-rw-r--r--. 1 user13 user13 0 9月   2 16:58 /tmp/file13
-rw-r--r--. 1 user14 user14 0 9月   2 16:58 /tmp/file14
-rw-r--r--. 1 user15 user15 0 9月   2 16:58 /tmp/file15
-rw-r--r--. 1 user16 user16 0 9月   2 16:58 /tmp/file16
-rw-r--r--. 1 user17 user17 0 9月   2 16:58 /tmp/file17
-rw-r--r--. 1 user18 user18 0 9月   2 16:58 /tmp/file18
-rw-r--r--. 1 user19 user19 0 9月   2 16:58 /tmp/file19


转载于:

发布评论

评论列表(0)

  1. 暂无评论