1.传给脚本一个参数:目录,输出该目录中文件最大的,文件名和文件大小

#!/bin/bash
if [ $# -ne 1 -o ! -d $1 ];then
echo "Args is error."
exit 3
fi LC=`du -a $1 | wc -l`
for I in `seq $LC` ;do
size=`du -a $1 | sort -nr | head -$I | tail -1 | awk '{print $1}'`
filename=`du -a $1 | sort -nr | head -$I | tail -1 | awk '{print $2}'`
if [ -f $filename ];then
echo "The big file is $filename ,Size is $size."
break
fi
done

2.查询当前192.168.1.x网段内,那些IP被使用了,输出这些IP到一个文件中。

#!/bin/bash

for I in {1..254};do
N=`ping -c 1 192.168.1.$I | awk -F, '/received/ {print $2}' | cut -d' ' -f2`
[ $N -eq 1 ] && echo "192.168.1.$I" >>/tmp/ips.txt
done

3.把shell为bash的用户输出到/tmp/users.txt中

#!/bin/bash
LC=`wc -l /etc/passwd | awk '{print $1}'`
N=1
for I in `seq $LC` ;do
SHE=`head -$I /etc/passwd | tail -1 | cut -d: -f7`
U=`head -$I /etc/passwd | tail -1 | cut -d: -f1`
if [ $SHE == '/bin/bash' ];then
echo -e "$N \t $U" >> /tmp/users.txt
N=$[$N+1]
fi
done

4.依次向/etc/passwd中的每个用户问好,输出用户名和id,并统计一共有多少个用户

(1)写法一

#!/bin/bash
file="/etc/passwd"
LINES=`wc -l $file | cut -d" " -f1`
for I in `seq 1 $LINES`;do
userid=`head -$I $file | tail -1 |cut -d: -f3`
username=`head -$I $file | tail -1 |cut -d: -f1`
echo "hello $username,your UID is $userid"
done
echo "there are $LINES users"

(2).写法二

#!/bin/bash
file=/etc/passwd
let num=0
for I in `cat $file`;do
username=`echo "$I" | cut -d: -f1`
userid=`echo "$I" | cut -d: -f3`
echo "Hello,$username,your UID is $userid"
num=$[$num+1]
done
echo "there are $num users"

5.切换工作目录到/var ,依次向/var 目录中每个文件或子目录问好(形如:Hello,log),并统计/var目录下共有多少个文件显示出来

(提示:for File in /var/*;或 for File in 'ls /var ')

#!/bin/bash
cd /var
let num=0
for I in `ls /var/*`;do
echo "hello $I"
num=$[$num+1]
done
echo "the number of files is $num"

6.循环读取文件/etc/passwd的第2,4,6,10,13,15行,并显示其内容,然后把这些行保存到/tmp/mypasswd文件中

#!/bin/bash
file="/etc/passwd"
for I in 2 4 6 10 13 15;do
exec 3>/tmp/mypasswd
line=`head -$I $file | tail -1`
echo "$line"
echo "$line" >&3
exec 3>&-
done

7,写一个脚本

  (1)显示当前系统日期和时间,而后创建目录/tmp/lstest

  (2)切换工作目录至/tmp/lstest

(3)创建目录a1d,b56e,6test

(4)创建空文件xy,x2y,732

(5)列出当前目录下以a,x或者6开头的文件或目录

(6)列出当前目录下以字母开头,后跟一个任意数字,而后跟任意长度字符的文件或目录 

  

#!/bin/bash

date
mkdir -pv /tmp/lstest
cd /tmp/lstest
mkdir a1d b56e 6test
touch xy x2y 732
ls [ax6]*
ls [[:alpha:]][[:digit:]]*

8.添加10个用户user1到user10,但要求只有用户不存在的情况下才能添加

 #!/bin/bash
for I in `seq 1 10`;do
cut -d: -f1 /etc/passwd |grep "user$I" 2>>/tmp/etc.err || useradd user$I
done

9.通过ping命令测试192.168.0.151到192.168.0.254之间的所有主机是否在线
      
如果在线,就显示“ip is up”
      
如果不在线,就显示“ip is down”  

#!/bin/bash
for I in `seq 151 254`;do
ping -c1 -w1 192.168.0.$I &>/dev/null && echo "192.168.0.$I is up" || echo "192.168.0.$I is down"
done

10.zookeeper一个节点通过脚本启动所有的节点

#!/bin/bash

if [ $# -ne 1 ]
then
echo "please give a parameter to tell me what to do for example [start|stop|restart|status]" && exit 1
fi
echo "$1 zkServer in every node......" list=`cat /usr/local/zookeeper-3.4.6/conf/zoo.cfg | grep server | awk -F : '{print $1}' | awk -F '=' '{print $2}'` for i in $list
do
echo "$1 zkServer in $i......"
ssh $i "/usr/local/zookeeper-3.4.6/bin/zkServer.sh $1" &> /dev/null
done for i in $list
do
echo "show zkServer status in $i......"
ssh $i "/usr/local/zookeeper-3.4.6/bin/zkServer.sh status"
done

  

 

  

  

  

  

  

shell脚本实例(2)的更多相关文章

  1. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

  2. shell脚本实例,通向shell脚本大师的必经之路

    概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...

  3. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  4. shell脚本实例

    备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...

  5. shell脚本实例一,移动文件夹中大于2000B的文件到另一个文件夹

    shell脚本能帮我们简化linux下的一些工作,现在有个需求,把TMPA文件夹下大于2000B的文件都移动到TMPB下 #! /bin/bash function movefiles() { ` d ...

  6. shell脚本实例一

    一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim  test.sh     ##shell脚本一般都 ...

  7. shell脚本实例-mysql多机部署

    今天我给大家分享shell 安装mysql 多机部署的实例,本次实验是基于各个主机的公钥已经配置好了,如果还不会推送公钥的同学,可以看看我以前写的文章,那里面有写推公钥的实例,mysql 多机部署一般 ...

  8. shell脚本实例-菜单样例

    1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...

  9. 【shell脚本实例】一个恶作剧—— kill掉占用CPU较高的matlab进程

    我们实验室有台服务器,博士们在服务器上跑MATLAB,基本都是4核都是超过95%的CPU占用,想了个恶作剧的shell 定时kill掉MATLAB程序,是不是很邪恶啊,哈哈~~~  不过我只是干过一次 ...

随机推荐

  1. dataStructure@ Binary Search Tree

    #include<iostream> #include<cstdio> #include<cstring> #include<limits> #incl ...

  2. 【noip2007】树网的核

    题解: 首先我们要知道一个性质:如果有多条直径 这个核不论在哪条直径上 答案都是一样的 这样我们就可以随便找一条直径 在这条直径上枚举核的位置 并且dfs预处理maxlon[i] (i在直径上) 表示 ...

  3. jqgrid使用sql row_number进行分页

    背景 系统中使用了jqgrid的展示,现在要处理10w+的数据量 现状 使用了全查询的,查询到了10w+的数据放到了datatable中,每次页面刷新需要9秒多,并且传递给另一个dll来处理一些事情. ...

  4. Java中的IP对象以及本地域名解析

    本地域名解析操作步骤: 1.打开C:\WINDOWS\system32\drivers\etc目录 2.找到host文件,用记事本打开 3.添加“空间IP  域名” package WebProgra ...

  5. Umbraco中的ModelBuilder

    Umbraco中的ModelBuilder有以下几种形式 Pure Live models Dll models LiveDll models AppData models LiveAppData m ...

  6. 类型检测汇总!typeof 和 instanceof 和isArray

    , ]; alert(arr instanceof Array);//true 以上老方法判断是否是数组,存在一个问题,就是它只适用于单执行环境(窗口),如果该窗口有其他框架(比如 iframe)则会 ...

  7. ZZTHX-Androidannotations框架联想

    我们首先来看一段代码: 在android开发中findViewById是最常用的一个方法,用来实例化页面上的控件,基本上每个控件都需要调用一次的,加入我们页面上有100个需要使用,那么findView ...

  8. CentOS 6.5 下载地址

    CentOS 6.5 主要改动 Precision Time Protocol(精确时间协议)—— 原先是项技术预览 —— 现在已获全面支持.以下驱动程序支持网络时间戳印:bnx2x.tg3.e100 ...

  9. JQuery.Ajax之错误调试帮助信息介绍

    下面是Jquery中AJAX参数详细列表: timeout Number 设置请求超时时间(毫秒).此设置将覆盖全局设置. async Boolean (默认: true) 默认设置下,所有请求均为异 ...

  10. python会什么比c慢

    众所周知,python执行速度比c慢.原因为何? 先来看下面这张图: python的传统运行执行模式:录入的源代码转换为字节码,之后字节码在python虚拟机中运行.代码自动被编译,之后再解释成机器码 ...