今天一下写了5篇博客好累啊,这是最后一篇把我写的一些脚本给大家参考一下,有改进的地方希望各位留言指出来,谢谢啦!

练习:

1、创建10用户user10-user19;密码同用户名;

2、在/tmp/创建10个空文件file10-file19; 

3、把file10的属主和属组改为user10,依次类推;

## [root@bogon scrips]# vim mkuserfile.sh

##  1 #!/bin/bash

##  2 #

##  3 dir=/tmp/filedb

##  4 username=user

##  5 filename=file

##  6 cd $dir

##  7 for i in {10..19};do

##  8         useradd $username$i

##  9         echo $username$i | passwd --stdin $username$i &> /dev/null

## 10         touch $filename$i

## 11         chown $username$i:$username$i $filename$i

## 12 done


练习:写一个脚本

1、获取并列出当前系统上的所有磁盘设备;

2、显示每个磁盘设备上每个分区相关的空间使用信息;

## [root@bogon scrips]# vim ckdiskinfo.sh 

##  1 #!/bin/bash

##  2 #

##  3 echo -e "These are owned by our system disk:

##  4 `fdisk -l | grep -o '^Disk /dev/.*:' | cut -d: -f1`\n"

##  5 

##  6 cat << EOF

##  7 (1) Make your choice(Example:/dev/sda).

##  8 (2) Enter 'q' to quite.

##  9 EOF

## 10 read -p 'Your choice is:' choice

## 11 while [ $choice != 'q' ];do

## 12         echo -e "\033[34mDisk info:\033[0m"

## 13         fdisk -l $choice

## 14         echo -e "\033[34mUseage info:\033[0m"

## 15         df -m $choice 2> /dev/null

## 16         if [ $? != 0 ];then

## 17                 echo -e "\033[31mPlease enter the disk device on the list:\n`fdisk -l | grep -o '^Disk /dev/.*:'     | cut -d: -f1`\033[0m\n"

## 18         fi

## 19         read -p 'Please choice again:' choice

## 20 done


3、写一个脚本/sbin/datamonitor.sh,要求:

(1)判断/data目录是否存在,且挂载了一个存储设备(通过grep /etc/mtab文件实现);

(2)如果是,则分别显示此设备上总的空间大小和可用空间大小;如果可用空间大小与总空间大小之比低于20%,则以红色显示警告信息;

(3)同时显示总的inode条目的数目和可用inode条目的数目;如果可用可用inode条目的数目与总的inode条目的数目之比低于20%,则以红色显示警告信息;

(4)以root用户的身份设置此脚本每两小时执行一次;

## [root@bogon scrips]# vim datamonitor.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 grep '/data' /etc/mtab &> /dev/null

##  4 exist=$?

##  5 dev=`grep '/data' /etc/mtab | cut -d' ' -f1`

##  6 if [ $exist = 0 ];then

##  7         df $dev;df -i $dev

##  8         useage=`df $dev | grep -o '[0-9]%' | cut -d% -f1`

##  9         iuseage=`df -i $dev | grep -o '[0-9]%' | cut -d% -f1`

## 10         if [ $useage -lt 20 ];then

## 11                 echo -e "\033[31mWarning:The disk useage is too low.\033[0m"

## 12         fi

## 13         if [ $iuseage -lt 20 ];then

## 14                 echo -e "\033[31mWarning:The inode useage is too low.\033[0m"

## 15         fi

## 16 fi

## 17 grep "\<datamonitor.sh\>" /var/spool/cron/root &> /dev/null

## 18 cronexist=$?

## 19 if [ $cronexist != 0 ];then

## 20         echo "1 */2 * * * /root/scrips/datamonitor.sh" >> /var/spool/cron/root

## 21 fi


4、写一个小脚本,并执行;要求实现:

   (1)新建ID为306的系统组mysql;新建ID为306的系统用户mysql,要求其没有家目录,shell为/bin/nologin;

   (2)新建组dba;新建用户florian,要求其家目录为/users/florian,密码同用户名; 

   (3)新建用户douglas,其家目录为/users/douglas,密码同用户名;

   (4)用户florian和douglas均以dba为其附加组;

## [root@bogon scrips]# vim sysuseradd.sh 

##

##  1 #!/binbash

##  2 #

##  3 username='florian'

##  4 groupadd -r -g 306 mydb

##  5 useradd -M -r -u 306 -g 306 -s /bin/nologin mydb

##  6 mkdir -p /users/florian

##  7 groupadd dba

##  8 useradd -d /users/florian $username

##  9 echo $username | passwd --stdin $username

## 10 mkdir /users/douglas

## 11 useradd -d /users/douglas douglas

## 12 echo 'douglas' | passwd --stdin douglas

## 13 usermod -a -G dba florian

## 14 usermod -a -G dba douglas


练习:写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;

## [root@bogon scrips]# vim job0406.useradd.sh

##  1 #!/bin/bash

##  2 #

##  3 sum=0

##  4 for i in {1..20};do

##  5         useradd visitor$i

##  6         uid=`cat /etc/passwd | grep "\<visitor$i\>" | cut -d: -f3`

##  7         let sum+=$uid

##  8 done

##  9 echo "The sum of uid is $sum."


练习:写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;

## [root@bogon scrips]# vim job0406.fileline.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 for i in $@;do

##  4         lines=`grep "^#" $i | wc -l`

##  5         lineb=`grep "^$" $i | wc -l`

##  6         let sum+=$lines

##  7         let sumb+=$lineb

##  8         echo "$i of # start lines are:$lines."

##  9         echo "$i of blank start lines are:$lineb."

## 10 done

## 11 echo "The sum of # lines are:$sum."

## 12 echo "The sum of blank lines are:$sumb."


练习:写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;

## [root@bogon scrips]# vim job0406.uidsum.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 dir=/etc/passwd

##  4 declare -i lines=`cat $dir | grep '/bin/bash$' | wc -l &> /dev/null`

##  5 for i in {1..$lines};do

##  6         name=`grep '/bin/bash$' $dir | cut -d: -f1`

##  7         echo -e "Those username are: \n$name."

##  8         uid=`grep "/bin/bash$" $dir | cut -d: -f3`

##  9         for i in $uid;do

## 10                 let sum+=$i

## 11         done

## 12 done

## 13 echo "The sum of uid is:$sum."


练习:写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;

## [root@bogon scrips]# vim job0406.groupplus.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 userlist=`grep "[^:]$" /etc/group | cut -d: -f4`

##  4 let usernum=`cat /etc/passwd | wc -l`

##  5 for i in `seq 1 $usernum`;do

##  6         username=`head -$i /etc/passwd | tail -1 | cut -d: -f1`

##  7         echo "$userlist" | grep -o "$username" &> /dev/null

##  8         if [ $? == 0 ];then

##  9                 declare -i j=1

## 10                 let sum+=j

## 11         fi

## 12         echo "$userlist" | grep -o "$username" | sort -u

## 13 done

## 14 echo $sum


练习:写一个脚本

1、使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;

在线的主机使用绿色显示;

不在线的主使用红色显示;


## 练习:ping脚本

## [root@bogon scrips]# vim job0407.ping.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 ip=192.168.19.

##  4 for i in {1..254};do

##  5         ping -w 2 $ip$i &> /dev/null

##  6         if [ $? == 0 ];then

##  7                 echo -e "\033[32m$ip$i\033[0m"

##  8         else

##  9                 echo -e "\033[31m$ip$i\033[0m"

## 10         fi

## 11 done


练习:写一个脚本

(1) 传递一些目录给此脚本;

(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;

(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;

## [root@bogon scrips]# vim job0409.dircount.sh 

##

##  1 #!/bin/bash

##  2 #

##  3 for i in $@;do

##  4         for j in `ls $i`;do

##  5                 file $i/$j

##  6                 dir=`file $i/$j | cut -d: -f2 | cut -d' ' -f2`

##  7                 if [ $dir == "directory" ];then

##  8                         let l++

##  9                 fi

## 10                 let k++

## 11         done

## 12 done

## 13 let f=$k-$l

## 14 echo -e "\033[33mTotal dir:$l\033[0m,\033[32mtotal file:$f\033[0m,\033[31msum is $k\033[0m."

 

    希望本文能够帮助到您,如有错误敬请指出,拜谢!