标签:系统 脚本初级
1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
[root@Client ~]# cp /etc/rc.d/rc.sysinit /tmp/ [root@Client ~]# sed ‘s@^[[:space:]]\+@#&@‘ /tmp/rc.sysinit
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
[root@Client ~]# cp /boot/grub/grub.conf /tmp/ [root@Client ~]# sed ‘s@^[[:space:]]\+@@‘ /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/mapper/vg_client-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(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_client-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_client/lv_root rd_NO_MD rd_LVM_LV=vg_client/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
[root@Client ~]# sed ‘s@^#[[:space:]]\+@@‘ /tmp/rc.sysinit
4、为/tmp/grub.conf文件中前三行的行首加#号;
[root@Client ~]# sed ‘1,3s@^@&#@‘ /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/mapper/vg_client-lv_root # initrd /initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashimage=(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_client-lv_root rd_NO_LUKS LANG=en_US.UTF-8 rd_LVM_LV=vg_client/lv_root rd_NO_MD rd_LVM_LV=vg_client/lv_swap SYSFONT=latarcyrheb-sun16 crashkernel=auto KEYBOARDTYPE=pc KEYTABLE=us rd_NO_DM rhgb quiet initrd /initramfs-2.6.32-642.el6.x86_64.img
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[root@Client ~]# sed ‘s@\(enabled=\)0@\11@‘ /etc/yum.repos.d/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
[root@Client ~]# mkdir backup [root@Client ~]# crontab -e 1 */4 * * * cp -a /etc /backup/etc-$(date +%Y%m%d%H%M) #定义一个大范围的时间时 其小范围不能直接为* 如果没特殊要求 应用一个任意值表示 下同
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
[root@Client ~]# mkdir -p /backup/messages_logs [root@Client ~]# crontab -e 1 2 * * 2,4,6 cp -a /var/log/messages /backup/messages_logs/messages-$(date +%Y%m%d)
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[root@Client ~]# mkdir /stats; touch memory.txt [root@Client ~]# crontab -e 1 */2 * * * grep -E "^S" /proc/meminfo >> /stats/memory.txt
9、工作日的工作时间内,每两小时执行一次echo "howdy"
[root@Client ~]# crontab -e 1 9-17/2 * * 1-5 echo "howdy"
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
[root@Client ~]# vim a #!/bin/bash mkdir /tmp/testdir-$(date +%Y%m%d%H%M) [root@Client ~]# chmod 744 a [root@Client ~]# /root/a [root@Client ~]# ll /tmp/ total 48 -rwxr--r--. 1 root root 0 Aug 28 12:58 a -rw-r--r--. 1 root root 0 Aug 28 12:59 b-2016-08-28-12-59 -rw-------. 1 root root 825 Aug 28 11:16 grub.conf drwx------. 2 root root 4096 Aug 9 16:33 keyring-7XOAs1 drwx------. 2 gdm gdm 4096 Aug 28 09:55 orbit-gdm drwx------. 2 root root 4096 Aug 9 16:33 pulse-ehWlzteVGDf6 drwx------. 2 gdm gdm 4096 Aug 28 09:55 pulse-ImeEgkRzS0Oj drwx------. 2 mu mu 4096 Jul 31 19:34 pulse-Kh1VMoAD9UBl -rwxr-xr-x. 1 root root 20199 Aug 28 10:56 rc.sysinit drwxr-xr-x. 2 root root 4096 Aug 28 13:37 testdir-201608281337
11、在此目录创建100个空文件:file1-file100
[root@Client ~]# cd /tmp/testdir-201608281337/ [root@Client testdir-201608281337]# vim b #!/bin/bash touch file{1..100} [root@Client testdir-201608281337]# chmod 744 b [root@Client testdir-201608281337]# ./b
12、显示/etc/passwd文件中位于第偶数行的用户的用户名;
[root@Client ~]# vim c #!/bin/bash sed -n ‘n;p‘ /etc/passwd | cut -d: -f1 [root@Client ~]# chmod 744 c [root@Client ~]# ./c bin adm sync halt uucp games ftp dbus rpc avahi-autoipd abrt nfsnobody ntp saslauth gdm sshd mu user2 dff_grt mageiaa openstack hadoop testbash nologin
13、创建10用户user10-user19;密码同用户名;
[root@Client ~]# vim d #!/bin/bash for((i=10;i<=19;i++)) do useradd user$i echo "user$i" | passwd --stdin user$i done [root@Client ~]# chmod 744 d [root@Client ~]# ./d Changing password for user user10. passwd: all authentication tokens updated successfully. Changing password for user user11. passwd: all authentication tokens updated successfully. Changing password for user user12. passwd: all authentication tokens updated successfully. Changing password for user user13. passwd: all authentication tokens updated successfully. Changing password for user user14. passwd: all authentication tokens updated successfully. Changing password for user user15. passwd: all authentication tokens updated successfully. Changing password for user user16. passwd: all authentication tokens updated successfully. Changing password for user user17. passwd: all authentication tokens updated successfully. Changing password for user user18. passwd: all authentication tokens updated successfully. Changing password for user user19. passwd: all authentication tokens updated successfully.
14、在/tmp/创建10个空文件file10-file19;
[root@Client ~]# cd /tmp/ [root@Client tmp]# vim e #!/bin/bash touch file{10..19} [root@Client tmp]# chmod 774 e [root@Client tmp]# ./e [root@Client tmp]# ll total 52 -rw-r--r--. 1 root root 0 Aug 28 12:58 a -rw-r--r--. 1 root root 0 Aug 28 12:59 b-2016-08-28-12-59 -rwxrwxr--. 1 root root 31 Aug 28 14:30 e -rw-r--r--. 1 root root 0 Aug 28 14:31 file10 -rw-r--r--. 1 root root 0 Aug 28 14:31 file11 -rw-r--r--. 1 root root 0 Aug 28 14:31 file12 -rw-r--r--. 1 root root 0 Aug 28 14:31 file13 -rw-r--r--. 1 root root 0 Aug 28 14:31 file14 -rw-r--r--. 1 root root 0 Aug 28 14:31 file15 -rw-r--r--. 1 root root 0 Aug 28 14:31 file16 -rw-r--r--. 1 root root 0 Aug 28 14:31 file17 -rw-r--r--. 1 root root 0 Aug 28 14:31 file18 -rw-r--r--. 1 root root 0 Aug 28 14:31 file19 -rw-------. 1 root root 825 Aug 28 11:16 grub.conf drwx------. 2 root root 4096 Aug 9 16:33 keyring-7XOAs1 drwx------. 2 gdm gdm 4096 Aug 28 09:55 orbit-gdm drwx------. 2 root root 4096 Aug 9 16:33 pulse-ehWlzteVGDf6 drwx------. 2 gdm gdm 4096 Aug 28 09:55 pulse-ImeEgkRzS0Oj drwx------. 2 mu mu 4096 Jul 31 19:34 pulse-Kh1VMoAD9UBl -rwxr-xr-x. 1 root root 20199 Aug 28 10:56 rc.sysinit drwxr-xr-x. 102 root root 4096 Aug 28 14:06 testdir-201608281337
15、把file10的属主和属组改为user10,依次类推。
[root@Client tmp]# vim f #!/bin/bash for((i=10;i<=19;i++)) do chown user$i:user$i file$i done [root@Client tmp]# chmod 774 f [root@Client tmp]# ./f [root@Client tmp]# ll total 56 -rw-r--r--. 1 root root 0 Aug 28 12:58 a -rw-r--r--. 1 root root 0 Aug 28 12:59 b-2016-08-28-12-59 -rwxrwxr--. 1 root root 31 Aug 28 14:30 e -rwxrwxr--. 1 root root 71 Aug 28 14:37 f -rw-r--r--. 1 user10 user10 0 Aug 28 14:31 file10 -rw-r--r--. 1 user11 user11 0 Aug 28 14:31 file11 -rw-r--r--. 1 user12 user12 0 Aug 28 14:31 file12 -rw-r--r--. 1 user13 user13 0 Aug 28 14:31 file13 -rw-r--r--. 1 user14 user14 0 Aug 28 14:31 file14 -rw-r--r--. 1 user15 user15 0 Aug 28 14:31 file15 -rw-r--r--. 1 user16 user16 0 Aug 28 14:31 file16 -rw-r--r--. 1 user17 user17 0 Aug 28 14:31 file17 -rw-r--r--. 1 user18 user18 0 Aug 28 14:31 file18 -rw-r--r--. 1 user19 user19 0 Aug 28 14:31 file19 -rw-------. 1 root root 825 Aug 28 11:16 grub.conf drwx------. 2 root root 4096 Aug 9 16:33 keyring-7XOAs1 drwx------. 2 gdm gdm 4096 Aug 28 09:55 orbit-gdm drwx------. 2 root root 4096 Aug 9 16:33 pulse-ehWlzteVGDf6 drwx------. 2 gdm gdm 4096 Aug 28 09:55 pulse-ImeEgkRzS0Oj drwx------. 2 mu mu 4096 Jul 31 19:34 pulse-Kh1VMoAD9UBl -rwxr-xr-x. 1 root root 20199 Aug 28 10:56 rc.sysinit drwxr-xr-x. 102 root root 4096 Aug 28 14:06 testdir-201608281337
本文出自 “果麦” 博客,转载请与作者联系!
标签:系统 脚本初级
原文地址:http://guomai.blog.51cto.com/8530387/1851421