Linux脚本
链接地址:http://blog.51cto.com/lyjbog/1622941
http://blog.51cto.com/lyjbog/1623447
脚本01.sh:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello, root, your shell: /bin/bash
3、统计一共有多少个用户
#!/bin/bash
#
FILE=/etc/passwd
UNAMES=`cut -d ":" -f 1 /etc/passwd`
SHELLS=`cut -d ":" -f 7 /etc/passwd`
NUM="echo $UNAMES | wc -w"
#echo $UNAMES
#echo $SHELLS
#echo $NUM
for I in `seq 1 $NUM`;do
UNAME=`echo $UNAMES | cut -d " " -f $I`
SHELL=`echo $SHELLS | cut -d " " -f $I`
echo "Hello $UNAME,your shell is $SHELL"
done
echo "total $NUM users."
脚本02.sh:
添加10个用户user1到user10,密码同用户名;但要求只有用户不存在的情况下才能添加;
扩展:
接受一个参数:
add: 添加用户user1..user10
del: 删除用户user1..user10
#!/bin/bash
#
case $1 in
--add|-a)
for I in `seq 1 10`;do
if ! id user$I &> /dev/null;then
useradd user$I &> /dev/null
echo "user$I" | passwd --stdin user$I &> /dev/null
echo "user$I creat successful."
else
echo "user$I exist."
fi
done
;;
--del|-d)
for I in `seq 1 10`;do
if id user$I &> /dev/null;then
userdel -r user$I &>/dev/null
echo "user$I delete successful."
else
echo "user$I exist."
fi
done
;;
*)
echo "Unknow commend,-a for add ,-d for delete."
esac
脚本03.sh:
计算100以内所有能被3整除的正整数的和;
#!/bin/bash
#
SUM=0
for I in `seq 0 3 100`;don
SUM=$[$SUM+$I]
done
echo "$SUM"
脚本04.sh:
计算100以内所有奇数的和以及所有偶数的和;分别显示之;
#!/bin/bash
#
I=0
SUM1=0
SUM2=0
for I in `seq 0 2 100`;do
SUM1=$[$SUM1+$I]
done
for I in `seq 1 2 100`;do
SUM2=$[$SUM2+$I]
done
echo "ou SUM1=$SUM1"
echo "ji SUM2=$SUM2"
脚本05.sh:
写一个脚本22,分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
#!/bin/bash
#
echo -e "BASH,`grep /bash /etc/passwd | wc -l` users,they are :\n `grep /bash /etc/passwd | cut -d ":" -f 1 | tr "\n" ","`"
echo -e "NOLOGIN,`grep /sbin/nologin /etc/passwd | wc -l` users,they are : \n `grep /sbin/nologin /etc/passwd | cut -d ":" -f 1 | tr "\n" ","`"
脚本06.sh:
给定一个用户:
1、如果其UID为0,就显示此为管理员;
2、否则,就显示其为普通用户;
#!/bin/bash
#
read -p "Please input your user name:" NAME
UID=`id -u $NAME`
([ $UID == 0 ] && echo "You are admin." || echo "You are not admin."
脚本07.sh:
判断当前系统上是否有用户的默认shell为bash;
如果有,就显示有多少个这类用户;否则,就显示没有这类用户;
#!/bin/bash
#
(`grep /bash /etc/passwd &> /dev/null` && echo "`grep /bash /etc/passwd | wc -l `users using BASH.") || echo "no users using BASH."
脚本08.sh:
给定一个文件,比如/etc/rc.d/rc.sysinit
判断这个文件中是否有空白行;
如果有,则显示其空白行数;否则,显示没有空白行。
#!/bin/bash
#
FILE=/etc/rc.d/rc.sysinit
grep ^$ $FILE &> /dev/null && echo "there are `grep ^$ $FILE | wc -l` blank lines." || echo "no blank lines."
笔记:查找空白行的表示方法用“^$”,例“grep ^$ /etc/rc.d/rc.sysinit”
脚本09.sh:
给定一个用户,判断其UID与GID是否一样。
如果一样,就显示此用户为“good guy”;否则,就显示此用户为“bad guy”。
扩展:不使用id命令获得其id号;
#!/bin/bash
#
read -p "Please input your username:" NAME
echo $NAME
grep ^$NAME /etc/passwd &> /dev/null
if [ $? != 0 ];then
echo "no such user"
else
# UNAME=`grep ^$NAME /etc/passwd &> /dev/null | cut -d ":" -f 3`
# GNAME=`grep ^$NAME /etc/passwd &> /dev/null | cut -d ":" -f 4`
UNAME=`grep ^$NAME /etc/passwd | cut -d ":" -f 3`
GNAME=`grep ^$NAME /etc/passwd | cut -d ":" -f 4`
echo $UNAME
echo $GNAME
if [ $UNAME == $GNAME ] ; then
echo "GOOD BOY!"
else
echo "BAD BOY!"
fi
fi
笔记:$0:脚本本身。$1:第一个参数。$2:第二个参数。。。。$#:传递到脚本的参数个数。$?:命令执行状态返回值,0表示成功,其他表示失败; 如果变量赋值为一些命令的执行结果,则命令用反引号引起来。
脚本10.sh:
给定一个用户,获取其密码警告期限;
而后判断用户密码使用期限是否已经小于警告期限;
提示:计算方法,最长使用期限减去已经使用的天数即为剩余使用期限;
如果小于,则显示“Warning”;否则,就显示“OK”。
#!/bin/bash
#
read -p "Please input your username: " NAME
grep ^$NAME /etc/passwd &> /dev/null
if [ $? != 0 ];then
echo "no such user"
else
WARN_TIME=`grep ^NAME /etc/shadow | cut -d ":" -f 6`
MAX_TIME=`grep ^NAME /etc/shadow | cut -d ":" -f 5`
CHANG_TIME=`grep ^NAME /etc/shadow | cut -d ":" -f 3`
TONEW_TIME_S=`date +%s`
TONEW_TIME_D=$[$TONEW_TIME_S/86400]
TIME=$[$MAX_TIME-($TONEW_TIME_D-$CHANG_TIME)]
if [ $TIME -lt $WARN_TIME ];then
echo "Warning"
else
echo "OK"
fi
fi
脚本11.sh:
判定命令历史中历史命令的总条目是否大于1000;
如果大于,则显示“Some command will gone.”;否则显示“OK”。
#!/bin/bash
#
I=`history | wc -l`
if [ $I -gt 1000 ];then
echo "Some command will gone"
else
echo "OK"
fi
脚本01.sh:
给定一个文件:
如果是一个普通文件,就显示之;
如果是一个目录,亦显示之;
否则,此为无法识别之文件;
#!/bin/bash
#
read -p "Enter your file: " FILE
if [ ! -e $FILE ];then
echo "no such file."
else
if [ -f $FILE ];then
echo "common file."
elif [ -d $FILE ];then
echo "directory."
else
echo "unknow file."
fi
fi
脚本02.sh:
能接受一个参数(文件路径)
判定:此参数如果是一个存在的文件,就显示“OK.”;否则就显示"No such file."
#!/bin/bash
#
read -p "Enter your file name :" NAME
if [ -e $FILE ];then
echo "OK."
else
echo "no such file."
fi
脚本03.sh:
给脚本传递两个参数(整数);
显示此两者之和,之乘积;
#!/bin/bash
#
if [ $# != 2 ];then
echo "unknow commend"
else
echo "SUM=$[$1+$2]"
echo "SUM=$[$1*$2]"
fi
脚本04.sh:
写一个脚本完成一下任务:
1、使用一个变量保存一个用户名;
2、删除此变量中的用户,且一并删除其主目录;
3、显示“用户删除完成”类的信息;
#!/bin/bash
#
read -p "Enter your username :" NAME
id $NAME &>/dev/null
if [ $? == 0 ];then
userdel -r $NAME &> /dev/null
echo "delete user success."
else
echo "no such user"
fi
脚本05.sh:
传递一个参数(单字符就行)给脚本,如参数为q、Q、quit或Quit,就退出脚本;否则,就显示用户的参数;
#!/bin/bash
#
read -p "Please input your choice
q|Q for quit
any to continue
:" CHOICE
if [ $CHOICE == 'q' ];then
exit 0
elif [ $CHOICE == 'Q' ];then
exit 0
else
echo "This is your input: $CHOICI"
fi
脚本06.sh:
传递三个参数给脚本,第一个为整数,第二个为算术运算符,第三个为整数,将计算结果显示出来,要求保留两位精度。
#!/bin/bash
#
if [[ $1 =~ ^[0-9]+$ && $2 =~ ^[+,-,*,/]+$ && $3 =~ ^[0-9]+$ ]];then
echo "scale=2;$1$2$3" | bc
else
echo "Unknow."
fi
笔记:变量的匹配做if的条件需要用双“[]”,“=~”是匹配的意思,“+”前边的字符出现至少一次。
脚本07.sh:
传递3个参数给脚本,参数均为用户名。将此些用户的帐号信息提取出来后放置于/tmp/testusers.txt文件中,并要求每一行行首有行号。
#!/bin/bash
#
if [ $# != 3 ];then
echo "please enter three usernames!"
else
rm -f ./testuser.txt &> /dev/null
grep ^$1 /etc/passwd &> /dev/null && echo "1 `grep ^$1 /etc/passwd`" >> ./testuser.txt || echo "1 there is no user $1" >> ./testuser.txt
grep ^$2 /etc/passwd &> /dev/null && echo "2 `grep ^$2 /etc/passwd`" >> ./testuser.txt || echo "2 there is no user $2" >> ./testuser.txt
grep ^$3 /etc/passwd &> /dev/null && echo "3 `grep ^$3 /etc/passwd`" >> ./testuser.txt || echo "3 there is no user $3" >> ./testuser.txt
fi
脚本08.sh:
判断当前主机的CPU生产商,其信息在/proc/cpuinfo文件中vendor id一行中。
如果其生产商为AuthenticAMD,就显示其为AMD公司;
如果其生产商为GenuineIntel,就显示其为Intel公司;
否则,就说其为非主流公司;
#!/bin/bash
#
STRING=`grep ^vendor /proc/cpuinfo | cut -d ":" -f 2`
if [ $STRING == GenuineIntel ];then
echo "Intel."
elif [ $STRING == AuthenticAMD ];then
echo "AMD."
else
echo "others"
fi
脚本09.sh:
给脚本传递三个整数,判断其中的最大数和最小数,并显示出来。
#!/bin/bash
#
if [ $# != 3 ];then
echo "please enter three number."
else
if [ $1 -gt $2 ];then
MAX=$1
MIN=$2
else
MAX=$2
MIN=$1
fi
if [ $MAX -lt $3 ];then
MAX=$3
fi
if [ $MIN -gt $3 ];then
MIN=$3
fi
echo MAX=$MAX
echo MIN=$MIN
fi
写一个脚本:
1、设定变量FILE的值为/etc/passwd
2、依次向/etc/passwd中的每个用户问好,并显示对方的shell,形如:
Hello, root, your shell: /bin/bash
3、统计一共有多少个用户
4、只向默认shell为bash的用户问声好
#!/bin/bash
declare I
declare J=0
NUM=`cat /etc/passwd | wc -l `
for I in `seq $NUM` ;do
USER=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f1`
USHELL=`cat /etc/passwd | cut -d: -f1,7 | head -$I | tail -1 | cut -d: -f2 | sed 's@/.*/@@'`
# echo "HI $USER your shell is $USHELL"
if [ $USHELL == bash ];then
echo "hi $USER your shell is $USHELL"
let J=$J+1
fi
done
echo "the users total is $I the bash is $J"
----------------------------------------------------------------------------
[root@backup ~]# sh test.sh
hi root your shell is bash
hi test your shell is bash
hi oldboy your shell is bash
hi zuma your shell is bash
hi stu01 your shell is bash
hi stu02 your shell is bash
hi stu03 your shell is bash
hi stu04 your shell is bash
hi stu05 your shell is bash
hi stu06 your shell is bash
hi stu07 your shell is bash
hi stu08 your shell is bash
hi stu09 your shell is bash
hi stu10 your shell is bash
hi stu11 your shell is bash
hi stu12 your shell is bash
hi mysql your shell is bash
hi oldgirl your shell is bash
hi oldgirl888 your shell is bash
hi oldboy888 your shell is bash
hi student your shell is bash
hi student1 your shell is bash
hi student12 your shell is bash
hi user1 your shell is bash
hi test22 your shell is bash
the users total is 60 the bash is 25
---------------------------------------------------------------------------------
写一个脚本:
添加10个用户user1到user10,密码同用户名;但要求只有用户不存在的情况下才能添加;
扩展:
接受一个参数:
add: 添加用户user1..user10
del: 删除用户user1..user10
其它:退出
#!/bin/bash
if [ $1 == "add" ];then
for I in {1..10};do
if id user$I &> /dev/null;then
echo "user$I is exist"
else
useradd user$I
echo "user$1" | passwd --stdin user$I &> /dev/null
fi
done
elif [ $1 == "del" ];then
for I in {1..10};do
if id user$I &> /dev/null;then
userdel -r user$I
else
echo "user$I is not exist!!!"
fi
done
else
echo "please input add or del "
exit 2;
fi
写一个脚本:
计算100以内所有能被3整除的正整数的和
--------------------------------------------------
#!/bin/bash
#计算100以内所有能被3整除的正整数的和
#定义和变量
let SUM=0
for I in {1..100}; do
#取余运算
if [ $[$I%3] -eq 0 ]; then
SUM=$[$SUM+$I]
fi
done
echo "SUM=$SUM"
--------------------------------------------------
[root@backup ~]# sh test2.sh
SUM=1683
[root@backup ~]#
写一个脚本:
计算100以内所有奇数的和以及所有偶数的和;分别显示之;
--------------------------------------------------------------------
#!/bin/bash
#计算100以内所有奇数的和以及所有偶数的和;分别显示之
#奇数和变量
let SUM1=0
#偶数和变量
let SUM2=0
for I in {1..100}; do
if [ $[$I%2] -eq 0 ]; then
SUM1=$[$SUM1+$I]
else
SUM2=$[$SUM2+$I]
fi
done
echo -e "SUM1=$SUM1\nSUM2=$SUM2"
--------------------------------------------------------------------
写一个脚本:
分别显示当前系统上所有默认shell为bash的用户和默认shell为/sbin/nologin的用户,并统计各类shell下的用户总数。显示结果形如:
BASH,3users,they are:
root,redhat,gentoo
NOLOGIN, 2users, they are:
bin,ftp
思路如下:
#!/bin/bash
#
NUMBASH=`grep "bash$" /etc/passwd | wc -l`
BASHUSERS=`grep "bash$" /etc/passwd | cut -d: -f1`
BASHUSERS=`echo $BASHUSERS | sed 's@[[:space:]]@,@g'`
echo "BASH, $NUMBASH users, they are:"
echo "$BASHUSERS
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方案1:
[root@backup ~]# cat test4.sh
#!/bin/bash
#
NUMBASH=`grep "bash$" /etc/passwd | wc -l`
BASHUSERS=`grep "bash$" /etc/passwd | cut -d: -f1`
BASHUSERS=`echo $BASHUSERS | sed 's@[[:space:]]@,@g'`
echo "BASH, $NUMBASH users, they are:"
echo "$BASHUSERS"
[root@backup ~]#
[root@backup ~]#
[root@backup ~]#
[root@backup ~]# sh test4.sh
BASH, 24 users, they are:
root,test,oldboy,zuma,stu01,stu02,stu03,stu04,stu05,stu06,stu07,stu08,stu09,stu10,stu11,stu12,mysql,oldgirl,oldgirl888,oldboy888,student,student1,student12,test22
[root@backup ~]#
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方案2:
[root@backup ~]# cat test5.sh
#!/bin/bash
#显示bash用户的个数和用户名,命令调用符注意
echo "BASH,`grep "bash" /etc/passwd | cut -d: -f1 | wc -l` users,there are `grep "bash" /etc/passwd | cut -d: -f1 | sed ':a;N;$!ba;s@[[:space:]]@,@g'`"
echo ""
#显示nologin用户的个数和用户名,命令调用符注意
echo "NOLOGIN,`grep "nologin" /etc/passwd | cut -d: -f1 | wc -l` users,there are `grep "nologin" /etc/passwd | cut -d: -f1 | sed ':a;N;$!ba;s@[[:space:]]@,@g'`"
[root@backup ~]#
[root@backup ~]#
[root@backup ~]# sh test5.sh
BASH,24 users,there are root,test,oldboy,zuma,stu01,stu02,stu03,stu04,stu05,stu06,stu07,stu08,stu09,stu10,stu11,stu12,mysql,oldgirl,oldgirl888,oldboy888,student,student1,student12,test22
NOLOGIN,32 users,there are bin,daemon,adm,lp,mail,uucp,operator,games,gopher,ftp,nobody,dbus,vcsa,abrt,haldaemon,ntp,saslauth,postfix,sshd,tcpdump,www,apache,rsync,rpc,rpcuser,nfsnobody,nginx,avahi-autoipd,dhcpd,rtkit,pulse,gdm
[root@backup ~]#
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
方案3:
[root@backup ~]# cat test3.sh
#!/bin/bash
file=/etc/passwd
bsh='/bin/bash'
nobsh='/sbin/nologin'
use=`cat $file | cut -d: -f1`
declare -i d1=0
declare -i d2=0
for I in $use ; do
s=`grep "^$I:" $file | cut -d: -f7`
if [ "$s" = $bsh ] ; then
let d1=$d1+1
#muser=$I\,$muser
if [ $d1 -eq 1 ] ; then
muser=$I
else
muser=$muser\,$I
fi
elif [ "$s" = $nobsh ] ; then
let d2=$d2+1
#suser=$I\,$suser
if [ $d2 -eq 1 ] ; then
suser=$I
else
suser=$suser\,$I
fi
fi
done
echo "BASH,$d1 users ,they are:"
echo $muser
echo
echo "NOLOGIN,$d2 users ,they are:"
echo $suser
[root@backup ~]#
[root@backup ~]# sh test3.sh
BASH,24 users ,they are:
root,test,oldboy,zuma,stu01,stu02,stu03,stu04,stu05,stu06,stu07,stu08,stu09,stu10,stu11,stu12,mysql,oldgirl,oldgirl888,oldboy888,student,student1,student12,test22
NOLOGIN,32 users ,they are:
bin,daemon,adm,lp,mail,uucp,operator,games,gopher,ftp,nobody,dbus,vcsa,abrt,haldaemon,ntp,saslauth,postfix,sshd,tcpdump,www,apache,rsync,rpc,rpcuser,nfsnobody,nginx,avahi-autoipd,dhcpd,rtkit,pulse,gdm
[root@backup ~]#
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

浙公网安备 33010602011771号