shell脚本常用案例-5.10
1.
#!/bin/bash
# 通过参数传递一个用户名给脚本,此用户不存在,则添加,密码与用户名相同
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if grep "^$1\>" /etc/passwd &>/dev/null; then
echo "user $1 exist"
else
useradd $1
echo $1 |passwd --stdin $1 &>/dev/null
echo "add user $1"
fi
2.
#!/bin/bash
# 通过参数传递一个用户名给脚本,此用户存在,删除用户以及其家目录和邮件目录
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if id -u $1 &>/dev/null;then
userdel -r $1
else
echo "user $1 no exist"
fi
3.
#!/bin/bash
# 通过命令传参数给定两个数字,输出其中最大的数值
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if [ $1 -gt $2 ];then
max=$1
else
max=$2
fi
echo $max
4.
#!/bin/bash
# 通过命令传参数给定一个用户名,判断其ID号是偶数还是奇数
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if [ {id -u $1}%2 -gt 0 ] ;then
echo "ID is jishu"
else
echo "ID is oushu"
fi
5.
#!/bin/bash
# 通过命令行参数给定两个文件名,,此文件不存在,则结束脚本执行,如果都存在时返回两个文件的行数之和
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if [ -e $1 -a -e $2 ];then
hangshu=$[ $(grep ".*" $1 |wc -l)+$(grep ".*" $2 |wc -l) ]
else
echo "no file"
exit 1
fi
echo $hangshu
6.
#!/bin/bash
# 通过脚本创建一个用户,并设置密码,要求:需要用户交互式输入用户名和密码,如果
# 用户名存在,终止脚本,如果不存在就创建,且设置密码
read -p "enter a user name:" name
[ -z "$name" ]&& echo "a user name is needde." && exit 1
if id $name &>/dev/null;then
echo "$name already exists"
exit 2
else
read -p "enter a password for $name,default is 'password':" password
[ -z "passsword" ] && password="password"
adduser $name
echo "$password" |password --stdin $name &>/dev/null
echo "add user $name."
fi
7.
#!/bin/bash
# 通过脚本参数传递一个文件路径给脚本,判断此文件的类型
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if [ -f $1 ];then
echo "Common file"
elif [ -d $1 ];then
echo "Directory"
elif [ -L $1 ];then
echo "Symbolic link"
elif [ -b $1 ];then
echo "block special file"
elif [ -c $1 ];then
echo "character special file"
elif [ -S $1 ];then
echo "Socket file"
else
echo "Unknow"
fi
8.
#!/bin/bash
# 写一个脚本,传递一个参数给脚本,此参数为用户名。根据其ID号来判断用户类型
# 0:管理员 ,1-499:系统用户 ,500+:登录用户,输出用户是那种类型的用户
if [ $# -lt 1 ];
then
echo "please send argument"
exit 1
fi
if id -u $1 &>/dev/null;then
userid=$(id -u $1)
if [ $userid -eq 0 ];then
echo "root"
elif [ $userid -gt 0 -a id -u $1 -lt 500 ];then
echo "system user"
elif [ $userid -gt 500 ];then
echo "login user"
fi
else
echo "no such user"
fi
9.
#!/bin/bash
#列出如下菜单给用户
# disk :show disks info
# mem :show memory info
# cpu : show cpu info
# * : quit
# 提示用户给出自己的选择,而后显示对应其选择的相应系统信息
cat << EOF
disk :show disks info
mem :show memory info
cpu :show cpu info
* :quit
EOF
read -p "please input your option:" option
option=$(echo $option |tr [A-Z] [a-z])
if [ $option=="disk" ];then
fdisk -l
elif [ $option=="mem" ];then
free -m
elif [ $option=="cup" ];then
lscpu
elif [ $option == "*"];then
exit 1
else
echo "wrong option"
fi
10.
#!/bin/bash
# 求100内所有正整数之和
declare -i sum=0
for i in {1..100}; do
sum=$[ $sum+$i ]
done
echo "$sum"
11.
#!/bin/bash
# 列出/var/log/目录下所有文件的类型
for option in /*/*/*/*; do
if [ -f $option ];then
echo "$option is common file"
elif [ -d $option ];then
echo "$option is directory"
elif [ -L $option ];then
echo "$option is symbolic link"
elif [ -b $option ];then
echo "$option is block special file"
elif [ -c $option ];then
echo "$option is character special file"
elif [ -S $option ];then
echo "$option is socket file"
slse
echo "Unkonw"
fi
done
12.
#!/bin/bash
# 分别求出100以内所有偶数之和,奇数之和
declare -i jishuhe=0
declare -i oushuhe=0
for i in {1..100}; do
count=$[ $i % 2 ]
if [ $count -eq 0 ];then
oushuhe=$[ $oushuhe+$i ]
else
jishuhe=$[ $jishuhe+$i ]
fi
done
echo "oushuhe is $oushuhe"
echo "jishuhe is $jishuhe"
13.
#!/bin/bash
# 计算当前用户的id之和
declare -i idcount=0
user=$(wc -l /etc/passwd |cut -d" " -f1)
for i in `seq 1 $user`; do
username=$(cut -d: -f1 /etc/passwd |sed -n "$i"p)
# id=$(id -u $username)
# idcount=$[ $idcoun]
idcount=$[ $idcount+$(id -u $username) ]
done
echo "$idcount"
14.
#!/bin/bash
#通过脚本参数传递一个目录,计算所有文本文件的行数和,并说明文件的总数
if [ $# -lt 1 ];then
echo "a argoument is needed"
exit 1
fi
declare -i files=0
declare -i row=0
files=$( find $1 -type f |wc -l )
for i in `seq 1 $files`; do
row=$(find $1 -type f -exec wc -l {} \;|cut -d" " -f1 |sed -n "$i"p)
rows=$[ $rows+$row ]
done
echo "$files, $rows"
15.
#!/bin/bash
declare -i sum=0
declare -i i=1
while [ $i -lt 100 ] ;do
sum=$[ $sum+$i ]
let i++
done
echo "sum=$sum"
16.
#!/bin/bash
# 分别使用 while for until实现,求100以内的所有偶数之和,100以内所有奇数之和
#
############################################################
#使用for
#declare -i osh=0
#declare -i jsh=0
#
#
#for i in {1..100}; do
#
# count=$[ $i % 2 ]
# if [ $count -eq 0 ];then
# osh=$[ $i+$osh ]
# else jsh=$[ $i+$jsh ]
#
# fi
#done
#echo "osh is $osh"
#echo "jsh is $jsh"
##############################################33333333333
#使用while
#
#!/bin/bash
#declare -i jsh=0
#declare -i osh=0
#i=1
#while [ $i -le 100 ];do
# count=$[ $i%2 ]
# if [ $count -eq 0 ];then
# let osh+=$i
# else
# let jsh+=$i
# fi
# let i++
#done
#
#echo "jsh is $jsh"
#echo "osh is $osh"
#########################################################3
#使用until实现
#
#!/bin/bash
i=1
jsh=0
osh=0
until [ $i -gt 100 ];do
count=$[ $i % 2 ]
if [ $count -eq 0 ];then
let osh+=$i
else
let jsh+=$i
fi
let i++
done
echo "jsh is $jsh"
echo "osh is $osh"
17.
#!/bin/bash
# 创建9个用户,user101-user109; 密码用户名相同,如果这些用户存在就删除,不存在就#创建
for i in `seq 1 9`;do
if id user10"$i" &>/dev/null;then
userdel -r user10"$i"
else
useradd user10"$i"
echo "user10$i"|passwd --stdin user10"$i" &> /dev/null
echo "create user user10$i"
fi
done
18.
#!/bin/bash
#打印99乘法表
#
for((i=1;i<10;i++));do
for((j=1;j<=$i;j++));do
echo -n "$j x $i =$[ $j*$i ] "
done
echo
done
19.
#!/bin/bash
#打印逆序的99乘法表
declare -i k=9
while [ $k -ge 1 ];do
for i in `seq 1 $k|sort -n -r`;do
echo -n "$i x $k = $[ $i*$k ] "
done
echo ""
let k--
done
20.
#!/bin/bash
#linux7系统自动挂载、自动生成本地yum源脚本,只需要在脚本执行之前根据提示输入相对应的路径参数
read -p "Enter iso file url : " ISOURL
echo "iso file url is $ISOURL"
read -p "Enter iso mount url : " MOUNTURL
echo " iso mount url $MOUNTURL"
echo "iso file url is $ISOURL"
echo "iso mount url $MOUNTURL"
#创建挂载目录
mkdir -p $MOUNTURL
#开始挂载
mount $ISOURL $MOUNTURL
# mount /dev/cdrom /local/
# 实验环境下虚拟机挂载的光盘
##### 配置一次性yum源以下3行可以#注释掉 ###########
echo "mount $ISOURL $MOUNTURL" >> /etc/rc.local
cat /etc/rc.local
chmod +x /etc/rc.local
###################################################
#创建yum源配置文件
touch /etc/yum.repos.d/localyum.repo
cat <<EOF > /etc/yum.repos.d/localyum.repo
[localyum]
name=localyum
baseurl=file://$MOUNTURL
gpgcheck=0
enabled=1
EOF
#刷新yum缓存
yum makecache
yum repolist
21.
#!/bin/bash
#用于收集服务器系统基本信息,有待完善。
function system(){
echo "#########################系统信息#########################"
OS_TYPE=`uname`
OS_VER=`cat /etc/redhat-release`
OS_KER=`uname -a|awk '{print $3}'`
OS_TIME=`date +%F_%T`
OS_RUN_TIME=`uptime |awk '{print $3}'|awk -F, '{print $1}'`
OS_LAST_REBOOT_TIME=`who -b|awk '{print $2,$3}'`
OS_HOSTNAME=`hostname`
echo " 系统类型:$OS_TYPE"
echo " 系统版本:$OS_VER"
echo " 系统内核:$OS_KER"
echo " 当前时间:$OS_TIME"
echo " 运行时间:$OS_RUN_TIME"
echo "最后重启时间:$OS_LAST_REBOOT_TIME"
echo " 本机名称:$OS_HOSTNAME"
}
function network(){
echo "#########################网络信息#########################"
INTERNET=(`ifconfig|grep ens|awk -F: '{print $1}'`)
for((i=0;i<`echo ${#INTERNET[*]}`;i++))
do
OS_IP=`ifconfig ${INTERNET[$i]}|head -2|grep inet|awk '{print $2}'`
echo " 本机IP:${INTERNET[$i]}:$OS_IP"
done
curl -I http://www.baidu.com &>/dev/null
if [ $? -eq 0 ]
then echo " 访问外网:成功"
else echo " 访问外网:失败"
fi
}
function hardware(){
echo "#########################硬件信息#########################"
CPUID=`grep "physical id" /proc/cpuinfo |sort|uniq|wc -l`
CPUCORES=`grep "cores" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'`
CPUMODE=`grep "model name" /proc/cpuinfo|sort|uniq|awk -F: '{print $2}'`
echo " CPU数量: $CPUID"
echo " CPU核心:$CPUCORES"
echo " CPU型号:$CPUMODE"
MEMTOTAL=`free -m|grep Mem|awk '{print $2}'`
MEMFREE=`free -m|grep Mem|awk '{print $7}'`
echo " 内存总容量: ${MEMTOTAL}MB"
echo "剩余内存容量: ${MEMFREE}MB"
disksize=0
swapsize=`free|grep Swap|awk {'print $2'}`
partitionsize=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk {'print $3'}`)
for ((i=0;i<`echo ${#partitionsize[*]}`;i++))
do
disksize=`expr $disksize + ${partitionsize[$i]}`
done
((disktotal=\($disksize+$swapsize\)/1024/1024))
echo " 磁盘总容量: ${disktotal}GB"
diskfree=0
swapfree=`free|grep Swap|awk '{print $4}'`
partitionfree=(`df -T|sed 1d|egrep -v "tmpfs|sr0"|awk '{print $5}'`)
for ((i=0;i<`echo ${#partitionfree[*]}`;i++))
do
diskfree=`expr $diskfree + ${partitionfree[$i]}`
done
((freetotal=\($diskfree+$swapfree\)/1024/1024))
echo "剩余磁盘容量:${freetotal}GB"
}
#服务器型号、序列号、
#dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product|Serial"
SN='dmidecode |grep "System Information" -A9|egrep "Manufacturer|Product|Serial" |grep Serial '
function secure(){
echo "#########################安全信息#########################"
#防火墙、selinux
fir=`systemctl status firewalld|grep Active|awk {'print $3'}`
echo 系统防火墙状态$fir
#selinux状态
#用户安全
countuser=(`last|grep "still logged in"|awk '{print $1}'|sort|uniq`)
for ((i=0;i<`echo ${#countuser[*]}`;i++))
do echo "当前登录用户:${countuser[$i]}"
done
md5sum -c --quiet /opt/passwd.db &>/dev/null
if [ $? -eq 0 ]
then echo " 用户异常:否"
else echo " 用户异常:是"
fi
}
function chksys(){
system
network
hardwarede
secure
}
#执行函数
chksys
#查看服务器型号、序列号
#dmidecode|grep "System Information" -A9|egrep "Manufacturer|Product|Serial"
#查看主板型号
#dmidecode |grep -A16 "System Information$"
#查看BIOS信息
#dmidecode -t bios
#查看内存信息
#dmidecode -t memory
#查看最大支持多少内存
#dmidecode|grep -P 'Maximum\s+Capacity'
#查看内存的插槽数,已经使用多少插槽,每条内存多大
#dmidecode |grep -A5 "Memory Device" |grep Size|grep -v Range|
#查看内存频率
#dmidecode|grep -A16 "Memory Device"|grep 'Speed'