第二周作业

1、在档案中搜寻关键词的命令是( D)。

A、ps   B,eat  C、more  D、grep

 

2、查看⽂件最后100⾏的命令是( )。

 答:tail -n 100 FILENAME

 

3、实现查询⽂件fifile1⾥⾯空格开始的所在的⾏号?

 答:grep -nE "^[[:space:]]+" fifile1

 

4、统计/etc/fstab⽂件中每个单词出现的次数?

答:

[root@centos8 data]#grep -Eo "<[[:alnum:]_]+>" /etc/fstab | sort | uniq -c | sort -rn
      6 0
      4 UUID
      4 defaults
      3 8
      2 xfs
    ...

5、如何查看fifile1⽂件的第300到500⾏的内容?

 答:head -n 500 fifile1 | tail -n 200

 

6、shell 脚本编程的主要应用范围有哪些?

 答:1、自动化常用命令

    2、执行系统管理和故障排除

    3、创建简单的应用程序

    4、处理文件或文本

 

7、 shell 脚本文件的第一行中 #!/bin/bash 的作用是什么?

 答:说明该脚本使用的解释器

 

8、编写脚本 hostping.sh,接受一个主机的 IPv4 地址做为参数,测试是否可连通。如果能 ping 通,则提示用户“该IP地址可访问”;如果不可 ping 通,则提示用户“该IP地址不可访问”。

答:

#!/bin/bash

RED='e[1;31m'
GREEN='e[1;32m'
END='e[0m'

#判断参数个数
[ "$#" -ne 1 ] && echo -e "${RED}Need one argument,and only one${END}" && exit 1

#判断IP地址格式
[[ $1 =~ (([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$ ]] || { echo -e "${RED}Wrong IP Address format${END}";exit 2; }

echo -e "Start ping $1"
sleep 3
ping -c1 -W1 $1 &> /dev/null && echo -e "${GREEN}$1 can be access${END}" || echo -e "${RED}$1 can not be access${END}"

原文地址:https://www.cnblogs.com/jojohyj/p/13065192.html