一些简单的shell脚本题目练习
1.查找当前网段(10.1.1.0/24)内存活IP用户,并输出存活用户,重定向到/tmp/ip.txt文件中(ping)
#!/bin/bash
touch /tmp/ip.txt
for i in {1..254}; do(
ping -c 1 -W 1 10.1.1.$i > /dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "$i is alive"
echo "10.1.1.$i" >> /tmp/ip.txt
else
echo "$i is dead"
fi
)
done
2.自动创建用户student101-150,且密码为password101-150
#!/bin/bash
for i in {101..150}
do
useradd student$i
echo "passwd$i" | passwd --stdin student$i
done
3.根据passwd文件内容,输出“The line 1(行数) is root(用户名)”依次类推
#!/bin/bash
n=0
while read line
do
let n++
user=`echo $line | cut -d: -f1`
echo "The line$n is $user"
done < /etc/passwd
4.写一个脚本,显示当前系统上shell为-s指定类型的shell,并统计其用户总数。-s选项后面跟的参数必须是/etc/shells文件中存在的shell类型,否则不执行此脚本。另外,此脚本还可以接受--help选项,以显示帮助信息。
例:
./test8.sh -s bash
bash,3users,they are:
root redhat gentoo
#!/bin/bash
if [ $# -eq 2 ] ; then
case $1 in
-s)
type=`grep "$2" /etc/shells | cut -d/ -f3`
count=`grep "$2$" /etc/passwd | wc -l`
user=`grep "$2$" /etc/passwd | cut -d: -f1`
echo "$type,$count,they are:
$user"
;;
--help)
case $2 in
me)
echo "
hello!
this script is to calculate bash user and show message
option:
--help me: see the manual of use
-s [bash type]: show the bash user
"
;;
*)
echo "
wrong option,please type '--help me' to look for help
"
;;
esac
;;
*)
echo "
wrong option,please type '--help me' to look for help
"
;;
esac
fi
5.ldd是用来查看二进制程序的共享库文件的,现在通过脚本实现把库文件自动的复制到固定目录(/newroot)的相对应的目录中,如源库文件:/lib/a.so.1,应复制到/newroot/lib/a.so.1
#!/bin/bash
read -p "input command: " command
NEWroot="/newroot"
ldd `which $command` | grep '/' | grep -P -o '/lib(64){0,1}[^\s]{0,}' |
while read line;do
NEWdoc=$line
NEWdir=${line%/*}
mkdir -p $NEWroot$NEWdir
cp -r $NEWdoc $NEWroot$NEWdir
echo "cp $NEWdir$line is done"
done
结果
[root 16:40:54@localhost sbin]$ (1) cpldd.sh
input command: ping
cp /lib64/lib64/libcap.so.2 is done
cp /lib64/lib64/libidn.so.11 is done
cp /lib64/lib64/libcrypto.so.10 is done
cp /lib64/lib64/libresolv.so.2 is done
cp /lib64/lib64/libm.so.6 is done
cp /lib64/lib64/libc.so.6 is done
cp /lib64/lib64/libattr.so.1 is done
cp /lib64/lib64/libdl.so.2 is done
cp /lib64/lib64/libz.so.1 is done
cp /lib64/lib64/ld-linux-x86-64.so.2 is done
[root 16:41:01@localhost sbin]$ (2) ll /newroot
total 4
drwxr-xr-x. 2 root root 4096 Jan 13 16:41 lib64
[root 16:41:07@localhost sbin]$ (3) ll /newroot/lib64
total 0
lrwxrwxrwx. 1 root root 10 Jan 13 16:41 ld-linux-x86-64.so.2 -> ld-2.17.so
lrwxrwxrwx. 1 root root 16 Jan 13 16:41 libattr.so.1 -> libattr.so.1.1.0
lrwxrwxrwx. 1 root root 14 Jan 13 16:41 libcap.so.2 -> libcap.so.2.22
lrwxrwxrwx. 1 root root 19 Jan 13 16:41 libcrypto.so.10 -> libcrypto.so.1.0.2k
lrwxrwxrwx. 1 root root 12 Jan 13 16:41 libc.so.6 -> libc-2.17.so
lrwxrwxrwx. 1 root root 13 Jan 13 16:41 libdl.so.2 -> libdl-2.17.so
lrwxrwxrwx. 1 root root 17 Jan 13 16:41 libidn.so.11 -> libidn.so.11.6.11
lrwxrwxrwx. 1 root root 12 Jan 13 16:41 libm.so.6 -> libm-2.17.so
lrwxrwxrwx. 1 root root 17 Jan 13 16:41 libresolv.so.2 -> libresolv-2.17.so
lrwxrwxrwx. 1 root root 13 Jan 13 16:41 libz.so.1 -> libz.so.1.2.7
浙公网安备 33010602011771号