shell脚本编程练习
转至:http://www.178linux.com/88406
1、写一个脚本,使用ping命令探测172.16.250.1-172.16.250.254之间的所有主机的在线状态
在线的主机使用绿色显示
不在线的主使用红色显示
#!/bin/bash
#
for i in {1..254};do
if ping -c 6 -w 1 192.168.1.$i &> /dev/null;then
echo -e “\033[32m 192.168.1.$i \033[0m is up”
else
echo -e “\033[31m 192.168.1.$i \033[0m is down”
fi
done
2、如何给网络接口配置多个地址,有哪些方式?
- a) 使用ifconfig命令
例如 ifconfig eno16777736:0 192.168.0.100/24
- b) 使用Ip addr命令
例如 ip addr add 192.168.0.101/24 dev eno16777736
- c) 通过配置文件/etc/sysconfig/network-scripts/ifcfg-IFACE来识别接口并完成配置;
- d) 使用nmtui命令
3、写一个脚本,完成以下功能
(1) 假设某目录(/etc/rc.d/rc3.d/)下分别有K开头的文件和S开头的文件若干
(2) 显示所有以K开头的文件的文件名,并且给其附加一个stop字符串
(3) 显示所有以S开头的文件的文件名,并且给其附加一个start字符串
(4) 分别统计S开头和K开头的文件各有多少
#!/bin/bash
#
declare -i n=0
declare -i m=0
for i in $(ls /etc/rc.d/rc3.d); do
if [ $(echo $i | cut -c 1) == “K” ]; then
echo “$i stop”
let n++
elif [ $(echo $i | cut -c 1) == “S” ]; then
echo “$i start”
let m++
fi
done
echo “K is $n S is $m.”
4、写一个脚本,完成以下功能
(1) 脚本能接受用户名作为参数
(2) 计算此些用户的ID之和
#!/bin/bash
#
declare -i sum=0
for i in $@; do
if ! id $i &> /dev/null; then
echo “$i is not user”
else
let sum=$sum+$(id -u $i)
fi
done
echo “uid sum = $sum”
5、写一个脚本
(1) 传递一些目录给此脚本
(2) 逐个显示每个目录的所有一级文件或子目录的内容类型
(3) 统计一共有多少个目录;且一共显示了多少个文件的内容类型
#!/bin/bash
#
declare -i m=0
declare -i n=0
for i in $@;do
if [ -d $i ];then
for x in $i/*;do
echo $x
let m++
if [ -d $x ];then
echo $x
let n++
fi
done
else
echo “$i is not a dir or not file”
fi
done
echo ” dir = $n file = $m ”
6、写一个脚本
通过命令行传递一个参数给脚本,参数为用户名
如果用户的id号大于等于500,则显示此用户为普通用户
#!/bin/bash
#
uname=$1
if id $uname &> /dev/null ;then
if [ $(id -u $uname) -ge 500 ];then
echo “this is a normal user”
else
echo “Not an ordinary user”
fi
else
echo “not a user”
fi
7、写一脚本,用ping命令测试172.16.250.20-172.16.250.100以内有哪些主机在线,将在线的显示出来
#!/bin/bash
#
for i in {20..100};do
if ping -c 1 172.16.250.$i &> /dev/null;then
echo “172.16.250.$i is online”
fi
done
8、打印九九乘法表
#!/bin/bash
#
for i in ‘seq 1 9’;do
for j in ‘seq 1 $i’;do
echo -n -e “${j}X${i}=$[$j*$i]\t”
done
echo
done
shell脚本编程练习的更多相关文章
- Shell脚本编程30分钟入门
Shell脚本编程30分钟入门 转载地址: Shell脚本编程30分钟入门 什么是Shell脚本 示例 看个例子吧: #!/bin/sh cd ~ mkdir shell_tut cd shell_t ...
- Linux shell脚本编程(三)
Linux shell脚本编程 流程控制: 循环语句:for,while,until while循环: while CONDITION; do 循环体 done 进入条件:当CONDITION为“真” ...
- Linux shell脚本编程(二)
Linux shell脚本编程(二) 练习:求100以内所有偶数之和; 使用至少三种方法实现; 示例1: #!/bin/bash # declare -i sum=0 #声明一个变量求和,初始值为0 ...
- Linux shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- Linux shell脚本编程基础之练习篇
shell脚本编程基础之练习篇. 1.编写一个脚本使我们在写一个脚本时自动生成”#!/bin/bash”这一行和注释信息. #!/bin/bash ] then echo "请输入一个参数& ...
- 【Linux】Shell脚本编程(一)
Linux shell脚本编程: 守护进程,服务进程:启动?开机时自动启动: 交互式进程:shell应用程序 广义:GUI,CLI GUI: CLI: 词法分析:命令,选项,参数 内建命令: 外部命令 ...
- 学习笔记之Linux Shell脚本教程:30分钟玩转Shell脚本编程
Linux Shell脚本教程:30分钟玩转Shell脚本编程 http://c.biancheng.net/cpp/shell/
- Shell脚本编程总结及速查手册
Shell是一种编程语言, 它像其它编程语言如: C, Java, Python等一样也有变量/函数/运算符/if语句/循环控制/… 但在开始之前, 我想先理清Shell语言与Shell之间的关系. ...
- 关于shell脚本编程的10个最佳实践
每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.但大家解决问题的方式却不尽相同,这要取决于对专业知识的掌握程度.使 用命令的种类.看待问题的方式等等.对于那些处在shell脚本 ...
- 《Linux命令行与shell脚本编程大全》 第二十七章 学习笔记
第二十七章:shell脚本编程进阶 监测系统统计数据 系统快照报告 1.运行时间 uptime命令会提供以下基本信息: 当前时间 系统运行的天数,小时数,分钟数 当前登录到系统的用户数 1分钟,5分钟 ...
随机推荐
- C++中的const和mutable
1 #include<iostream> 2 using namespace std; 3 //如果在类A的成员函数dis()中想要修改_z,但是不能修改_x,_y怎么办? 4 //如果d ...
- 字符串自实现(一)(mystrcpy,mystrcat,mystrcmp)
char* mystrcpy(char* str_one,const char* str_two) { char* tmp = str_one; while (*str_one++ = *str_tw ...
- 免费注册香港Apple ID
注册 一.海外Apple ID的好处 1.APP软件资源多,比如传说对决(海外版王者荣耀).海外版抖音等,这些APP软件在国内的apple store是没有的: 2.部分APP软件在海外Apple s ...
- Core 3.1 MVC 抛异常“InvalidOperationException: No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered.”
.NET Core 的版本是 3.1遇到的问题是 Action 中 return View() 的时候报错 An unhandled exception occurred while processi ...
- 导入 static 修饰的包
一 static关键字,可以修饰变量 方法 代码块 , 静态内部类. 还可以用来修饰 需要导入的包 准备工作 package zhouxufeng; public class Text1 { ...
- Cause: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 解决
感谢大佬:https://blog.csdn.net/a704397849/article/details/93797529 springboot + mybatis多数据库 + druid连接池配置 ...
- 常用汉字大全:汉字读音表GB2312版(共7809个汉字)
转载请注明来源:https://www.cnblogs.com/hookjc/ 常用汉字:a1:阿啊呵腌吖锕a2:啊呵嗄a3:啊呵a4:啊呵ai1:哀挨埃唉哎捱锿ai2:呆挨癌皑捱ai3:矮哎蔼霭嗳a ...
- ittun.com的使用方法
[如果这篇文章对你有所作用,请加关注哦!] 步骤一: 进入官网http://ittun.com/ Windows 64位下载http://ittun.com/upload/17.2/ittun_win ...
- MySQL高级(进阶)SQL语句
MySQL高级(进阶)SQL语句 目录 MySQL高级(进阶)SQL语句 一.实例准备--制表 1. 表1(商店区域表) 2. 表2(商店销售表) 3. 表3(城市表) 4. 表4(total_sal ...
- go基础——基本数据类型
GO语言的数据类型: /* GO语言的数据类型: 1.基本数据类型: 布尔类型:true,false 数值类型:整数,浮点,复数complex 字符串:string 2.复合数据类型 array,sl ...