1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
[[email protected] rc.d]# cp /etc/rc.d/rc.sysinit /tmp [[email protected] rc.d]# ls /tmp rc.sysinit [[email protected] rc.d]# cd /tmp [[email protected] tmp]# vim /tmp/rc.sysinit #在末行模式下输入 :%s/^[^[:graph:]]/#&/ :wq #验证: [[email protected] tmp]# tail -10 /tmp/rc.sysinit ==> /tmp/rc.sysinit <== [ "$PROMPT" != no ] && plymouth --ignore-keystroke=Ii if strstr "$cmdline" confirm ; then # touch /var/run/confirm fi # Let rhgb know that we‘re leaving rc.sysinit if [ -x /bin/plymouth ]; then # /bin/plymouth --sysinit fi
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
[[email protected] tmp]# ls /boot/grub/grub.conf /boot/grub/grub.conf [[email protected] tmp]# cp /boot/grub/grub.conf /tmp [[email protected] tmp]# vim /tmp/grub.conf #在进入文本时会有一个提示,选择E,进入文件 #在末行模式下输入: :%s/^[^[:graph:]]\+// :wq
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
[[email protected] ~]# vim /tmp/rc.sysinit #末行模式下输入 :%/^#[[:space:]]\+// :wq
4、为/tmp/grub.conf文件中前三行的行首加#号;
[[email protected] ~]# vim /tmp/grub.conf #末行模式下输入 :1,3s/^/#&/ :wq
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
[[email protected] ~]# vim /etc/yum.repos.d/CentOS-Media.repo #末行模式下输入 :%s/enabled=0/enabled=1/g :%s/gpgcheck=0/gpgcheck=1/g :wq
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
[email protected] ~]# crontab -e [email protected] ~]# /mkdir backup * */4 * * * cp -a /etc /backup/etc-`date +%Y%m%d%H%M
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
[email protected] ~]# crontab -e [email protected] ~]# mkdir /backup/messages_logs * * * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
[email protected] ~]# crontab -e [email protected] ~]# mkdir /stats */2 * * * /bin/egrep ‘^s|S‘ /proc/meminfo >> /stats/memory.txt
9、工作日的工作时间内,每两小时执行一次echo "howdy"
[email protected] ~]# crontab -e [email protected] ~]# mkdir /stats * */2 * * 1-5 /bin/echo "howdy"
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
#!/bin/bash # mkdir -pv /tmp/testdir-`date +%Y%m%d%H%M`
11、在此目录创建100个空文件:file1-file100
#!/bin/bash # dirname=/tmp/testdir-`date +%Y%m%d%H%M` mkdir $dirname for digits in {1..100};do touch $dirname/file$digits done #验证: [[email protected] tmp]# ls testdir-201608281853/ file1 file16 file23 file30 file38 file45 file52 file6 file67 file74 file81 file89 file96 file10 file17 file24 file31 file39 file46 file53 file60 file68 file75 file82 file9 file97 file100 file18 file25 file32 file4 file47 file54 file61 file69 file76 file83 file90 file98 file11 file19 file26 file33 file40 file48 file55 file62 file7 file77 file84 file91 file99 file12 file2 file27 file34 file41 file49 file56 file63 file70 file78 file85 file92 file13 file20 file28 file35 file42 file5 file57 file64 file71 file79 file86 file93 file14 file21 file29 file36 file43 file50 file58 file65 file72 file8 file87 file94 file15 file22 file3 file37 file44 file51 file59 file66 file73 file80 file88 file95
12、显示/etc/passw d文件中位于第偶数行的用户的用户名;
#!/bin/bash # grep -o ‘^\<[[:alnum:]]*\>‘ /etc/passwd | sed -n ‘2~2p‘ #验证: [[email protected] tmp]# bash /bin/passwdline2x.sh bin adm sync halt uucp games ftp dbus vcsa rpcuser haldaemon saslauth sshd tcpdump mageia opentack openstacd archlinux testbash nologin
13、创建10用户user10-user19;密码同用户名;
[[email protected] tmp]# cat /adduser.sh #!/bin/bash if [ ! $UID -eq 0 ]; then echo "Only root" exit 1 fi for i in {10..19}; do if id user$i &> /dev/null; then echo "user$i exists" else useradd user$i if [ $? -eq 0 ]; then echo "user$i" | passwd --stdin user$i &> /dev/null echo "Add user$i successed." fi fi done #验证: [[email protected] tmp]# bash /adduser.sh Add user10 successed. Add user11 successed. Add user12 successed. Add user13 successed. Add user14 successed. Add user15 successed. Add user16 successed. Add user17 successed. Add user18 successed. Add user19 successed. [[email protected] tmp]# tail -10 /etc/passwd user10:x:3011:3011::/home/user10:/bin/bash user11:x:3012:3012::/home/user11:/bin/bash user12:x:3013:3013::/home/user12:/bin/bash user13:x:3014:3014::/home/user13:/bin/bash user14:x:3015:3015::/home/user14:/bin/bash user15:x:3016:3016::/home/user15:/bin/bash user16:x:3017:3017::/home/user16:/bin/bash user17:x:3018:3018::/home/user17:/bin/bash user18:x:3019:3019::/home/user18:/bin/bash user19:x:3020:3020::/home/user19:/bin/bash
14、在/tmp/创建10个空文件file10-file19;
#!/bin/bash # for i in {10..19};do touch /tmp/file$i && echo "file$i make success" done #验证 [[email protected] tmp]# bash /bin/mkfile.sh file10 make success file11 make success file12 make success file13 make success file14 make success file15 make success file16 make success file17 make success file18 make success file19 make success [[email protected] tmp]# ls /tmp/ etc.test file12 file15 file18 grub.conf file10 file13 file16 file19 testdir-201608281853 file11 file14 file17
15、把file10的属主和属组改为user10,依次类推。
#!/bin/bash # for i in {10..19};do chown user$i:user$i /tmp/file$i && echo "file$i‘s owner and group mddify success!" done #验证: [[email protected] tmp]# bash /bin/changemode.sh file10‘s owner and group mddify success! file11‘s owner and group mddify success! file12‘s owner and group mddify success! file13‘s owner and group mddify success! file14‘s owner and group mddify success! file15‘s owner and group mddify success! file16‘s owner and group mddify success! file17‘s owner and group mddify success! file18‘s owner and group mddify success! file19‘s owner and group mddify success! [[email protected] tmp]# ls -l /tmp/file1* -rw-r--r--. 1 user10 user10 0 Aug 28 19:31 /tmp/file10 -rw-r--r--. 1 user11 user11 0 Aug 28 19:31 /tmp/file11 -rw-r--r--. 1 user12 user12 0 Aug 28 19:31 /tmp/file12 -rw-r--r--. 1 user13 user13 0 Aug 28 19:31 /tmp/file13 -rw-r--r--. 1 user14 user14 0 Aug 28 19:31 /tmp/file14 -rw-r--r--. 1 user15 user15 0 Aug 28 19:31 /tmp/file15 -rw-r--r--. 1 user16 user16 0 Aug 28 19:31 /tmp/file16 -rw-r--r--. 1 user17 user17 0 Aug 28 19:31 /tmp/file17 -rw-r--r--. 1 user18 user18 0 Aug 28 19:31 /tmp/file18 -rw-r--r--. 1 user19 user19 0 Aug 28 19:31 /tmp/file19
时间: 2024-10-31 05:44:51