测试开发笔记二(Linux与Shell脚本)

2020-03-26  本文已影响0人  提摩太_e9ec

01 | Linux系统和Shell环境准备


运行shell

test.sh

#!/bin/bash
echo "hello"

运行脚本

方式一:
chmod+x ./test.sh
./test.sh
方式二:
/bin/sh test.sh  若以此方式运行,test.sh(shell脚本)中的 #!/bin/bash则不起作用

02 | Linux常用命令(文件/网络/性能)


文件

  1. 字母模式
chmod a+rwx file    # 所有人 赋予 file1 读、写、执行 的权限
chmod a-rwx file    # 所有人 取消 file1 读、写、执行 的权限
chmod u+r file    # 只给 file1的拥有者 赋予 读 的权限
chmod g-w file    # 只取消 同组人 写 的权限
chmod o+x file    # 只给 其他人 赋予 file1 执行 的权限
chmod ug+x,o-w file    # 给文件拥有者和同组人赋予执行权限,取消其他人的写权限
  1. 数字模式
chmod 777 file    等价于 chmod a+rwx file1 -rwxrwxrwx
chmod 765 file    等价于 chmod u+rwx,g+rw,o+rx file1    -rwxrw-r-x 
chmod 432 file    等价于 chmod u+r,g+wx,o+w    -r---wx-w-  

网络

netstat -apn | grep ssh  查看指定程序的端口
netstat -apn | grep ":80"  查看运行在指定端口的进程

性能

03 | Linux三剑客与管道使用


正则表达式

符号 说明
. 匹配换行符以外的任意字符
\w 匹配字母或数字或下划线或汉字
\s 匹配任意的空白符
\d 匹配数字
\b 匹配单词的开始或结束
^ 匹配行首
$ 匹配行尾
符号 说明
* 重复零次或更多次
+ 重复1次或更多次
重复零次或1次
{n} 重复n次
{n,} 重复n次或更多次,重复上限或下限
{n,m} 重复n到m次

grep

参考资料:https://www.cnblogs.com/ftl1012/p/grep.html

grep [options] pattern [file...]
pattern 可以是正则表达式

sed

参考资料:https://www.cnblogs.com/ftl1012/p/9250171.html

流处理器,一次处理一行内容
若sed后不加任何参数,则只操作模式空间的内容,不操作原文件;若想修改原文件,需加参数-i

sed -i '$a abc\ndef\nghi' file  在文件末尾插入
sed -i '1i abc\ndef\nghi' file  在文件开头插入

-sed和grep的区别
1.grep只能查询
2.sed能够增删改查

awk

参考资料:https://www.cnblogs.com/ftl1012/p/awk.html

把文件逐行的读入,以空格为默认分隔符将每行切片,切开的部分再进行后续处理

04 | Bash编程语法


变量

变量的作用范围只在当前窗口

数组初始化

array=(a b c d e)
echo ${array[0]}
echo ${array[@]}  打印所有元素
echo ${array[*]}  同上

数组单个定义

array[0]=a
array[2]=c
echo ${array[*]}

if

if condition
then
  command1
  command2
  ...
  commandN
fi
符号 解释
-gt greater than
-lt less than
-ge greater or equal
-le less or equal
-eq equal
-ne not equal

比较两个变量的大小并输出不同的值

if [ $a -eq $b ];then echo "equal"; elif [ $a -lt $b ];then echo "small";elif [ $a -gt $b ];then echo "big"; fi

for

for var in item1 item2 ... itemN
do 
  command1
  command2
  ...
  commandN
done

循环读取文件内容并输出

for i in ${cat test.txt};do echo "$i";done

while

while condition
do
  command
done
int=1
while(( $int<=5 ))
do
  echo $int
  let "int++"
done

循环读取文件内容并输出

while read line;do echo $line;done<test.txt

05 | Bash脚本编写


read命令

read a
123
echo $a

read a b c
123 456 789
echo $a $b $c

read
012
echo $REPLY

脚本参数传递

1.$0 脚本名称
2.$1~$n 获取参数
3.$# 传递到脚本的参数个数
4.$$ 脚本运行的当前进程ID号
5.$* 显示所有的参数
6.$? 显示最后命令的退出状态。0表示没有错误,其他任何值表明有错误

#!/bin/bash
echo "文件名:"$0
echo "入参个数:"$#
echo "所有入参:"$*
echo "第1、2、3个入参:"$1 $2 $3
echo "退出状态:"$?

运行test.sh

chmod u+x test.sh
bash test.sh 1 2 3

基本运算

1.加法 `expr $a + $b`
2.减法 `expr $a - $b`
3.乘法 `expr $a \* $b`
4.除法 `expr $b / $a`
5.取余 `expr $a % $b`
6.赋值 a=$b
7.相等 [ $a == $b ]
8.不相等 [ $a != $b ]

bash与目录命令

创建目录并生成文件

mkdir test
cd test
echo "hello" > test.txt
ls

bash与内存

统计内存使用

for i in `ps aux | awk '{print $6}' | grep -v 'RSS'`
do
  echo `expr $i / 1024`
done

06 | Nginx日志分析


shell命令中单引号和双引号的区别:单引号里内容不转义,双引号里的内容会转义,举例说明:

a=123
echo '$a'  输出$a,$a未转换
echo "$a"  输出123,$a转换为变量值

grep只支持基本正则,不支持扩展正则,要使用“|”需用egrep,举例说明:

egrep  "ab[c|cc]" test.sh
输出abc abcc
awk '{print $1}' nginx.log | sort | uniq -c | sort -nr | head -3

grep -o '^[0-9]*.[0-9]*.[0-9]*.[0-9]*' nginx.log | sort | uniq -c | sort -nr | head -3
grep "/topics/" nginx.log | sed 's#/topics/[0-9]*#/topics/number#g'
awk '{print $1}' nginx.log | sed ':1;N;s/\n/|/g;t1'

N  将下一行读取到模式空间
:1  标记
t1  t1前的命令执行成功,就跳转到标记1
反复执行":1"和"t1"之间的命令

07 | Linux性能统计分析


uptime

命令说明

命令 全称 说明
mpstat Multiprocessor Statistics 多处理器状态
vmstat virtual memory statistics 虚拟内存状态
iostat input output statistics 输入输出状态

uptime

15:52:31 up 4 days, 21:33,  1 user,  load average: 0.00, 0.01, 0.05

当前时间  总开机时间  当前用户  负载(1分,5分,15分)  

负载

特定时间间隔内运行队列中的平均进程数。进程满足以下条件则位于运行队列中:
1.未等待I/O操作的结果
2.未主动进入等待状态(未调用wait)
3.未被停止(等待终止)

一般来说,每个CPU内核当前活动进程数不大于3,则表示系统运行表现良好。若是多核则需要累加,即4核cpu<12

dmesg | tail -20

打印系统日志

vmstat

image.png vmstat.png

参考资料:http://www.ha97.com/4512.html

procs(进程):
1.r:运行队列中进程数量,可判断是否需要增加CPU(长期大于1)
2.b:等待IO的进程数量

memory(内存):
1.swpd:使用虚拟内存大小,此值越大说明物理内存已不够用
2.free:空闲物理内存大小
3.buff:用作缓冲的内存大小
4.cache:用作缓存的内存大小

CPU:
1.us:用户进程执行时间
2.sy:系统进程执行时间
3.id:空闲时间(包括IO等待时间),中央处理器的空闲时间
4.wa:等待IO时间

mpstat

参数 说明
%usr 用户级(应用程序)执行时发生的CPU利用率百分比
%nice 优先级较高的用户级别执行时发生的CPU利用率百分比
%sys 系统级(内核)执行时发生的CPU利用率百分比
%iowait 系统有未完成的磁盘I / O请求时一个或多个CPU空闲的时间百分比
%irq 一个或多个CPU服务硬件中断所花费的时间百分比
%soft 一个或多个CPU服务软件中断所花费的时间百分比
%idle 一个或多个CPU空闲且系统没有未完成的磁盘I / O请求的时间百分比

CPU空闲时间处理io请求的占比


image.png

CPU可以并行处理io请求


image.png

其他命令

08 | 三剑客实战抽奖程序脚本


#!/bin/bash
rand() {
  seeds=`while read line; do echo ${line// /..}; done < wx.txt`  
  while [[ $count != 1 ]];do
    seeds=`for seed in $seeds;do (($RANDOM%2==0)) && echo $seed;  done` # seeds只有1个元素时,$RANDOM%==1,此时会输出空白
    count=`echo "$seeds" | wc -l`
  done
  echo $seeds
}
rand
#!/bin/bash
rand() {
  local count
  local seeds
  count=0
  seeds=`while read line; do echo ${line// /..}; done < wx.txt`
  while [[ $count != 1 ]];do
    seeds=`for seed in $seeds;do (($RANDOM%2==0)) && echo $seed;  done`
    count=`echo "$seeds" | wc -l`
  done
  if [[ $seeds == "" ]];then
    rand  # 这里用到了递归
  fi  
  if [[ $seeds != "" ]];then
    echo $seeds
  fi  
}
while true;do
  rand
  sleep 1s
done
rand() {
  local seeds
  local count
  count=0
  seeds=`while read line;do echo ${line// /..}; done<test.txt`
  while [[ $count != 1 ]];do
    seeds=`for seed in $seeds;do (($RANDOM%2==0)) && echo $seed;done`
    count=`echo "$seeds" | wc -l`
  done
  if [[ $seeds == "" ]];then
    rand
  fi
  if [[ $seeds != "" ]];then
   echo $seeds 
  fi
}

res() {
 for i in `eval echo {1..$1}`;do
  tmp=`rand`
   while [[ `is_repeat $tmp` == 0  ]];do
     tmp=`rand`
   done
   arrs[$i]=$tmp
 done
 echo ${arrs[@]}
}

is_repeat() {
  for arr in ${arrs[@]};do
    if [[ $arr == $1  ]];then
      #此处需要有echo,因为子进程只能捕获echo的输出,不能捕获return的值
      echo 0
      return 0
    fi
  done
#输出1的逻辑在这里,因为需要遍历完整的数组后,才能输出1
  echo 1
}
res $1
vim ~/.vimrc
set nu
上一篇 下一篇

猜你喜欢

热点阅读