本周作业内容:
1、复制/etc/rc.d/rc.sysinit文件至/tmp目录,将/tmp/rc.sysinit文件中的以至少一个空白字符开头的行的行首加#;
cp /etc/rc.d/rc.sysinit /tmp
sed -i "s/^[[:space:]]\+/#&/" /tmp/rc.sysinit
2、复制/boot/grub/grub.conf至/tmp目录中,删除/tmp/grub.conf文件中的行首的空白字符;
cp /boot/grub/grub.conf /tmp
sed -i "s/^[[:space:]]\+//" /tmp
3、删除/tmp/rc.sysinit文件中的以#开头,且后面跟了至少一个空白字符的行行的#和空白字符
sed -i "s/^#[[:space:]]\+//" tmp/rc.sysinit
4、为/tmp/grub.conf文件中前三行的行首加#号;
sed -i "1,+2s/#/##/" /tmp/grub.conf
5、将/etc/yum.repos.d/CentOS-Media.repo文件中所有的enabled=0或gpgcheck=0的最后的0修改为1;
sed -e "/gpgcheck=0/c gpgcheck=1" -e "/enabled=0/c enabled=1" /etc/yum.repos.d/CentOS-Media.repo
6、每4小时执行一次对/etc目录的备份,备份至/backup目录中,保存的目录名为形如etc-201608300202
0 */4 * * * root /usr/bin/tar czf /backup/etc-$(date +%Y%m%d%H%M) /etc
7、每周2,4,6备份/var/log/messages文件至/backup/messages_logs/目录中,保存的文件名形如messages-20160830
* * * * 2,4,6 root /usr/bin/tar czf /backup/messages_logs/messages-$(date +%Y%m%d) /var/log/messages
8、每天每两小时取当前系统/proc/meminfo文件中的所有以S开头的信息至/stats/memory.txt文件中
0 */2 * * * root /usr/bin/grep "^S" /proc/meminfo > /stats/memory.txt
9、工作日的工作时间内,每两小时执行一次echo "howdy"
0 8,10,14,16 * * 1-5 root /usr/bin/echo "howdy"
脚本编程练习
10、创建目录/tmp/testdir-当前日期时间;
#/bin/bash
#
FileDate="/tmp/testdir$(date +%F-%H-%M)"
if [ -e $FileDate ];then
echo "the directory is existed."
else
mkdir -p $FileDate
echo "The diretcory is created "
fi
11、在此目录创建100个空文件:file1-file100
#!/bin/bash
#
declare -i counter=1
directory="/tmp/testdir"
function create_file {
for couter in {1..100};do
if [ ! -e $directory/file$couter ]; then
touch $directory/file$couter
echo "the file$couter is created"
else
echo "the file$couter existed"
fi
done
}
if [ -e $directory ]; then
create_file
else
mkdir $directory
create_file
fi
12、显示/etc/passw d文件中位于第偶数行的用户的用户名;
#!/bin/bash
#
for username in `sed -n "n;p" /etc/passwd | cut -d: -f1 ` ; do
echo "the username is $username"
let username++
done
13、创建10用户user10-user19;密码同用户名;
#!/bin/bash
#
declare -i x=10
untile [ $x -gt 19 ] ; do
if [ id user$x &> /dev/null ] ; then
echo "The user is existed"
let x++
else
useradd user$x
echo "user$x" | passwd --stdin user$x &> /dev/null
let x++
fi
done
14、在/tmp/创建10个空文件file10-file19;
#!/bin/bash
#
declare -i x=10
while [ $x -le 19 ]; do
if [ -e /tmp/file$i ] ;then
echo "The file$ is existed."
else
touch /tmp/file$x
echo "the file$x was created"
fi
let x++
done
15、把file10的属主和属组改为user10,依次类推。
#!/bin/bash
#
read -p "enter a file with path " filename
if [ ! -e $filename ]; then
echo " the $filename is not exist "
else
name=$(basename $filename)
if [ ! "$(echo "$name" | sed 's@[0-9]*$@@')" == "file" ];then
echo "the filename is not right, it must be like : file20 "
else
num=$(echo "$name" | grep -o "[0-9]*$" )
if ! id user$num &> /dev/null ;then
echo "the user user$num is not exist , you should add it first"
else
chown user$num:user$num $filename
echo "the $filename is changed"
fi
fi
fi