shell实例
实例一:判断系统中有没有“zhangsan”这个用户。如果没有,则创建“zhangsan”这个用户;如果存在,提醒用户已经存在。
vim is-user.sh
#!/bin/bash
# is-user
if grep -wq '^zhangsan' /etc/passwd
then
echo "zhangsan user is exists"
else
useradd zhangsan
fi
实例二:提醒用户输入数字[0-100],输入为空或不是纯数字,则退出;输入大于100时,则提醒用户(case用法)
vim case.sh
#!/bin/bash
#case example
read -p "Please input a number: " n
if [ -z "$n" ]; then
echo "Please input a number."
exit 1
fi
n1=`echo $n|sed 's@[0-9]@@g'`
if [ -n "$n1" ]; then
echo "Please input a number."
exit 2
fi
if [ $n -lt 60 ] && [ $n -ge 0 ]; then
tag=1
elif [ $n -ge 60 ] && [ $n -lt 80 ]; then
tag=2
elif [ $n -ge 80 ] && [ $n -lt 90 ]; then
tag=3
elif [ $n -ge 90 ] && [ $n -le 100 ]; then
tag=4
else
tag=0
fi
case $tag in
1)
echo "Fail "
;;
2)
echo "pass"
;;
3)
echo "excellent"
;;
4)
echo "Very good"
;;
*)
echo "The number range 0-100"
;;
esac
实例三:打印从1到100的和(for循环的应用)
vim add.sh
#!/bin/bash
# And how much is it from 1 to 100
sum=0
for i in `seq 1 100`; do
sum=$[$sum+$i]
done
echo $sum
实例四:系统1分钟的负载大于3时,每隔1分钟发邮件给指定邮箱,给这个脚本放到screen虚拟终端运行
vim load.sh
#!/bin/bash
load=`uptime | awk -F 'load average:' '{print $2}'|cut -d. -f1|sed 's/ //'`
while :
do
if [ $load -gt 3 ]; then
/usr/lib/zabbix/alertscripts/mail.py "邮箱名" "主题" "内容"
fi
sleep 60
done
实例五:直到输入的数字是纯数字就停止循环(continue,break的用法)
vim while.sh
#!/bin/bash
# Until the input number is a pure number, the cycle stops
while true; do
read -p "Please enter a pure number: " n
if [ -z "$n" ]; then
continue
fi
n2=`echo $n|sed 's/[0-9]//g'`
if [ -n "$n2" ]; then
continue
fi
break
done
echo $n
实例六:continue,break用在for循环中
vim break-continue-for.sh
#!/bin/bash
# The use of break, continue in the for loop
for i in `seq 1 10`; do
if [ $i -eq 3 ]; then
continue
fi
if [ $i -eq 8 ]; then
break
fi
echo $i
done
实例七:输入系统上存在的网卡名就打印出对应的ip,反之输入系统上不存在的网卡名就一直提示输入网卡名直到存在就结束
vim ethenet.sh
#!/bin/bash
ip() {
ifconfig |grep -A1 "$1: "|grep 'inet'|awk '{print $2}'
}
while true; do
read -p "Please enter the eth name: " eth
if ifconfig | grep -woq "^$eth"; then
ip $eth
break
else
echo "$eth is not system eth name."
continue
fi
done
实例八:批量创建用户student01-student10,密码随机,并将用户名和密码保存到一个文件
vim useradd.sh
#!/bin/bash
[ -f /etc/init.d/functions ] && source /etc/init.d/functions
[ $UID -ne 0 ] && {
echo "denied pemission"
exit 1
}
for user in $(seq -f 'studend%02g' 10); do
if id $user &> /dev/null; then
action "$user is already exists" /bin/false
continue
fi
pass=$(echo $RANDOM|md5sum|cut -c 1-10)
useradd $user && echo "$pass" | passwd --stdin $user &> /dev/null
RETVAL=$?
if [ $RETVAL -eq 0 ]; then
action "useradd $user is OK" /bin/true
fi
echo "$user:$pass" >> /tmp/userlist.log
done
实例九:自动安装nginx
[root@localhost ~]# vim /scripts/auto_install_nginx.sh
#!/bin/bash
NGINX_VER="$1"
NGINX_SOFT="nginx-${NGINX_VER}.tar.gz"
NGINX_URL="http://nginx.org/download"
NGINX_DIR="/usr/local/nginx"
NGINX_SRC=$(echo $NGINX_SOFT|sed 's/.tar.*//g')
NGINX_YUM="yum install -y"
NGINX_ARG="--user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module"
if [ $# -eq 0 ]; then
echo -e "\033[32m-----------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.2.3|1.12.2|help}\033[0m"
exit 0
fi
if [ $1 == "help" ]; then
echo -e "\033[32m---------------------\033[0m"
echo -e "\033[32mUsage:{/bin/bash $0 1.2.3|1.12.2|help}\033[0m"
exit 0
fi
$NGINX_YUM wget make tar gcc gcc-c++ glibc zlib zlib-devel
$NGINX_YUM perl perl-devel pcre pcre-devel openssl openssl-devel
wget -c $NGINX_URL/$NGINX_SOFT
tar -zxvf $NGINX_SOFT
cd $NGINX_SRC
useradd -s /sbin/nologin nginx
./configure --prefix=$NGINX_DIR/ $NGINX_ARG
make -j4
make -j4 install
$NGINX_DIR/sbin/nginx
ps -ef | grep nginx
netstat -tlnp|grep 80
实例十:根据网卡名输入对应的IP
vim ip.sh
#!/bin/bash
ip addr|grep -E '^[1-9]+'|awk -F: '{print $1,$2}' > /tmp/ip.log
while true; do
read -p "请输入网卡名($(cat /tmp/ip.log|awk '{print $2}'|xargs|sed 's/ /,/g')): " e
if [ -n "$e" ]; then
if grep -qw "$e" /tmp/ip.log; then
break
else
echo "输入的网卡不正确,请重新输入:"
continue
fi
else
echo "输入的网卡不能为空,请重新输入:"
continue
fi
done
getIp(){
n1=$(grep -w "$1" /tmp/ip.log|awk '{print $1}')
n2=$[$n1+1]
line1=$(ip addr|grep -wn "$1:"|awk -F: '{print $1}')
line2=$(ip addr|grep -n "^$n2:"|awk -F: '{print $1}')
if [ -z $line2 ]; then
ip addr|sed -n "$line1,$"p|grep 'inet '|awk -F ' +|/' '{print $3}'
else
ip addr|sed -n "$line1,$line2"p|grep 'inet '|awk -F ' +|/' '{print $3}'
fi
}
myIp=$(getIp $e)
if [ -z "$myIp" ]; then
echo "网卡$e没有设置IP."
else
echo "网卡$eIP地址为:$myIp"
fi
实例十一:构建一个分发系统
定义好 ip.list,filename.list
chmod +x ./send.sh ./send.sh 执行,此时会生成 ./rsync.expect文件,这样就可以将文件同步到多到机器上了。
chmod +x ./exe.sh ./exe.sh 执行,此时会生成 ./exe.expect文件,实现远程登陆到多台机器并执行命令。
实例十二:获得对方在线的ip和推送公钥
#!/bin/bash
>/tmp/ip.list
password=123456
rpm -q expect &>/dev/null
if [ $? -eq 0 ];then
echo "package expect is installed."
else
yum install expect -y &>/dev/null
fi
id_rsa=~/.ssh/id_rsa
if [ ! -f $id_rsa ];then
ssh-keygen -f $id_rsa -N "" -q
fi
for i in {10..254};do
{
ip=192.168.144.$i
ping -c1 -W1 $ip &>/dev/null
if [ $? -eq 0 ];then
echo $ip >> /tmp/ip.list
/usr/bin/expect <<-EOF
set timeout 30
spawn ssh-copy-id $ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password" { send "$password\r" }
}
expect eof
EOF
fi
}&
done
wait
echo -e "\a"
echo "finish......"
实例十三:判断ip是否存活3次机会
实例十四:shell模拟jumpserver