马哥博客作业第二周

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

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

 

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

 

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

grep -n " " test.txt | head -n1 | cut -d: -f1

 

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

grep -E -o "<[[:alpha:]]*>" /etc/fstab | sort | uniq -c

 

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

head -n500 fifile1 | tail -n201

 

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

自动化常用命令;执行系统管理和故障排除;创建简单的应用程序;处理文本或文件

 

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

 告知系统此脚本用/bin/bash这个shell来运行

 

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

#! /usr/bin/bash
#
#*********************************
#FileName      hostping.sh
#Author        GeHaiBao
#Date          2020-06-07
#*********************************

set -ue

#whether the input is an IPv4 address or not
[[ $1 =~ ^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$ ]] &&
echo "Ping start..." || { echo "Please input an IPv4 address! ";exit; }

#ping the address and set 10 secs timeout
ping -c1 -w10 $1 &> /dev/null && echo "$1 可以访问" || echo "$1 不可以访问"
echo finished

 

 

原文地址:https://www.cnblogs.com/gehaibao/p/13057497.html