034-shell習題01

1.只要「/etc/rc.d/rc.local」或者「/etc/init.d/rc.local」中有一個是文件,則顯示「YES」,不然無任何輸出。

[root@Carlton ~]# [ -f /etc/rc.d/rc.local ] || [ -f /etc/init.d/rc.local ]&&echo  "YES"
YES

2.若當前用戶是root 且使用的shell程序是「/bin/bash」 則顯示"yes"不然無任何輸出。

[root@Carlton ~]# echo $USER$SHELL
root/bin/bash
[root@Carlton ~]# [ $USER = "root" ]||[ $SHELL = "/bin/bash" ]&& echo "yes"
yes

3. 檢查「/var/log/messages」文件是否存在,若存在則統計文件內容的行數並輸出,不然不作任何操做log

[root@Carlton September]# vim file.sh 
#/bin/bash
file="/var/log/messages"
if [ -f $file ]; 
   then
   wc -l $file
fi

[root@Carlton September]# ./file.sh 
1 /var/log/messages

4.提示用戶指定備份目錄的路徑,若目錄已存在則顯示提示信息後跳過,不然顯示相應提示信息後建立該目錄。

[root@Carlton scripts]# vim backup.sh 
read -p "what is your backup directory?" backfile
if [ -d $backfile ];
then
echo "$backfile is already exis"
else
echo "$backfile is not exist,will make it"
mkdir -p $backfile
fi
[root@Carlton scripts]# ./backup.sh 
what is your backup directory?/etc        
/etc is already exis
[root@Carlton scripts]# ./backup.sh 
what is your backup directory?/server/scripts/toolss/
/server/scripts/toolss/ is not exist,will make it
[root@Carlton scripts]# find . -type d -maxdepth 1|grep /too*
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.

./toolss
./tools
[root@Carlton scripts]# ls -ld /server/scripts/tools*
drwxr-xr-x 2 root root 4096 Aug 31 09:02 /server/scripts/tools
drwxr-xr-x 2 root root 4096 Aug 31 09:08 /server/scripts/toolss

5.統計當前登陸到系統中的用戶數量,並判斷是否超過三個,如果則顯示實際數量並給出警告信息,不然列出登陸的用戶帳號名稱及所在終端。

[root@Carlton September]# ./test.sh 
this is temporary alert,too many login users(Total:6)
[root@Carlton September]# vim test.sh 
Usernumber=`who |wc -l`
if [ $Usernumber -gt 7 ];  #數字能夠改成3
  then
echo "this is temporary alert,too many login users(Total:$Usernumber)"
else
 echo "Login users: "
who | awk '{print $1}'
fi
"test.sh" 9L, 184C written                                    
[root@Carlton September]# ./test.sh 
Login users: 
carlton
root
root
root
root
root

6.檢查portmap進程是否已經存在,若已經存在則輸出「portmap service is running.」 ;不然檢查是否存在「/etc/rc.d/init.d/portmap」 可執行腳本,存在則啓動portmap服務,不然提示「no portmap script file.」。

[root@Carlton scripts]# ./portmap.sh 
portmap service is running
[root@Carlton scripts]# vim portmap.sh 
#!/bin/bash
 #top -bn1 |grep portmap &>/dev/null
 pgrep kacdwadhpid &>/dev/null
   if [ $? -eq 0 ] ;
then
     echo "portmap service is running"
elif [ -x "/etc/rc.d/init.d/portmap" ] ;
     then
        Service portmap start
else
       echo "no portmap script file"
fi

"portmap.sh" 13L, 275C written                                                                                                                                                                                           
[root@Carlton scripts]# ./portmap.sh 
no portmap script file

7.每隔五分鐘監測一次mysqld服務進程的運行狀態,若發現mysqld進程已終止,則在「/var/log/messages」文件中追加寫入日誌信息(包括當時時間),並重啓mysqld服務;不然不進行任何操做。

#!/bin/bash
Pgrep mysqld &>/dev/null
If [$? –ne 0];
  Then
  echo 「`$date +%F` is restart mysqld services 」 >/var/log/messages
  services mysqld restart 
fi
crontab –e 
5 * * * * /server/scripts/mysqld.sh
crontab -l

8.依次輸出三條文字信息,包括一天中的「Moring」、「Noon」、「Evening」字串。

[root@Carlton scripts]# vim XB.sh 
#!/bin/bash
for XB in "Morning" "Noon" "Evening"
do
  echo "The $XB of the day"
done

"XB.sh" 6L, 87C written                                                                                                                                                                                                  
[root@Carlton scripts]# ./XB.sh 
The Morning of the day
The Noon of the day
The Evening of the day

9.對於使用「/bin/bash」做爲登陸shell的系統用戶,檢查他們在「/opt」目錄中擁有的子目錄或文件數量,若是超過100個,則列出具體數值及對應的用戶帳號。

#!/bin/bash
DIR="/opt"
NUM=100
USERnum=`grep "/bin/bash" /etc/passwd |cut -d ":" -f 1`
for nnnn in $USERnum
do
  NUN=`find $DIR -user $nnnn |wc -l`
if [ $NUN -gt 100 ];
   then
   echo " $nnnn have $NUN files in $DIR "
fi
done

"bbb.sh" 13L, 231C written                                    
[root@Carlton scripts]# ./bbb.sh 
 root have 140 files in /opt 
[root@Carlton scripts]# find /opt -user root |wc -l
140
相關文章
相關標籤/搜索