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 $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