[root@rocky ~]# aa=nginx
[root@rocky ~]# id $aa 2> /dev/null && echo $aa is exist || { useradd $aa ; echo $aa is creat ;}
nginx is creat
[root@rocky ~]# id $aa 2> /dev/null && echo $aa is exist || { useradd $aa ; echo $aa is creat ;}
uid=1008(nginx) gid=1009(nginx) groups=1009(nginx)
nginx is exist
简单for循环
#!/bin/bash
#
for ((i=1;i<=10;i++));do
echo $i
done
#
for a in `seq 10`;do
echo $a
done
#
for b in {1..10};do
echo $b
done
########################################
a="
100
200
123
"
for i iin $a;do #变量a是三个数 for只循环三次
done
99乘法表
#!/bin/bash
for i in `seq 9`;do
for a in `seq $i`;do
let b=$i*$a
echo -e "$a*$i=${b}\t\c"
done
echo
done
~
[root@rocky tmp]# bash 99
1*1=1
1*2=2 2*2=4
1*3=3 2*3=6 3*3=9
1*4=4 2*4=8 3*4=12 4*4=16
1*5=5 2*5=10 3*5=15 4*5=20 5*5=25
1*6=6 2*6=12 3*6=18 4*6=24 5*6=30 6*6=36
1*7=7 2*7=14 3*7=21 4*7=28 5*7=35 6*7=42 7*7=49
1*8=8 2*8=16 3*8=24 4*8=32 5*8=40 6*8=48 7*8=56 8*8=64
1*9=9 2*9=18 3*9=27 4*9=36 5*9=45 6*9=54 7*9=63 8*9=72 9*9=81
多线程扫描端口
#!/bin/bash
i=1
host=192.168.1.20
while [ $i -le 65535 ];do
{
if nc -zv $host $i &>/dev/null;then
echo $i | tee -a prot.txt
fi
let i++
} & ##后台执行多线程
done
wait #等待执行完关闭线程
#!/bin/bash
#
nginx_cmd=/usr/local/nginx/sbin/nginx
nginx_pid=/usr/local/nginx/logs/nginx.pid
start(){
$nginx_cmd
sleep 2
if [ -e $nginx_pid ];then
echo "服务nginx已经启动PID是`cat $nginx_pid`"
else
echo "服务启动失败"
fi
}
stop(){
$nginx_cmd -s stop
if [ -e $nginx_pid ];then
echo "nginx服务停止"
else
echo "nginx关闭失败"
fi
}
reload(){
if $nginx_cmd -t &>/dev/null;then
$nginx_cmd -s reload
else
$nginx_cmd -t
fi
}
status(){
if [ -e $nginx_pid ];then
echo "服务nginx已经启动PID是`cat $nginx_pid`"
else
echo "没有nginx服务"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
reload)
reload
;;
status)
status
;;
*)
echo -e "$1 is an invalid command\nplease input is start|stop|reload|status to use "
;;
esac
######################################
[root@111 init.d]# /etc/init.d/nginx asdsa
asdsa is an invalid command
please input is start|stop|reload|status to use
[root@111 init.d]# /etc/init.d/nginx status
服务nginx已经启动PID是94971
[root@111 init.d]# /etc/init.d/nginx stop
nginx服务停止
[root@111 init.d]# /etc/init.d/nginx status
没有nginx服务
[root@111 init.d]# /etc/init.d/nginx start
服务nginx已经启动PID是95255
[root@111 init.d]# /etc/init.d/nginx status
服务nginx已经启动PID是95255
[root@111 init.d]# /etc/init.d/nginx reload
[root@111 init.d]# /etc/init.d/nginx reload
nginx: [emerg] unknown directive "djaskldjklas" in /usr/local/nginx/conf/nginx.conf:3
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed
[root@111 init.d]# /etc/init.d/nginx reload
[root@111 init.d]#
批量创建用户设置随机密码
#!/bin/bash
aduser(){
for i in `seq 10`;do
id user$i &> /dev/null && echo user$i is exists || { useradd user$i ; PASS=`cat /dev/urandom | tr -dc '[a-zA-Z0-9]' | head -c 12` ;echo "user${i}:${PASS}" >> /tmp/pass;echo "user${i}:${PASS}" | chpasswd ;}
done
}
#
deuser(){
for i in `seq 10`;do
id user$i &>/dev/null && { userdel -rf user$i &>/dev/null ; cat /dev/null > /tmp/pass ;echo user$i del success ;}|| echo user$i no esists
done
}
#
echo "1-创建用户"
echo "2-删除用户"
read -p "请输入要操作的类型:" sum
case $sum in
1)
aduser
;;
2)
deuser
;;
*)
echo "输入错误"
;;
esac
所有UID相加
awk -F: '{print $3}' /etc/passwd | paste -s -d+ |bc
0+1+2+3+4+5+6+7+8+11+12+14+65534+81+999+193+59+998+997+996+995+994+993+74+70+992+72
#!/bin/bash
while read i ;do
num=`echo $i | awk -F: '{print $3}'`
let nb=$nb+$num
done < /etc/passwd
echo $nb
每十分钟检查一次超过80的/dev/硬盘
#/bin/bash
WARNING=80
while true;do
df | sed -rn '/^\/dev\//s/.* ([0-9]+)%.*/\1/p' | while read DKspace ;do
if [ $DKspace -ge $WARNING ];then
echo "${DKspace} is no space available"
fi
done
sleep 10m
done
nohub bash df.sh &
1-100的所以奇数相加
echo {1..100..2} | tr ' ' +|bc
#!/bin/bash
sum=0
for i in {1..100};do
if [ $((i%2)) -eq 0 ];then continue;fi
let sum+=i
done
echo $sum
break循环退出和continue本次循环退出和return函数退出
#!/bin/bash
for i in `seq 10`;do
if [ $i -eq 6 ];then break;fi
echo $i
done
echo haha
#
1
2
3
4
5
haha
#############################
#!/bin/bash
for i in `seq 10`;do
if [ $i -eq 6 ];then continue;fi
echo $i
done
echo haha
#
1
2
3
4
5
7
8
9
10
haha
shift和cat菜单
cat <<EOF
haha
heihei
hehe
EOF
while [ $1 ];do
echo $1 is ok
shift
done
[root@rocky shell]# bash shift.sh wang abc 123
haha
heihei
hehe
wang is ok
abc is ok
123 is ok
批量创建00到100用户
#!/bin/bash
#
IPADDR=`ifconfig | awk '/netmask/{print $2}' | head -n +1`
if [[ $IPADDR =~ 3 ]] ;then
date
else
groupadd magedu
for i in `seq -w 00 99`;do
useradd -G magedu magedu_$i >/dev/null && echo magedu_${i} is creat success || echo creat fail
done
useradd -G magedu magedu_$((i+1)) >/dev/null && echo magedu_$((i+1)) creat success | echo creat fail
fi
###################################
#!/bin/bash
#
IPADDR=`ifconfig | awk '/netmask/{print $2}' | head -n +1`
if [[ $IPADDR =~ 3 ]] ;then
date
else
groupadd magedu
for i in `seq -w 00 10 && seq 11 100`;do
useradd -G magedu magedu_$i >/dev/null && echo magedu_${i} is creat success || echo creat fail
done
fi
awk -F: '/bash$/{print $1}' /etc/passwd
#!/bin/bash
function yellow() {
echo -e "\033[33m[ $1 ]\033[0m"
}
## green to echo
function green(){
echo -e "\033[32m[ $1 ]\033[0m"
}
## blue to echo
function blue(){
echo -e "\033[34m[ $1 ]\033[0m"
}
## red to echo
function red(){
echo -e "\033[31m[ $1 ]\033[0m"
}
red aa
数组
数组
#!/bin/bash
hehe=(
'ls'
'pwd'
'echo 123'
)
for i in "${hehe[@]}";do
$i
done
trap
#!/bin/bash
trap "echo no ctrl+c or no ctrl+\ " int quit
trap -p
for i in `seq 10`;do
sleep 1
echo $i
done
trap '' int
trap -;
for i in `seq 11 20`;do
sleep 1
echo $i
done
trap '-' int
trap -p
for i in `seq 21 30`;do
sleep 1
echo $i
done
[root@centos8-node1 tmp]# bash trap
trap -- 'echo no ctrl+c or no ctrl+\ ' SIGINT
trap -- 'echo no ctrl+c or no ctrl+\ ' SIGQUIT
1
2
^Cno ctrl+c or no ctrl+
3
4
5
^\Quit (core dumped)
no ctrl+c or no ctrl+
6
7
8
^Cno ctrl+c or no ctrl+
9
10
trap: usage: trap [-lp] [[arg] signal_spec ...]
11
12
^C13
14
15
^C16
17
18
19
20
trap -- 'echo no ctrl+c or no ctrl+\ ' SIGQUIT
21
^C
-------------------------------------------------------------------------------
#!/bin/bash
aa(){
echo "test.`date +%F-%T`" | tee -a /root/aa
}
trap aa exit
while true ;do
echo hehe
done
[root@centos8-node1 tmp]# cat /root/aa
test.2022-08-03-09:43:09
批量修改密码
chpasswd < pw.txt
vi pw.txt
user1:978ebbf1763
user2:23c2626c37b
数组数字比对
数字比对
[root@wang ~]# cat bidui
#!/bin/bash
#
aa=(10 2515 1515 489465 12313215 12312313 116516513132)
max=${aa[0]}
cc=${#aa[*]}
let bb=$cc-1
for i in `seq $bb`;do
if [ "$max" -lt "${aa[$i]}" ];then
max=${aa[$i]}
fi
done
echo $max
----------------------------------------------
随机数字比对
#!/bin/bash
declare -i min max
declare -a num
for i in `seq 10`;do
num[$i]=$RANDOM
[ $i -eq 1 ] && min=${num[1]} && max=${num[1]} && continue
[ ${num[$i]} -gt $max ] && max=${num[$i]} && continue
[ ${num[$i]} -lt $min ] && min=${num[$i]}
done
echo 所有数字是${num[@]} 第一个数字是${num[1]}
echo max is $max
echo min is $min
[root@rocky ~]# bash shuzu.sh
所有数字是13961 6057 4610 28901 1928 12318 5569 15145 3481 21503 第一个数字是13961
max is 28901
min is 1928