简单SHELL脚本练习

1.查找当前网段(10.1.1.0/24)内存活IP用户,重定向到/tmp/ip.txt文件中(ping)

#!/bin/bash
#查找当前网段(10.1.1.0/24)内存活IP用户,重定向到/tmp/ip.txt文件中(ping)
for i in {1..254};do (		#将括号中的代码放入子shell执行
	ping -c1 -w1 10.1.1.$i > /dev/null 2>&1
	if [ $? -eq 0 ];then
		echo "10.0.1.$i is alive" >> /tmp/ip.txt
		echo "10.0.1$i is alive"
	else 
		echo "10.0.1.$i is dead" > /dev/null 2>&1
	fi
	)
done

2.自动创建用户student101-150,且密码为password101-150

#!/bin/bash
#自动创建用户student101-150,且密码为password101-150
for i in {101..150};do
	id student$i > /dev/null 2>&1
	if [ $? -ne 0 ];then
		useradd student$i
		echo "password$i" > passwd --stdin student$i 
	fi
done
echo "用户创建完成"

运行结果(部分截图):

[root 15:48:52@fancysummer homework]$ (84) ./2.sh 
用户创建完成
[root 15:48:56@fancysummer homework]$ (85) cat /etc/passwd | grep 'student'
student101:x:1009:1010::/home/student101:/bin/bash
student102:x:1010:1011::/home/student102:/bin/bash
student103:x:1011:1012::/home/student103:/bin/bash
student104:x:1012:1013::/home/student104:/bin/bash
student105:x:1013:1014::/home/student105:/bin/bash
student106:x:1014:1015::/home/student106:/bin/bash
student107:x:1015:1016::/home/student107:/bin/bash
student108:x:1016:1017::/home/student108:/bin/bash
student109:x:1017:1018::/home/student109:/bin/bash
student110:x:1018:1019::/home/student110:/bin/bash

3.根据passwd文件内容,输出“The line 1(行数) is root(用户名)”依次类推

#!/bin/bash
#根据passwd文件内容,输出“The line 1(行数) is root(用户名)”依次类推
n=1
while read line;do
	user=`echo $line | awk -F: '{print $1}'`
	echo "the line$n is $user"	
	let n++
done < /etc/passwd

运行结果:

[root 12:02:27@fancysummer homework]$ (38) ./4.sh 
the line1 is root
the line2 is bin
the line3 is daemon
the line4 is adm
the line5 is lp
the line6 is sync
the line7 is shutdown
the line8 is halt
the line9 is mail
the line10 is operator
...

4.写一个脚本,显示当前系统上shell为-s指定类型的shell,并统计其用户总数。-s选 项后面跟的参数必须是/etc/shells文件中存在的shell类型,否则不执行此脚本。另 外,此脚本还可以接受–help选项,以显示帮助信息。执行及显示结果如下: # ./test8.sh -s bash bash,3users,they are: root alice bob

#!/bin/bash
#显示结果形如:
#./test7.sh -s bash
#bash,3users,they are:
#root redhat gentoo

case $1 in 
	-h|--help)
		echo "USAGE:./test7.sh [-h|--help] [-s shell_name]"  
	;;
	-s)
		shell=$2
		shell_flag=`grep "\b$shell\b" /etc/shells`
		if [ -n "$shell_flag" ];then
			user_number=`grep "\b$shell\b" /etc/passwd | wc -l`
			users=`grep "\b$shell\b" /etc/passwd | awk -F: '{print $1}'`
			echo  $shell,$user_number users,they are $users
		else
			echo "WARNING:no these shell in /etc/shells!"
			exit 1
		fi	
	;;
	*)
		echo "wrong arguments,use --help"
		exit 1
	;;
esac

运行结果:

[root 15:39:28@fancysummer homework]$ (78) ./7.sh -h 
USAGE:./test7.sh [-h|--help] [-s shell_name]
[root 15:39:34@fancysummer homework]$ (79) ./7.sh -s bash
bash,10 users,they are root yyk YYKK bash testbash basher user1 k1 k2 k3

5.ldd是用来查看二进制程序的共享库文件的,现在通过脚本实现把库文件自动的复制 到固定目录(/newroot)的相对应的目录中,如源库文件:/lib/a.so.1,应复制 到/newroot/lib/a.so.1

#!/bin/bash

#ldd是用来查看二进制程序的共享库文件的,现在通过脚本实现把库文件自动的复制到固定目录(/newroot)的相对应的目录中,如源库文件:/lib/a.so.1,应复制到/newroot/lib/a.so.1

read -p"输入一个命令" commands
NEWROOT="/newroot"
#复制命令
cpbin(){
	BPATH=`which $commands | grep '/'`
	BDIR=${BPATH%/*}
	[ ! -d $NEWROOT$BDIR ] && mkdir -p $NEWROOT$BDIR
	[ -e $BPATH ] && cp -r $BPATH $NEWROOT$BDIR
}
#复制库文件
cplib(){
	ldd `which $commands | grep '/'` | grep -P -o '/lib(64){0,1}[^\s]{0,}' | while read line;do
		LPATH=$line
		LDIR=${line%/*}
		[ -d "$LDIR" ] && mkdir -p $NEWROOT$LDIR
		[ -e $LPATH ] && cp -r $LPATH $NEWROOT$LDIR
		echo "cp $LPATH $NEWROOT$LDIR finish"
	done	
}
cpbin
cplib

运行结果:

[root 15:02:20@fancysummer homework]$ (18) ./10.sh
输入一个命令grep
cp /lib64/libpcre.so.1 /newroot
cp /lib64/libc.so.6 /newroot
cp /lib64/libpthread.so.0 /newroot
cp /lib64/ld-linux-x86-64.so.2 /newroot
[root 15:08:51@fancysummer /]$ (35) tree /newroot
/newroot
├── lib64
│	 ├── ld-linux-x86-64.so.2 -> ld-2.17.so
│	 ├── libc.so.6 -> libc-2.17.so
│	 ├── libpcre.so.1 -> libpcre.so.1.2.0
│	└── libpthread.so.0 -> libpthread-2.17.so
└── usr
    └── bin
        └── grep

3 directories, 5 files
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值