一、複製/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;
[10:21:13 root@qa36 ~]#cp /etc/rc.d/rc.sysinit /tmp/ [10:21:26 root@qa36 ~]#ll /tmp/ 總用量 24 -rwxr-xr-x 1 root root 19688 9月 7 10:21 rc.sysinit [10:44:33 root@qa36 /tmp]#sed -n 's/^[[:space:]]\+/#&/gp' /tmp/rc.sysinit | more # . /etc/sysconfig/network # HOSTNAME=localhost # mount -n -t proc /proc /proc # mount -n -t sysfs /sys /sys >/dev/null 2>&1 # modprobe usbcore >/dev/null 2>&1 && mount -n -t usbfs /proc/bus/usb /proc/bus/usb # mount -n -t usbfs /proc/bus/usb /proc/bus/usb # if [ -r "/selinux/enforce" ] ; then # SELINUX_STATE=$(cat "/selinux/enforce") # else # # assume enforcing if you can't read it # SELINUX_STATE=1 # fi # /sbin/restorecon -R -F /dev 2>/dev/null # echo $"*** Warning -- SELinux is active" # echo $"*** Disabling security enforcement for system recovery." # echo $"*** Run 'setenforce 1' to reenable." # echo "0" > "/selinux/enforce" # # if /sbin/init is not labeled correctly this process is running in the # # wrong context, so a reboot will be required after relabel # AUTORELABEL= # . /etc/selinux/config # echo "0" > /selinux/enforce # [ -n "$PLYMOUTH" ] && plymouth --hide-splash # if [ "$AUTORELABEL" = "0" ]; then # echo # echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required. " # echo $"*** /etc/selinux/config indicates you want to manually fix labeling" # echo $"*** problems. Dropping you to a shell; the system will reboot" # echo $"*** when you leave the shell." # start rcS-emergency # else # echo # echo $"*** Warning -- SELinux ${SELINUXTYPE} policy relabel is required." # echo $"*** Relabeling could take a very long time, depending on file" # echo $"*** system size and speed of hard drives." # /sbin/fixfiles -F restore > /dev/null 2>&1 # fi # rm -f /.autorelabel --More--
二、複製/boot/grub/grub.conf至/tmp目錄中,刪除/tmp/grub.conf文件中的行首的空白字符;
[10:48:18 root@qa36 /tmp]#cp /boot/grub/grub.conf /tmp/ [10:49:09 root@qa36 /tmp]#sed 's/^[[:space:]]\+//g' grub.conf #grub.confgeneratedbyanaconda # #Notethatyoudonothavetorerungrubaftermakingchangestothisfile #NOTICE:Youhavea/bootpartition.Thismeansthat #allkernelandinitrdpathsarerelativeto/boot/,eg. #root(hd0,0) #kernel/vmlinuz-versionroroot=/dev/sda3 #initrd/initrd-[generic-]version.img #boot=/dev/sda default=0 timeout=5 splashp_w_picpath=(hd0,0)/grub/splash.xpm.gz hiddenmenu titleCentOS(2.6.32-431.el6.x86_64) root(hd0,0) kernel/vmlinuz-2.6.32-431.el6.x86_64roroot=UUID=10cca036-87a5-4646-9bdb-88f252f589berd_NO_LUKSKEYBOARDTYPE=pcKEYTABLE=usrd_NO_MDcrashkernel=autoLANG=zh_CN.UTF-8rd_NO_LVMrd_NO_DMrhgbquiet initrd/initramfs-2.6.32-431.el6.x86_64.img
三、刪除/tmp/rc.sysinit文件中的以#開頭,且後面跟了至少一個空白字符的行行的#和空白字符
[10:52:42 root@qa36 /tmp]#sed -n 's/^#[[:space:]]\+//gp' rc.sysinit | more /etc/rc.d/rc.sysinit - run once at boot time Taken in part from Miquel van Smoorenburg's bcheckrc. Check SELinux status Print a text banner. Only read this once. Initialize hardware Set default affinity Load other user-defined modules Load modules (for backward compatibility with VARs) Configure kernel parameters Set the hostname. Sync waiting for storage. Device mapper & related initialization Start any MD RAID arrays that haven't been started yet Remount the root filesystem read-write. Clean up SELinux labels If relabeling, relabel mount points. Mount all other filesystems (except for NFS and /proc, which is already mounted). Contrary to standard usage, filesystems are NOT unmounted in single user mode. The 'no' applies to all listed filesystem types. See mount(8). Update quotas if necessary Check to see if a full relabel is needed Initialize pseudo-random number generator Configure machine if necessary. Clean out /. Do we need (w|u)tmpx files? We don't set them up, but the sysadmin might... Clean up /var. Clean up utmp/wtmp Clean up various /tmp bits Make ICE directory Start up swapping. Set up binfmt_misc Boot time profiles. Yes, this should be somewhere else. Now that we have all of our basic modules loaded and the kernel going, let's dump the syslog ring somewhere so we can find it later create the crash indicator flag to warn on crashes, offer fsck with timeout Let rhgb know that we're leaving rc.sysinit
四、爲/tmp/grub.conf文件中前三行的行首加#號;
[11:19:54 root@qa36 /tmp]#sed -n '1,3s/.*/#&/gp' grub.conf ## grub.conf generated by anaconda ## ## Note that you do not have to rerun grub after making changes to this file
五、將/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最後的0修改爲1;
[11:33:03 root@qa36 /tmp]#sed -e 's/\(enabled=\)0/\11/g' -e 's/\(pgcheck=\)0/\11/g' /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
六、每4小時執行一次對/etc目錄的備份,備份至/backup目錄中,保存的目錄名爲形如etc-201608300202
0 */4 * * * /usr/bin/tar czfP /backup/etc-`date +\%Y\%m\%d\%H\%M`.tar.gz /etc
七、每週2,4,6備份/var/log/messages文件至/backup/messages_logs/目錄中,保存的文件名形如messages-20160830
10 0 * * 2,4,6 /usr/bin/tar czfP /backup/messages_logs/messages-`date +\%Y\%m\%d`.tar.gz /var/log/messages
八、每天每兩小時取當前系統/proc/meminfo文件中的所有以S開頭的信息至/stats/memory.txt文件中
0 */2 * * * /usr/bin/echo --------`date`-------- >> /stats/memory.txt && grep '^S' /proc/meminfo >> /stats/memory.txt #文本中添加一行時間,檢驗crontab是否正常運行 [root@localhost shell]# more /stats/memory.txt --------2016年 09月 07日 星期三 16:00:02 CST-------- SwapCached: 0 kB SwapTotal: 4064252 kB SwapFree: 4064252 kB Shmem: 8652 kB Slab: 150896 kB SReclaimable: 118728 kB SUnreclaim: 32168 kB --------2016年 09月 07日 星期三 18:00:01 CST-------- SwapCached: 0 kB SwapTotal: 4064252 kB SwapFree: 4064252 kB Shmem: 8652 kB Slab: 150912 kB SReclaimable: 118888 kB SUnreclaim: 32024 kB --------2016年 09月 07日 星期三 20:00:01 CST-------- SwapCached: 0 kB SwapTotal: 4064252 kB SwapFree: 4064252 kB Shmem: 8620 kB Slab: 150900 kB SReclaimable: 118868 kB SUnreclaim: 32032 kB
九、工作日的工作時間內,每兩小時執行一次echo "howdy"
0 */2 * * 1-5 /usr/bin/echo "howdy"
腳本編程練習
十、創建目錄/tmp/testdir-當前日期時間;
#!/bin/bash if [[ -d /tmp/testdir-`date +\%Y\%m\%d\%H\%M` ]];then echo '目錄已存在' else mkdir /tmp/testdir-`date +\%Y\%m\%d\%H\%M` && echo '目錄已創建' fi
十一、在此目錄創建100個空文件:file1-file100
#!/bin/bash for((i=1;i<=100;i++)) do touch file${i} done
十二、顯示/etc/passw d文件中位於第偶數行的用戶的用戶名;
[root@localhost shell]# cat oshu.sh #!/bin/bash line=`cat /etc/passwd | wc -l` for((i=2;i<${line};i=i+2)) do sed -n "${i}p" /etc/passwd | awk -F ':' '{print$1}' done [root@localhost shell]# ./oshu.sh bin adm sync halt operator ftp avahi-autoipd systemd-network polkitd postfix ntp slackware bash basher fedora
13、創建10用戶user10-user19;密碼同用戶名;
[root@localhost shell]# vim useradd.sh #!/bin/bash for i in $(seq 10 19) do if id user${i} >/dev/null 2>&1 then echo "user${i}已存在" else useradd user${i} echo user${i} | passwd --stdin user${i} >/dev/null 2>&1 && echo "user${i}創建成功" fi done [root@localhost shell]# ./useradd.sh user10創建成功 user11創建成功 user12創建成功 user13創建成功 user14創建成功 user15創建成功 user16創建成功 user17創建成功 user18創建成功 user19創建成功
14、在/tmp/創建10個空文件file10-file19;
#!/bin/bash for i in $(seq 10 19) do if [ -f /tmp/file${i} ] then echo "file${i}已存在" else touch /tmp/file${i} && echo "file${i}創建成功" fi done [root@localhost shell]# ./file.sh file10創建成功 file11創建成功 file12創建成功 file13創建成功 file14創建成功 file15創建成功 file16創建成功 file17創建成功 file18創建成功 file19創建成功
15、把file10的屬主和屬組改爲user10,依次類推。
[root@localhost shell]# vim chmod.sh #!/bin/bash for i in $(seq 10 19) do if id user${i} >/dev/null 2>&1 then if [ -f /tmp/file${i} ] then chown user${i}:user${i} /tmp/file${i} && echo "file${i}修改屬主和屬組成功" else echo "file${i}不存在" fi else echo echo "user${i}不存在,無法修改file${i}的主和屬組" fi done [root@localhost shell]# ./chmod.sh file10修改屬主和屬組成功 file11修改屬主和屬組成功 file12修改屬主和屬組成功 file13修改屬主和屬組成功 file14修改屬主和屬組成功 file15修改屬主和屬組成功 file16修改屬主和屬組成功 file17修改屬主和屬組成功 file18修改屬主和屬組成功 file19修改屬主和屬組成功