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

 

转载于:https://my.oschina.net/u/3635512/blog/1526199

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值