、用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)

         


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


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

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

3、My Scripts的更多相关文章

  1. 二、npm scripts

    一.执行原理 安装npm 包,会将其package.json bin 字段添加到node_modules bin 里面,创建对应的.cmd文件,因此: 例如: "scripts": ...

  2. 4、My Scripts

    脚本目录列表 1.在windows编写的shell脚本利用dos2unix命令格式化一下(P308) 2.bash命令参数调试(P309) 3.使用set命令调试部分脚本内容(P312) 4.开发脚本 ...

  3. 2、My Scripts

    http://www.cnblogs.com/image-eye/archive/2011/10/26/2220405.html      注释详解 1.打印选择菜单,按照选择项一键安装不同的web服 ...

  4. 1、My Scripts

    1.写一个包含命令.变量和流程控制的语句来清除/var/log的messages日志文件的shell脚本.(P26)(11-21) 2.利用$0和(dirname.basename)取出当前路径的目录 ...

  5. mvc中Scripts.Render、Styles.Render

    一.配置BundleConfig.cs文件 1.首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件 2.BundleConfig就是一个微软新加的 一个打 ...

  6. MVC 中Scripts.Render、Styles.Render

    在ASP.NET MVC项目中,可以在视图中利用Scripts.Render.Styles.Render统一加载js.css文件,需要利用BundleConfig类来Add 各种Bundle,例如:b ...

  7. [ASP.NET MVC]@Scripts.Render、@Styles.Render的使用

    一.配置BundleConfig.cs文件 1.首先要在App_Start 里面BundleConfig.cs 文件里面 添加要包含的css文件 2.BundleConfig就是一个微软新加的 一个打 ...

  8. Fix "Missing Scripts"

    一.Missing Scripts(脚本引用丢失) 请看下面的两张图的Warn(脚本引用丢失),在某些情况下我们会遇到这个警告. 二.解决办法 参考资料 http://unitygems.com/la ...

  9. 你必须知道的28个HTML5特征、窍门和技术

    注意:每周有那么几次,此列表会更新一些新的窍门,最终,本文会成为超级有用的资源.//zxx:丑话说在前头,我可没功夫更新,所以,即使到您女儿出嫁那天,本文还是28项内容 前端的发展如此之迅猛,一不留神 ...

随机推荐

  1. 初探AngularJs框架(三)

    一.实现todoList的demo 功能很简单,提供一个文本框,用户输入回车后添加新条目.每个条目可以在待处理和处理中两个区域间切换,每个条目都可以被删除,大致的界面如下图所示: 二.处理逻辑 首先将 ...

  2. bc 命令

    bc命令是一种支持任意精度的交互执行的计算器语言.是Linux简单的计算器,能进行进制转换与计算.能转换的进制包括十六进制.十进制.八进制.二进制等.可以使用的运算符号包括(+)加法.(-)减法.(* ...

  3. python装饰器介绍

    """装饰器 定义:本质是函数(器:就是函数的意思),功能:装饰其他函数,就是为其他函数添加附加功能 原则: 1. 不能修改被装饰的函数的源代码 2. 不能修改被装饰的函 ...

  4. redis 五大数据类型以及操作

    一.redis的两种链接方式 1.简单连接 import redis conn = redis.Redis(host='10.0.0.200',port=6379) conn.set('k1','年后 ...

  5. 删去k个数字后的最小值

    public static String removeKDigits(String num,int k) { //新整数的最终长度=原长度 - k int newLength=num.length() ...

  6. Linux 查看端口使用情况

    之前查询端口是否被占用一直搞不明白,问了好多人,终于搞懂了,现在总结下: 1.netstat  -anp  |grep   端口号 如下,我以3306为例,netstat  -anp  |grep   ...

  7. AdminLTE模板使用

    AdminLTE介绍: AdminLTE是一款建立在bootstrap和jquery之上的开源的模板主题工具,它提供了一系列响应的,可重复使用的组件,并内置了多个模板页面;同时自适应多种屏幕分辨率,兼 ...

  8. An Example of How Oracle Works

    Oracle是怎么工作的,摘自Oracle 9i的官方文档 The following example describes the most basic level of operations tha ...

  9. PHP API接口签名验证

    hash_hmac 在php中hash_hmac函数就能将HMAC和一部分哈希加密算法相结合起来实现HMAC-SHA1  HMAC-SHA256 HMAC-MD5等等算法.函数介绍如下: string ...

  10. php实现共享内存进程通信函数之_shm

    前面介绍了php实现共享内存的一个函数shmop,也应用到了项目中,不过shmop有局限性,那就是只支持字符串类型的:sem经过我的测试,是混合型,支持数组类型,可以直接存储,直接获取,少了多余的步骤 ...