shell脚本实例(2)
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)的更多相关文章
- 分享7个shell脚本实例--shell脚本练习必备
概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...
- shell脚本实例,通向shell脚本大师的必经之路
概述 读书百遍其义自见,shell脚本也是,只要例子看得多了,自然就知道怎么写了.这里主要整理了20几个例子,因为内容比较多,所以分了几次来做介绍了.下面的实例最好先自己思考怎么去实现,然后再看下实现 ...
- shell脚本实例-系统监控
shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...
- shell脚本实例
备注:一些与传递给shell的参数相关的变量:$# 命令行参数的个数$? 调用命令的返回值$$ 当前进程的进程号$! 最后一个后台命令的进程号$0 命令行的第一个参数,也就是命令名$n 命令行的第n个 ...
- shell脚本实例一,移动文件夹中大于2000B的文件到另一个文件夹
shell脚本能帮我们简化linux下的一些工作,现在有个需求,把TMPA文件夹下大于2000B的文件都移动到TMPB下 #! /bin/bash function movefiles() { ` d ...
- shell脚本实例一
一. 什么是shell 脚本时一种解释性语言: shell脚本保存执行动作: 脚本判定命令的执行条件 脚本来实现动作的批量执行.二.如何创建 vim test.sh ##shell脚本一般都 ...
- shell脚本实例-mysql多机部署
今天我给大家分享shell 安装mysql 多机部署的实例,本次实验是基于各个主机的公钥已经配置好了,如果还不会推送公钥的同学,可以看看我以前写的文章,那里面有写推公钥的实例,mysql 多机部署一般 ...
- shell脚本实例-菜单样例
1.9.1 实例需求 用户在进行Linux系统管理的过程中,经常需要用到查看进程的信息.用户的信息等常用的功能.本例针对这一需求,使用shell编程实现基本的系统管理 功能.通过本程序,可以按照要求实 ...
- 【shell脚本实例】一个恶作剧—— kill掉占用CPU较高的matlab进程
我们实验室有台服务器,博士们在服务器上跑MATLAB,基本都是4核都是超过95%的CPU占用,想了个恶作剧的shell 定时kill掉MATLAB程序,是不是很邪恶啊,哈哈~~~ 不过我只是干过一次 ...
随机推荐
- hdu 1491 Octorber 21st
Octorber 21st Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdoj 1856 More is better【求树的节点数】
More is better Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 327680/102400 K (Java/Others) ...
- hdoj 2120 Ice_cream's world I【求成环数】
Ice_cream's world I Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- css控制内容显示,自动加"..."
.css{ overflow:hidden; white-space:nowrap; text-overflow:ellipsis; width:100px; } c ...
- like用法
SQL:btitle like '%"+keyword+"%' 存储过程:keyword like ''%'+@keyword+'%'' 直接查找:name like '%wang ...
- rank() over(partition)的使用
有的时候会遇到这样的问题,我们需要查询一张表,而且要按照业务排序,比如我需要如下的结果: 地区 日期 费用 产品编号 用户编号 290 201202 258 1 ...
- Activity的生命周期,BACK键和HOME键生命周期
Activity的生命周期模型在Google提供的官方文档上有比较详细的一个图示 public class HelloActivity extends Activity { public static ...
- [每日一题] 11gOCP 1z0-053 :2013-10-12 RESULT_CACHE在哪个池?.............................44
转载请注明出处:http://blog.csdn.net/guoyjoe/article/details/12657479 正确答案:B Oracle 11g 新特性:Result Cache , ...
- hdu 4104
先排序,再动态规划.须要优化 #include<iostream> #include<cstdio> #include<cstring> #include<s ...
- java_spring_实例化bean的3种方法
//Dao类 package com.dao.bean.www; public interface PersonServiceDao { public abstract void save(); } ...