SHELL脚本
SHELL脚本
centos7 虚拟机初始化配置脚本
#!/bin/bash
#CENTOS 7.9 initialization config
#SELinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
#Firewalld
systemctl stop firewalld
systemctl disable firewalld
#VMware tools depends
yum -y install perl gcc gcc-c++ make cmake kernel kernel-headers kernel-devel net-tools
#Common tools
yum install -y vim screen wget lrzsz
#Create .shh
mkdir ~/.ssh
touch ~/.ssh/authorized_keys
监控进程运行状态的脚本
#!/bin/bash
sersync="/usr/local/sersync/sersync2"
confxml="/usr/local/sersync/confxml.xml"
status=$(ps aux |grep 'sersync2'|grep -v 'grep'|wc -l)
if [ $status -eq 0 ];
then
$sersync -d -r -o $confxml &
else
exit 0;
fi杀死指定进程名称的脚本
kill -9 $(ps -ef | grep 'rsync' | grep -v 'grep' | awk '{print $2}')
#返回进程名称给kill -9shell 生成指定范围随机数与随机字符串
1.使用系统的 $RANDOM 变量
echo $RANDOM$RANDOM 的范围是 [0, 32767]
如需要生成超过32767的随机数,可以用以下方法实现。
例:生成400000~500000的随机数
#!/bin/bash
function rand(){
min=$1
max=$(($2-$min+1))
num=$(($RANDOM+1000000000)) #增加一个10位的数再求余
echo $(($num%$max+$min))
}
rnd=$(rand 400000 500000)
echo $rnd
exit 02.使用date +%s%N
例:生成1~50的随机数
#!/bin/bash
function rand(){
min=$1
max=$(($2-$min+1))
num=$(date +%s%N)
echo $(($num%$max+$min))
}
rnd=$(rand 1 50)
echo $rnd
exit 03.使用/dev/random 和 /dev/urandom
/dev/random 存储着系统当前运行环境的实时数据,是阻塞的随机数发生器,读取有时需要等待。
/dev/urandom 非阻塞随机数发生器,读取操作不会产生阻塞。
例:使用/dev/urandom生成100~500的随机数,使用urandom避免阻塞。
#!/bin/bash
function rand(){
min=$1
max=$(($2-$min+1))
num=$(cat /dev/urandom | head -n 10 | cksum | awk -F ' ' '{print $1}')
echo $(($num%$max+$min))
}
rnd=$(rand 100 500)
echo $rnd
exit 04.使用linux uuid
uuid 全称是通用唯一识别码,格式包含32个16进制数字,以'-'连接号分为5段。形式为8-4-4-4-12 的32个字符。
fdipzone@ubuntu:~/shell$ cat /proc/sys/kernel/random/uuid
fd496199-372a-403e-8ec9-bf4c52cbd9cd
例:使用linux uuid 生成100~500随机数
#!/bin/bash
function rand(){
min=$1
max=$(($2-$min+1))
num=$(cat /proc/sys/kernel/random/uuid | cksum | awk -F ' ' '{print $1}')
echo $(($num%$max+$min))
}
rnd=$(rand 100 500)
echo $rnd
exit 05.生成随机字符串
例:生成10位随机字符串
使用date 生成随机字符串
date +%s%N | md5sum | head -c 10使用 /dev/urandom 生成随机字符串
cat /dev/urandom | head -n 10 | md5sum | head -c 10