1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
#1.sed 命令 #脚本文件 #/bin/bash cp /etc/rc.d/rc.sysinit /tmp #sed:文本行编辑器 #-i:对原文件直接进行编辑操作,默认sed是对模式空间的数据进行操作 #s/str1/str2/:将匹配到str1的内容替换成str2,&后向引用,即str1内容 sed -i ‘s/^[[:space:]]\+/#&/‘ /tmp/rc.sysinit #2.当然你也可以直接对文本进行vi编辑 #末行模式下使用s进行文本替换 :%s/^[[:space:]]\+/#&/g
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
#脚本文件 [[email protected] week6]# cat second.sh #/bin/bash cp /boot/grub/grub.conf /tmp #^:行首匹配 #[:space:]:空白字符 #\+:至少一次 sed -i ‘s/^[[:space:]]\+//‘ /tmp/grub.conf #查看执行结果 [[email protected] week6]# cat /tmp/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/sda2 # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(hd0,0)/grub/splash.xpm.gz hiddenmenu title CentOS (2.6.32-431.el6.x86_64) root (hd0,0) kernel /vmlinuz-2.6.32-431.el6.x86_64 ro root=UUID=33ddc759-09de-4936-845a-811c03ea3b08 rd_NO_LUKS rd_NO_LVM LANG=en_US.UTF-8 rd_NO_MD SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-431.el6.x86_64.img
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行的#和空白字符
sed ‘s/^#[[:space:]]\+//‘ /tmp/rc.sysinit
4、为/tmp/grub.conf文件中前三行的行首加#号;
#地址定界 #1.全文匹配 #2.单地址 #:指定某行,/pattern/:pattern匹配到的所有行 #3.地址范围 m,n;第m行到第n行 m,+n:第m行到m+n行 /part1/,/part2/:从part1匹配到的行到part2匹配到底行 #1,3:1~3行匹配 [[email protected] week6]# sed ‘1,3 s/^.*/#&/‘ /tmp/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/sda2 # initrd /initrd-[generic-]version.img
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[[email protected] week6]# cat forth.sh #/bin/bash #因为要修改配置文件,我把它复制到/tmp底下进行操作 cp /etc/yum.repos.d/CentOS-Media.repo /tmp sed -i ‘s/enabled=0/enabled=1/g‘ /tmp/CentOS-Media.repo sed -i ‘s/gpgcheck=0/gpgcheck=1/g‘ /tmp/CentOS-Media.repo #查看执行结果 [[email protected] week6]# cat /tmp/CentOS-Media.repo # CentOS-Media.repo # # This repo can be used with mounted DVD media, verify the mount point for # CentOS-6. You can use this repo and yum to install items directly off the # DVD ISO that we release. # # To use this repo, put in your DVD and use it with the other repos too: # yum --enablerepo=c6-media [command] # # or for ONLY the media repo, do this: # # yum --disablerepo=\* --enablerepo=c6-media [command] [c6-media] name=CentOS-$releasever - Media baseurl=file:///media/CentOS/ file:///media/cdrom/ file:///media/cdrecorder/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-6
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
#备份的方法比较多: #1.单独文件备份 cp复制文件即可 #2.tar :文件归档 #3.rsync :文件同步 #4.dd:克隆分区 #还有很多,包括软件工具啊 #*/4:每4个小时,如果可以被24整除可以直接这样写,不能整除则要写脚本实现 [[email protected] cron]# cat sixth.sh #/bin/bash tar -zcvf /backup/etc-$(date +‘%Y%m%d%H%M‘) /etc #进入编辑模式 [[email protected] week6]# crontab -e 0 */4 * * * /bin/bash /root/shell/cron/sixth.sh
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
[[email protected] cron]# cat seven.sh #/bin/bash cp /var/log/messages /backup/messages_logs/messages-$(date +‘%Y%m%d‘) [[email protected] week6]# crontab -e 0 0 * * 2,4,6 /bin/bash /root/shell/cron/seven.sh
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
#1.用grep实现,查出符合条件的行重定向生成到新文件中 #^:行首匹配 #>:重定向 0 */2 * * * grep "^S" /proc/meminfo > stats/memory.txt #2.用sed实现,w命令直接写到新文件 0 */2 * * * sed "/^S/w stats/memory.txt" /proc/meminfo
9、工作日的工作时间内,每两小时执行一次echo "howdy"
#工作时间:8~17点 #工作日:周一到周五:1~5 0 8-17/2 * * 1-5 /bin/echo "howdy"
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
11、在此目录创建100个空文件:file1-file100
#第10题和11题我就放一个脚本啦 [[email protected] ~]# cat tenth.sh #/bin/bash filename=testdir-$(date +‘%F-%H-%M-%S‘) mkdir /tmp/$filename if [ ! $? -eq 0 ];then echo "mkdir false" fi cd /tmp/$filename for i in $(seq 1 100);do touch file$i done #查看执行结果 [[email protected] ~]# ls /tmp/testdir-2016-09-11-08-04-48/ file1 file13 file18 file22 file27 file31 file36 file40 file45 file5 file54 file59 file63 file68 file72 file77 file81 file86 file90 file95 file10 file14 file19 file23 file28 file32 file37 file41 file46 file50 file55 file6 file64 file69 file73 file78 file82 file87 file91 file96 file100 file15 file2 file24 file29 file33 file38 file42 file47 file51 file56 file60 file65 file7 file74 file79 file83 file88 file92 file97 file11 file16 file20 file25 file3 file34 file39 file43 file48 file52 file57 file61 file66 file70 file75 file8 file84 file89 file93 file98 file12 file17 file21 file26 file30 file35 file4 file44 file49 file53 file58 file62 file67 file71 file76 file80 file85 file9 file94 file99
12、显示/etc/passwd文件中位于第偶数行的用户的用户名;
#1.sed:n:读取匹配行的下一行到模式空间中,p-打印 #匹配第一行后,取出下一行到模式空间然后打印,打印的就是下一行即第二行。再匹配指针往下走就是第三行了,依次类推打印的就是偶数行了 #cut: -d:指定分隔符 -f:取第几个字段 [[email protected] week6]# sed -n ‘n;p‘ /etc/passwd | cut -d: -f1 bin adm sync halt uucp games ftp dbus vcsa avahi-autoipd #2. 2~2,从第二行开始,步进是2,第二次匹配就是第4行,第三次往后取两行就是第6行,依次类推 #这里用awk进行分割,-F:分隔符 ,print:打印 [[email protected] week6]# sed -n ‘2~2 p‘ /etc/passwd | awk -F : ‘{print $1}‘ bin adm sync halt uucp games ftp dbus vcsa avahi-autoipd
13、创建10用户user10-user19;密码同用户名;
#for循环列表 #1.直接列出 10,11,12,13,15,16,17,18,19 #2.整数列表 {10..19} 或者$(seq [10 [1]] 19) #3.返回列表的命令 #4.glob #5.变量引用 [email protected],$* [[email protected] week6]# cat thirteen.sh #/bin/bash for i in $(seq 10 19);do username=user$i useradd $username #判断上个命令是否执行成功,不成功则退出 if [ ! $? -eq 0 ];then echo "useradd $username failed" exit 2 fi echo $username | passwd --stdin $username done
14、在/tmp/创建10个空文件file10-file19;
[[email protected] week6]# cat fourteen.sh #/bin/bash for i in $(seq 10 19);do touch /tmp/file$i done
15、把file10的属主和属组改为user10,依次类推。
#/bin/bash for i in $(seq 10 19);do username=user$i #chown [OPTION]... [OWNER][:[GROUP]] FILE:修改文件的属主和属组 chown $username:$username file$i done
时间: 2024-11-05 13:37:22