034-shell习题01
2017-08-30 00:00
1126 查看
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
相关文章推荐
- Linux_shell_文件安全与权限(01)
- 算典03_习题_01_Uva-1585
- shell习题-处理日志
- 笨办法学python 习题0中 shell无法调用python
- 01_shell学习
- 《C++ Primer》第五版课后习题解答_第二章(1)(01-08)
- shell例程01
- python核心编程学习笔记-2016-08-23-01-习题16-13
- 01-shell文本处理三剑客之grep
- 高数 01.[08-10]习题课
- shell习题-判断函数
- 01_shell学习
- shell习题-批量同步代码
- 50道Java习题之01
- 高数 01.[01-07]02习题课
- 高数 01.[01-10]习题课
- shell基础知识01
- 01_shell学习
- python核心编程学习笔记-2016-07-28-01-习题6-16
- python核心编程学习笔记-2016-08-03-01-习题9-9