3、My Scripts

时间:2024-05-04 00:05:26
、用for循环批量修改文件扩展名(P240)
、使用专业改名命令rename来实现
、通过脚本实现sshd、rsyslog、crond、network、sysstat服务在开机时自动启动(P244)
、打印99乘法表(P246)
、用for循环打印1到100的和(P247)
、批量创建10个系统账号,并设置密码(P255)
、Linux系统产生随机数的6中方法(P257)
、select循环(P259)
、break、continue、return、exit脚本演练
10、开发shell脚本实现为服务配置多个IP(P271)
11、利用RANDOM生成随机一段文字并利用md5sum加密保存到一个文件中,根据指定字符串(1ea2edc6fe31)找出破解其加密前的数字(P275)
12、通过C语言型的for循环语句打印数组元素(P283)
13、利用for循环语句打印字母数大于等于4的单词(P286)
14、批量检查多个网址是否正常(P287)
15、检查MySQL主从复制是否正常(P291)

3、My Scripts     3、My Scripts    3、My Scripts

3、My Scripts3、My Scripts


1、用for循环批量修改文件扩展名(P240)

3、My Scripts


2、使用专业改名命令rename来实现

3、My Scripts

3、通过脚本实现sshd、rsyslog、crond、network、sysstat服务在开机时自动启动(P244)            
使用循环
#for i in `chkconfig --list | grep 3:on | awk '{print $1}'`; do chkconfig --level 3 $i off; done
通过bash命令
chkconfig --list | grep 3:off | grep -vE "iscsi|iscsid|multipathor|netconsole|netfs|rdisc|restorecond|saslauthd" | awk '{print " chkconfig " $1 " on "}' | bash

4、打印99乘法表(P246)

 1 #!/bin/bash
2 #
3 for num1 in `seq 9`; do
4 for num2 in `seq 9`; do
5 if [ $num1 -ge $num2 ]; then
6 if (((num1*num2)>9)); then //如果两个乘数相乘大于9,这是控制输出格式
7 echo -en "${num1}x${num2}=$((num1*num2)) " //结尾多一个空格
8 else
9 echo -en "${num1}x${num2}=$((num1*num2)) " //结尾多两个空格
10 fi
11 fi
12 done
13
14 echo " "
15 done
[root@C ~]# bash 99.sh
1x1=1
2x1=2 2x2=4
3x1=3 3x2=6 3x3=9
4x1=4 4x2=8 4x3=12 4x4=16
5x1=5 5x2=10 5x3=15 5x4=20 5x5=25
6x1=6 6x2=12 6x3=18 6x4=24 6x5=30 6x6=36
7x1=7 7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49
8x1=8 8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64
9x1=9 9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81

5、用for循环打印1到100的和(P247)

 #!/bin/bash
#
i=0
while ((i<=100)); do
((sum=sum+i))
((i++))
done echo $sum

方法1

1 #!/bin/bash
2 #
3 for ((i=1;i<=100;i++)); do
4 ((sum=sum+i))
5 done
6
7 echo $sum

方法2

# echo $(( (100+1) * 100/2))

6、批量创建10个系统账号,并设置密码(P255)

 #!/bin/bash
#
. /etc/init.d/functions
user="cos"
passfile="/tmp/user.log" echo ------this is my script for adding users------
for num in `seq -w 10`; do
useradd $user$num
pass="`echo "cos$RANDOM" | md5sum | cut -c 5-12`" echo -e "$user${num}:$pass" >>$passfile if [ $? -eq 0 ]; then
action "Add $user$num is ok." /bin/true
else
action "Add $user$num is fail." /bin/true
fi
done echo "----------Add Users----------"
chpasswd < $passfile &>/dev/null //读取密码文件进行密码设置
cat $passfile

批量添加密码1

 #!/bin/bash
#
. /etc/init.d/functions
user="cos"
passfile="/tmp/user.log" echo ------this is my script for adding users------
for num in `seq -w 10`; do
useradd $user$num
pass="`echo "cos$RANDOM" | md5sum | cut -c 5-12`" echo "$pass" | passwd --stdin $user$num &>/dev/null &&\
echo -e "user:$user$num\tpasswd:$pass" >>$passfile if [ $? -eq 0 ]; then
action "Add $user$num is ok." /bin/true
else
action "Add $user$num is fail." /bin/true
fi
done echo "----------Add Users----------"
cat $passfile

批量添加用户2

#!/bin/bash
#
user="cos"
passfile="/tmp/user.log" for num in `seq -w 10`; do
id $user$num &>/dev/null
if [ $? -eq 0 ]; then
userdel -r $user$num
echo "$user$num was delete."
else
echo "$user$num is not exist."
fi
done cat /dev/null >$passfile

批量删除用户

7、Linux系统产生随机数的6中方法(P257)

1、通过系统环境变量($RANDOM)实现

# echo "hello world$RANDOM" | md5sum | cut -c 5-13
66f32895e

1

2、通过openssl产生随机数   数字和大小写字符、特殊字符相结合

# openssl rand -base64 80
eTlRKlp2r6v/cMt4C3lkNpSU1i/NV8oCnzvMA35zyXLLzQObS3jpjeh5w3doNINk
MGl0gWXji4vupUDyIOUIGV1te25vn+EcJxEjKTt8wqg=

2

4、通过/dev/urandom配合chksum生成随机数

# head /dev/urandom | cksum
1284149441 4169

3

5、通过UUID产生随机数

# cat /proc/sys/kernel/random/uuid
4b3d6a51-990b-4993-b130-cc9dae5df37f

4

3、通过时间(date)产生随机数
# date +%s%N
1533976483650549026

5

6、使用expect附带的mkpasswd生成随机数

# yum install expect -y   //安装expect

# mkpasswd -l 9 -d 2 -c 3 -C 3 -s 1 | md5sum | cut -c 3-12

221d1f369b

相关参数说明:

-l:指定密码长度

-d:指定密码中数字的数量

-c:指定密码中小写字母的数量

-C:指定密码中大写字母的数量

-s:指定密码中特殊字符的数量

6

8、select循环(P259)

 #!/bin/bash
#
select name in xiaohong xiaoming xiaoqiang; do
echo $name
done
# bash 3.sh
1) xiaohong
2) xiaoming
3) xiaoqiang
#? 1
xiaohong
#? 2
xiaoming
#? 3
xiaoqiang

采用数组做变量列表

 #!/bin/bash
#
array=(hong ming qiang) select name in "${array[@]}"; do
echo $name
done

把命令结果作为变量列表

 #!/bin/bash
# select name in `ls /etc`; do
echo $name
done

调整select循环菜单默提示符即利用select变量打印数字序号

 #!/bin/bash
# PS3="Please select a num from menu:" //PS3是控制select循环的提示符
select name in hong qiang ming; do
echo -e "You choice is:\n $REPLY) $name" //REPLY是菜单项对应的数字,即用户输入的数字
done # bash 1.sh
1) hong
2) qiang
3) ming
Please select a num from menu:1
You choice is:
1) hong
Please select a num from menu:2
You choice is:
2) qiang

9、break、continue、return、exit脚本演练(P269)

 #!/bin/bash
# if [ $# -ne 1 ]; then
echo "Usage:$0 {break|continue|exit|return}"
exit 1
fi test() {
for ((i=0;i<=5;i++)); do
if [ $i -eq 3 ]; then
$*; //如果$i=3,这里就接受函数外的参数,即test $*
fi
echo $i
done
echo "I am in func."
} test $*
func_ret=$?
if [ `echo $* | grep return | wc -l` -eq 1 ]; then
echo "return is exit status:$func_ret"
fi echo "ok"

continue是终止当前循环执行下一次循环,break是跳出当前循环(if循环),return是不但终止当前循环还跳出当前函数,exit是直接推出脚本

10、开发shell脚本实现为服务配置多个IP(P271)

 #!/bin/bash
# [ -f /etc/init.d/functions ] && . /etc/init.d/functions
op() {
if [ $1 == "del" ]; then
list=`echo {157..147}`
else
list=`echo {147..157}`
fi for ip in $list; do
if [ $ip -eq 150 ]; then
continue
fi
ip addr $1 192.168.184.$ip/24 dev eth0 label eth0:$ip &>/dev/null
if [ $? -eq 0 ]; then
action "$1 $ip" /bin/true
else
action "$1 $ip" /bin/false
fi
done
} case "$1" in
add)
op add ;;
del)
op del ;;
readd)
op del
echo "Ip was deleted"
sleep 3
echo "Ip will add"
op add ;;
*)
echo "Usage:$0 {add|del|readd}"
esac

使用ifconfig配置别名IP

#ifconfig eth0:0 192.168.184.147/24 up     添加IP

#ifconfig eth0:0 192.168.184.147/24 down   删除IP

使用IP命令配置ip

#ip addr add 192.168.184.147/24 dev eth0 label eth0:147   添加IP

#ip addr del 192.168.184.147/24 dev eth0 label eth0:147   删除IP

11、利用RANDOM生成随机一段文字并利用md5sum加密保存到一个文件中,根据指定字符串(1ea2edc6fe31)找出破解其加密前的数字(P275)

 #!/bin/bash
# #for n in {..}; do //生成随机数,并保存到文件里面
# echo "`echo $n | md5sum` $n" >>/tmp/.log
#done md5char="1ea2edc6fe31" 定义要查找的字符串
#cat /tmp/.log | while read line; do
while read line; do
if [ `echo $line | grep "$md5char" | wc -l` -eq ]; then
echo $line
break
fi
done </tmp/.log

RANDOM随机数范围再0~32767

读取文件中的每一行参考:https://www.cnblogs.com/demonxian3/p/7886177.html

数组

12、通过C语言型的for循环语句打印数组元素(P283)

 #!/bin/bash
#
array=(1 2 3 4 5 6)
for ((i=0;i<${#array[*]};i++)); do
echo ${array[i]}
done #!/bin/bash
#
array=(1 2 3 4 5 6)
for i in ${array[*]}; do
echo $i
done

View

 #!/bin/bash
#
array=(
192.168.184.145
192.168.184.146
192.168.184.147
) #for ((i=;i<${#array[@]};i++)); do //C语言for循环语法
# echo "${array[i]}"
#done for n in ${array[*]}; do //普通for循环语法
echo $n
done

静态数组:array=(1 2 3) 动态数组:array=($(ls))

打印所有数组:${array[*]}  ${array[@]}

打印数组长度:${#array[*]} ${#array[@]}

 #!/bin/bash
#
array=(
192.168.184.145
192.168.184.146
192.168.184.147
) #for ((i=;i<${#array[@]};i++)); do
# echo "${array[i]}"
#done for n in ${array[*]}; do
echo $n
done

13、利用for循环语句打印字母数大于等于4的单词(P286)

 #!/bin/bash
# array=(I am exercising bash scripts I like it very much) #for ((i=0;i<${#array[*]};i++)); do
# if [ ${#array[$i]} -ge 4 ]; then
# echo "${array[$i]}"
# fi
#done for word in ${array[*]}; do
if [ `expr length $word` -ge 4 ]; then
echo $word
fi
done

14、批量检查多个网址是否正常(P287)

 #!/bin/bash
#
. /etc/init.d/functions check_count=0
url_list=(
http://www.baidu.com
https://i.cnblogs.com
https://hao.360.cn
http://www.ninini
https://www.sina.com.cn
) wait() {
echo -n '3秒后,执行检查URL操作.';
for ((i=0;i<;i++)); do
echo -n ".";sleep 1
done
echo
} check_url() {
wait
for ((i=0;i<${#url_list[*]};i++)); do
wget -T 3 --tries=2 --spider ${url_list[$i]} &>/dev/null
if [ $? -eq 0 ]; then
action "${url_list[$i]}" /bin/true
else
action "${url_list[$i]}" /bin/false
fi
done
((check_count++))
} main() {
while true; do
check_url
echo "----------check count:${check_count}-----------"
sleep 3
done
} main

15、检查MySQL主从复制是否正常(P291)

slave.log

Slave_IO_Running:No
Slave_SQL_Running:No
Seconds_Behind_Master:0

 #!/bin/bash
# status=($(awk -F ':' '/_Running|_Behind/{print $NF}' slave.log)) for ((i=0;i<${#status[*]};i++)); do
count=0
if [ "${status[${i}]}" != "yes" -a "${status[${i}]}" != 0 ]; then
let count+=1
fi
done if [ $count -ne 0 ]; then
echo "mysql replcation is failed"
else
echo "mysql replcation is success"
fi