1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
%s@^[[:space:]]\+[^[:space:]]@#&@g
原文件:
替换后的文件:
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
%s@^[[:space:]]\+@@g
原文件:
查找替换后的文件:
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
%s@^#[[:space:]]\+@@g
原文件
替换查找后的文件
4、为/tmp/grub.conf文件中前三行的行首加#号;
:1,3s@.*@#&@
原文件
替换后的文件
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
%s@\(enabled=0|gpgcheck=0\)=0@\1=1@g
原文件
替换查找后的文件:
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201504020202
* */4 * * * /bin/cp –r /etc /backup/etc-$(date +%Y%m%d)
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20150402
* * * * 2,4,6 /bin/cp -f /var/log/messages /backup/messages_logs/messages-$(date Y%m%d)
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
* */2 */1 * * /bin/grep "^S" /proc/meminfo >> /stats/memory.txt
9、工作日的工作时间内,每两小时执行一次echo "howdy"
* 9-18/2 * * 1-5 /bin/echo "howdy"
列出上述创建的4个周期性计划任务
[root@localhost backup]# crontab -e crontab: installing new crontab [root@localhost backup]# crontab -l * 9-18/2 * * 1-5 /bin/echo "howdy" * */2 */1 * * /bin/grep "^S" /proc/meminfo >> /stats/memory.txt * */4 * * * /bin/cp -r /etc/ /backup/etc-$(date +%Y%m%d) * * * * 2,4,6 /bin/cp -rf /var/log/messages /backup/messages_logs/messages-$(date +%Y%m%d)
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
11、在此目录创建100个空文件:file1-file100
脚本文件
#!/bin/bash # Author: Max Meng # Date: 2016/09/22 # Summary: # 10. Create a directory in /tmp: such as /tmp/testdir-20169010123030 # 11. Create 100 empty file: file1 - file100 # Create direcotry using date command dirname="test-$(date +%Y%m%d%H%M%S)" mkdir /tmp/$dirname # If directory created successfully, then trying to create file1 to file100 if [ -e /tmp/$dirname ]; then for i in {1..100}; do touch /tmp/$dirname/file$i done echo "Create file1 to file100 finished" else echo "create dirname failure" exit 1 fi
执行结果
[root@localhost week06]# ls /tmp/test-20160923061932/ file1 file14 file2 file25 file30 file36 file41 file47 file52 file58 file63 file69 file74 file8 file85 file90 file96 file10 file15 file20 file26 file31 file37 file42 file48 file53 file59 file64 file7 file75 file80 file86 file91 file97 file100 file16 file21 file27 file32 file38 file43 file49 file54 file6 file65 file70 file76 file81 file87 file92 file98 file11 file17 file22 file28 file33 file39 file44 file5 file55 file60 file66 file71 file77 file82 file88 file93 file99 file12 file18 file23 file29 file34 file4 file45 file50 file56 file61 file67 file72 file78 file83 file89 file94 file13 file19 file24 file3 file35 file40 file46 file51 file57 file62 file68 file73 file79 file84 file9 file95
12、显示/etc/passwd文件中位于第偶数行的用户的用户名;
脚本文件
#!/bin/bash # sed -n 2~2p /etc/passwd | cut -d: -f1
执行结果
[root@www week06]# bash sh14.sh bin adm sync halt operator ftp pegasus ods systemd-network polkitd apache colord unbound saslauth amandabackup geoclue rpc rtkit qemu rpcuser radvd mysql pulse tomcat gdm avahi sshd dovenull tcpdump user1 hbase max user22 fedora user20 user10 user12 user14 user16 user18
13、创建10用户user10-user19;密码同用户名;
14、在/tmp/创建10个空文件file10-file19;
15、把file10的属主和属组改为user10,依次类推。
脚本文件
#!/bin/bash # Author: Max Meng # Date: 2016/09/22 for i in {10..19}; do username=user$i if id $username >& /dev/null; then echo "user already exists" else # add user using $username useradd $username echo $username | passwd --stdin $username >& /dev/null echo "add user $username successfully, the password is same as username" filename=file$i # Create empty file using $filename touch /tmp/$filename # Change file owner and file group of $filename equavilant to $username chown $username:$username /tmp/$filename echo "Create $filename successfully" fi done
执行结果
[root@www week06]# bash sh13.sh add user user10 successfully, the password is same as username Create file10 successfully add user user11 successfully, the password is same as username Create file11 successfully add user user12 successfully, the password is same as username Create file12 successfully add user user13 successfully, the password is same as username Create file13 successfully add user user14 successfully, the password is same as username Create file14 successfully add user user15 successfully, the password is same as username Create file15 successfully add user user16 successfully, the password is same as username Create file16 successfully add user user17 successfully, the password is same as username Create file17 successfully add user user18 successfully, the password is same as username Create file18 successfully add user user19 successfully, the password is same as username Create file19 successfully [root@www week06]# cat /etc/passwd | grep user1 user10:x:1024:1024::/home/user10:/bin/bash user11:x:1025:1025::/home/user11:/bin/bash user12:x:1026:1026::/home/user12:/bin/bash user13:x:1027:1027::/home/user13:/bin/bash user14:x:1028:1028::/home/user14:/bin/bash user15:x:1029:1029::/home/user15:/bin/bash user16:x:1030:1030::/home/user16:/bin/bash user17:x:1031:1031::/home/user17:/bin/bash user18:x:1032:1032::/home/user18:/bin/bash user19:x:1033:1033::/home/user19:/bin/bash [root@www week06]# ll /tmp | grep file1 -rw-r--r--. 1 user10 user10 0 Sep 23 09:30 file10 -rw-r--r--. 1 user11 user11 0 Sep 23 09:30 file11 -rw-r--r--. 1 user12 user12 0 Sep 23 09:30 file12 -rw-r--r--. 1 user13 user13 0 Sep 23 09:30 file13 -rw-r--r--. 1 user14 user14 0 Sep 23 09:30 file14 -rw-r--r--. 1 user15 user15 0 Sep 23 09:30 file15 -rw-r--r--. 1 user16 user16 0 Sep 23 09:30 file16 -rw-r--r--. 1 user17 user17 0 Sep 23 09:30 file17 -rw-r--r--. 1 user18 user18 0 Sep 23 09:30 file18 -rw-r--r--. 1 user19 user19 0 Sep 23 09:30 file19
原创文章,作者:mxb93,如若转载,请注明出处:http://www.178linux.com/48458