小小腳本飛啊飛

   今天一下寫了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."

 

    希望本文能夠幫助到您,如有錯誤敬請指出,拜謝!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章