1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
方法一:vim替换
|
1
2
3
4
5
|
[[email protected] ~]# cat /etc/redhat-release
CentOS release 6.5 (Final)[[email protected] ~]# cp /etc/rc.d/rc.sysinit /tmp/
[[email protected] ~]# vim /tmp/rc.sysinit
:%s/^[[:space:]]/#&/ # #@表示在匹配的内容前加#
|
方法二:sed替换
|
1
2
3
|
[[email protected] ~]# rm -f /tmp/rc.sysinit
[[email protected] ~]# cp /etc/rc.d/rc.sysinit /tmp/
[[email protected] ~]# sed '[email protected]^[[:space:]]@#&@' /tmp/rc.sysinit #此替换只显示到屏幕上,使用sed -i可以直接对文件进行更改
|
图示:
vim替换后用vimdiff对比文件
sed替换,屏幕显示
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
方法一:vim替换
|
1
2
3
|
[[email protected] ~]# cp /boot/grub/grub.conf /tmp/
[[email protected] ~]# vim /tmp/grub.conf
:%s/^[[:space:]]\+// #^[[:space:]]以空格开头,\+还可以再出现空格,//把前面内容替换为空
|
方法二:sed替换
|
1
2
3
|
[[email protected] ~]# rm -f /tmp/grub.conf
[[email protected] ~]# cp /boot/grub/grub.conf /tmp/
[[email protected] ~]# sed '[email protected]^[[:space:]]\[email protected]@' /tmp/grub.conf # /还可以换成@或#
|
图示:
vim替换后用vimdiff对比文件
sed替换,屏幕显示
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
方法一:vim替换
|
1
2
3
|
[[email protected] ~]# cp /etc/rc.d/rc.sysinit /tmp/
[[email protected] ~]# vim /tmp/rc.sysinit
:%s/^#[[:space:]]\+// # ^#[[:space:]]\+以#开头后面可以接空格,空格可多次出现,//把前面内容替换为空
|
方法二:sed替换
|
1
2
3
|
[[email protected] ~]# rm -f /tmp/rc.sysinit
[[email protected] ~]# cp /etc/rc.d/rc.sysinit /tmp/
[[email protected] ~]# sed '[email protected]^#[[:space:]]\[email protected]@' /tmp/rc.sysinit #此处对/tmp/rc.sysinit演示
|
图示:
vim替换后用vimdiff对比文件
sed替换,屏幕显示
4、为/tmp/grub.conf文件中前三行的行首加#号;
方法一:vim替换
|
1
2
|
[[email protected] ~]# vim /tmp/grub.conf
:1,3s/^/#&/ #1,3s表示1-3行,#&在1-3行前加#
|
方法二:sed替换
|
1
2
3
4
|
[[email protected] ~]# 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 |
图示:
vim替换后用vimdiff对比文件
sed替换,屏幕显示
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
方法一:vim替换
|
1
2
3
4
|
[[email protected] ~]# cp /etc/yum.repos.d/CentOS-Media.repo /tmp/
[[email protected] ~]# vim /etc/yum.repos.d/CentOS-Media.repo
:%s/enabled=0/enabled=1/g #全局把enabled=0替换成enabled=1
:%s/gpgcheck=0/gpgcheck=1/g #全局把gpgcheck=0替换成gpgcheck=1
|
方法二:sed替换
|
1
|
[[email protected] ~]# sed '/^enabled=/{s/=0$/=1/}; /^gpgcheck=/{s/=0$/=1/}' /tmp/CentOS-Media.repo
|
图示:
vim替换后用vimdiff对比文件
sed替换,屏幕显示
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
首先单独测试一下备份命令
|
1
2
3
4
|
[[email protected] ~]# mkdir backup #在当前目录创建备份目录backupp
[[email protected] ~]# cp -a shell/ backup/etc-`date +%Y%m%d%H%M` #使用-a参数把shell文件夹复制到backup下,date +%Y%m%d%H%M是依照题目的日期格式
[[email protected] ~]# ls backup/etc-201609121833/ #查看是否复制成功
0 3 grub2.cfg grub.cfg space.sh test.sh useridsum.s
|
以上测试成功后,再创建定时任务
|
1
2
3
4
5
6
7
|
[[email protected] ~]# mkdir /backup
[[email protected] ~]# which cp #查看cp命令的完整路径
alias cp='cp -i'
/bin/cp
[[email protected] ~]# crontab -e #crontab -e编辑任务
#每4小时执行一次对/etc目录的备份,备份至/backup目录中 #计划任务时最好加注释,不然时间长了就忘记这任务有什么用了,英语不好,现在先用中文注释0 */4 * * * bin/cp /etc /backup/etc-`date +%Y%m%d%H%M`
|
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
首先单独测试一下备份命令
|
1
2
3
4
|
[[email protected] ~]# mkdir -p /backup/messages_logs
[[email protected] ~]# cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`
[[email protected] ~]# ll /backup/messages_logs/messages-20160912
-rw------- 1 root root 145 Sep 11 03:36 /backup/messages_logs/messages-20160912
|
以上测试成功后,再创建定时任务
|
1
2
3
|
[[email protected] ~]# crontab -e #crontab -e编辑任务
#每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中 #这行是注释,暂时用中替代,说明下面命令是干什么的0 0 * * 2,4,6 /bin/cp -a /var/log/messages /backup/messages_logs/messages-`date +%Y%m%d`
|
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
首先单独测试一下命令
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[[email protected] ~]# mkdir /stats #创建status文件夹
[[email protected] ~]# which grep #查看grep命令完整路径
/bin/grep[[email protected] ~]# grep '^S' /proc/meminfo #查看以S开头的信息
SwapCached: 0 kBSwapTotal: 2031608 kBSwapFree: 2031608 kBShmem: 228 kBSlab: 57452 kBSReclaimable: 33632 kBSUnreclaim: 23820 kB[[email protected] ~]# grep '^S' /proc/meminfo > /stats/memory.txt #追加到/stats/memory.txt中
[[email protected] ~]# cat /stats/memory.txt #查看,追加成功
SwapCached: 0 kBSwapTotal: 2031608 kBSwapFree: 2031608 kBShmem: 228 kBSlab: 57504 kBSReclaimable: 33644 kBSUnreclaim: 23860 kB |
以上测试成功后,再创建定时任务
|
1
2
3
|
[[email protected] ~]# crontab -e
#每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中 #这行是注释,暂时用中替代,说明下面命令是干什么的0 */2 * * * /bin/grep '^S' /proc/meminfo > /stats/memory.txt
|
9、工作日的工作时间内,每两小时执行一次echo "howdy"
|
1
2
3
4
|
[[email protected] ~]# which echo #查看echo命令完整路径
/bin/echo[[email protected] ~]# crontab -e
* 9-18/2 * * 1-5 /bin/echo "howdy" #9-18/2表示9点至18点每2个小时运行,1-5是周一至周五
|
* 9-18/2 * * 1-5谢谢老师的提醒,分钟没有写明。
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
首先单独测试一下命令
|
1
2
3
|
[[email protected] ~]# mkdir /tmp/testdir-`date +%Y%m%d%H%M`
[[email protected] ~]# ls -d /tmp/testdir-201609121114/ #下面已创建符合要求的目录
/tmp/testdir-201609121114/ |
脚本
|
1
2
|
#!/bin/bashmkdir /tmp/testdir-`date +%Y%m%d%H%M`
|
11、在此目录创建100个空文件:file1-file100
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[[email protected] tmp]# vim touchfile.sh
#!/bin/bash#for i in {1..100};do #按照题目要求创建一个1-100的循环
touch /tmp/testdir-201609121114/file$i #根据变量创建文件
done[[email protected] tmp]# bash -n touchfile.sh
[[email protected] tmp]# bash touchfile.sh
[[email protected] tmp]# ll /tmp/testdir-201609121114/
total 0-rw-r--r-- 1 root root 0 Sep 12 16:03 file1-rw-r--r-- 1 root root 0 Sep 12 16:03 file10-rw-r--r-- 1 root root 0 Sep 12 16:03 file100-rw-r--r-- 1 root root 0 Sep 12 16:03 file11-rw-r--r-- 1 root root 0 Sep 12 16:03 file12-rw-r--r-- 1 root root 0 Sep 12 16:03 file13-rw-r--r-- 1 root root 0 Sep 12 16:03 file14.....以下略 |
12、显示/etc/passw d文件中位于第偶数行的用户的用户名;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[[email protected] shell]# vim test11.sh
#!/bin/bash#sed -n 'n;p' /etc/passwd | cut -d':' -f1
[[email protected] shell]# bash -n test11.sh #检查语法
[[email protected] shell]# bash test11.sh #执行test11.sh脚本
binadmsynchaltuucpgamesftpvcsapostfixnginxntp |
13、创建10用户user10-user19;密码同用户名;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
[[email protected] shell]# vim useradd.sh
#!/bin/bash#if [ ! $UID -eq 0 ]; then #判断UID是否为0,0是root。
echo "Only root." #如果不是root,给出提示,并退出
exit 1
fifor username in {10..19};do #创建一个循环,10-19
if id $username &> /dev/null; then #判断用户是否存在
echo "$username exists." #如果存在,给出提示
else
useradd user$username &> /dev/null #不存在就创建用户
if [ $? -eq 0 ]; then #如果上面创建用户,echo $?等于0,就说明创建成功,为用户设置密码,提示Add $username finished.
echo "user$username" | passwd --stdin user$username &> /dev/null
echo "Add user$username finished." fi
fi
done[[email protected] shell]# bash -n useradd.sh #检查语法
[[email protected] shell]# bash useradd.sh #执行useradd.sh脚本
Add user10 finished.Add user11 finished.Add user12 finished.Add user13 finished.Add user14 finished.Add user15 finished.Add user16 finished.Add user17 finished.Add user18 finished.Add user19 finished. |
14、在/tmp/创建10个空文件file10-file19;
|
1
2
3
4
5
6
7
8
9
10
11
|
[[email protected] ~]# vim touch.sh
#!/bin/bash#for i in {10..19};do #创建一个循环,10-19
touch /tmp/file$i #创建文件夹
done[[email protected] ~]# bash -n touch.sh #检查语法
[[email protected] ~]# bash touch.sh #执行touch.sh脚本
[[email protected] ~]# ls
b file11 file13 file15 file17 file19 grub2.cfg vim.txtfile10 file12 file14 file16 file18 functions touch.sh
|
15、把file10的属主和属组改为user10,依次类推。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
[[email protected] tmp]# vim chown.sh
for i in {10..19};do #创建一个循环
if [ -e /tmp/file$i ];then #文件存在测试
chown user$i:user$i /tmp/file$i #更改属主和属组为
else echo "No such file." #文件不在,则提示No such file.
fi
done[[email protected] tmp]# bash -n chown.sh
[[email protected] tmp]# bash chown.sh
[[email protected] tmp]# ll #user10-19属主和属组已改
total 44-rw-r--r-- 1 root root 630 Sep 12 09:58 CentOS-Media.repo-rw-r--r-- 1 root root 175 Sep 12 15:58 chown.sh
-rw-r--r-- 1 user10 user10 0 Sep 12 15:57 file10-rw-r--r-- 1 user11 user11 0 Sep 12 15:57 file11-rw-r--r-- 1 user12 user12 0 Sep 12 15:57 file12-rw-r--r-- 1 user13 user13 0 Sep 12 15:57 file13-rw-r--r-- 1 user14 user14 0 Sep 12 15:57 file14-rw-r--r-- 1 user15 user15 0 Sep 12 15:57 file15-rw-r--r-- 1 user16 user16 0 Sep 12 15:57 file16-rw-r--r-- 1 user17 user17 0 Sep 12 15:57 file17-rw-r--r-- 1 user18 user18 0 Sep 12 15:57 file18-rw-r--r-- 1 user19 user19 0 Sep 12 15:57 file19-rw------- 1 root root 822 Sep 12 09:48 grub.conf-rwxr-xr-x 1 root root 19688 Sep 12 09:41 rc.sysinitdrwxr-xr-x 2 root root 4096 Sep 12 14:19 testdir-201609121114drwxr-xr-x 2 root root 4096 Sep 12 11:11 testdir-date +%m%d
-rw-r--r-- 1 root root 108 Sep 12 15:57 touch.sh
-rw-------. 1 root root 0 Jul 8 01:41 yum.log |