第八周-Shell脚本编程

1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态;

在线的主机使用绿色显示;

不在线的主使用红色显示;

#!/bin/bash

for i in {1..254};
do
{
  ip=172.16.250.$i
  if ping -c 1 -w 1 $ip &> /dev/null ; then
    echo -e "\033[32;49;1m$ip\033[39;49;0m\n"
  else
    echo -e "\033[31;49;1m$ip\033[39;49;0m\n"
  fi
} &
done
wait

2、如何给网络接口配置多个地址,有哪些

(1)、使用ifconfig命令

ifconfig eth0:0 192.168.1.200 up

(2)、使用ip命令

ip addr add 192.168.1.200/24 dev eth0

(3)、新建子接口配置文件(永久性)

vim /etc/sysconfig/network-scripts/ifcfg-eth0:0
添加以下配置:
DEVICE=eth0:0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.1.200
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
保存后启用此接口
ifup eth0:0

3、写一个脚本,完成以下功能

(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干;

(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串;

(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串;

(4) 分别统计S开头和K开头的文件各有多少;

#!/bin/bash
S=0
K=0

for i in `ls /etc/rc.d/rc3.d/`;do
case $i in
  S*)
 echo "$i start"
 S=$[S+1]
  ;;
  K*)
 echo "$i stop"
 K=$[K+1]
  ;;
esac
done
echo -e "S $S Files\nK $K Files"

4、写一个脚本,完成以下功能

(1) 脚本能接受用户名作为参数;

(2) 计算此些用户的ID之和;

!/bin/bash

sum=0
for i in $@;do
 if id $i &>/dev/null ;then
    sum=$[sum+`id -u $i`]
 else
    echo  "User $i is no existed"
 fi
done
echo "Users id sum is $sum"

5、写一个脚本

(1) 传递一些目录给此脚本;

(2) 逐个显示每个目录的所有一级文件或子目录的内容类型;

(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型;

#!/bin/bash
dir=0
files=0
for i in $@;do
 file $i/*
  for e in `ls $i`;do
     if [ -d $i/$e ];then
       dir=$[dir+1]
     fi
     files=$[files+1]
  done
done
echo " dir = $dir ,all file type= $files "

6、写一个脚本

通过命令行传递一个参数给脚本,参数为用户名

如果用户的id号大于等于500,则显示此用户为普通用户

#!/bin/bash

for i in $@;do
   if ! id $i &>/dev/null;then
      echo "user $i no exist"
   elif [ `id -u $i` -ge 500 ];then
      echo "$i is general user"
   fi
done

7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来;

#!/bin/bash
for i in {20..100};
do
{
  ip=172.16.250.$i
  if ping -c 1 -w 1 $ip &> /dev/null ; then
    echo -e "\033[32;49;1m$ip\033[39;49;0m\n"
  fi
} &
done
wait

8、打印九九乘法表;

#!/bin/bash

for i in  `seq 9`;do
         for j in `seq 1 $i`;do
         echo -n "$i*$j= `echo $(($i*$j))`      "
         done
         echo
done

原创文章,作者:N27_what,如若转载,请注明出处:http://www.178linux.com/85294

(0)
N27_whatN27_what
上一篇 2017-08-23 16:21
下一篇 2017-08-23 17:20

相关推荐

  • sed– 用于筛选和转换文本的流编辑器

    sed 用于筛选和转换文本的流编辑器命令格式:sed [OPTION] {script} file选项       -n, –quiet, –silent 抑制模式空间的自动打印  echo -e “abc\ndef” | sed ‘p’ #输出 abc # abc # def # d…

    Linux干货 2017-08-15
  • httpd-2.2和httpd-2.4区别 、请求方法和响应状态码、虚拟主机、访问控制、持久链接

    第十周
    The Apache HTTP Server is a powerful, efficient, and extensible web server.
    2018/2/4 16:33

    2018-02-04
  • 一个删除MySQL大表数据的shell脚本

    #!/bin/bash #为了删除一些特别大的表 dbname=''  #库名 tabname=''  #表名 step='10000' #删除步长 sleeptime=1  #睡眠时间 start_index=30000001 &…

    系统运维 2015-07-16
  • Linux文件管理类命令相关

    Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示。 1)、查看文件命令: (1)ls命令: list,列出目录下的内容 语法: ls [OPTION]… [FILE]… 常用选项: -a: 显示所有文件,包括隐藏文件; -A:显示除.和..之外的所有文件; -l: –long, 长格式列表,即显示文件的详细属性信息; -h, –huma…

    Linux干货 2016-11-06
  • CentOS7编译安装LAMP—php-fpm

    inux的环境是: [root@localhost ~]# lsb_release -a LSB Version:     :core-4.1-amd64:core-4.1-noarch Distributor ID: CentOS Description:     CentOS…

    Linux干货 2016-12-21
  • Linux三剑客之grep

    grep(Globel Search Regular Expression and Printing out the line)全面搜索正则表达式并把行打印出来)是一个强大的文本搜索工具,使用正则表达式搜索文本的文本,并把结果打印出来。Unix家族包括grep、egrep和fgrep。egrep是扩张的正则表达式它支持更多的字符,fgrep是fast gre…

    Linux干货 2016-08-12