shell练习-shell和adb shell

date

# 打印当前时间
[root@zy ~]# date +"%Y-%m-%d %H:%M:%S"
2020-06-20 22:51:47

# 打印时间戳
[root@zy ~]# date +%s
1592664722

shell

[root@zy ~]# cat  doc.sh
echo  "获取脚本执行的参数:$0";
echo "获取的参数的个数:$#";
echo "获取第一个参数:$1";
echo "获取第二个参数:$2";
echo "获取到的参数(str):$*";
echo "获取到的参数(每一个参数都是一个str):$@";
echo "获取当前进程id号(PID):$$";

[root@zy ~]# chmod +x doc.sh 

[root@zy ~]# sh doc.sh 
获取脚本执行的参数:doc.sh
获取的参数的个数:0
获取第一个参数:
获取第二个参数:
获取到的参数(str):
获取到的参数(每一个参数都是一个str):
获取当前进程id号(PID):31978

# 1-转为shell
[root@zy ~]# cat doc.sh 
#! /bin/bash
functest(){
  echo  "获取脚本执行的参数:$0";
  echo "获取的参数的个数:$#";
  echo "获取第一个参数:$1";
  echo "获取第二个参数:$2";
  echo "获取到的参数(str):$*";
  echo "获取到的参数(每一个参数都是一个str):$@";
  echo "获取当前进程id号(PID):$$";
}

functest a b c d e f g

[root@zy ~]# sh doc.sh 
获取脚本执行的参数:doc.sh
获取的参数的个数:7
获取第一个参数:a
获取第二个参数:b
获取到的参数(str):a b c d e f g
获取到的参数(每一个参数都是一个str):a b c d e f g
获取当前进程id号(PID):4581


# 2-获取testrhome博客点赞数
[root@zy ~]# ids=`curl -s https://testerhome.com/topics|grep -o 'href="/topics/[0-9]*"'|awk -F '"|/' '{print $4}'`;for id in $ids;do url='https://testerhome.com/topics/'$id;zan=`curl -s $url|grep -o -m1 '<span>[0-9]*'|awk -F '>' '{print $2}'`;if [ -n "$zan" ];then echo $url '点赞人数' $zan;else echo $url '点赞人数' 0;fi;done|awk -F '/' '{print $NF}'
23456 点赞人数 2
21805 点赞人数 9
23945 点赞人数 0
...

# 2-获取testrhome博客点赞数--shell
[root@zy ~]# cat testerhome.sh 
#!/bin/bash
ids=`curl -s https://testerhome.com/topics|grep -o 'href="/topics/[0-9]*"'|awk -F '"|/' '{print $4}'`
for id in $ids
do
        url='https://testerhome.com/topics/'$id
        zan=`curl -s $url|grep -o -m1 '<span>[0-9]*'|awk -F '>' '{print $2}'`
	if [ -n "$zan" ]
	then 
		echo $url '点赞人数' $zan
	else 
		echo $url '点赞人数' 0
	fi
done|awk -F '/' '{print $NF}'

[root@zy ~]# sh testerhome.sh 
23456 点赞人数 2
21805 点赞人数 9
24157 点赞人数 0
24141 点赞人数 2
24155 点赞人数 0
22600 点赞人数 1
23904 点赞人数 0
23865 点赞人数 8
24138 点赞人数 7

函数

# function-shell
[root@zy ~]# cat func.sh 
#!/bin/bash
demo(){
  echo a
  echo b
}

demo
[root@zy ~]# sh func.sh 
a
b

批量修改文件名

[root@zy test]# find | while  read file; do echo mv $file $file.bak; done

检查死链接

curl -s https://www.testing-studio.com/ |grep  href|grep -o "https[^\"']*" | while read line;do curl -s -I $line |grep "200" && echo 200 $line || echo ERR $line; done;

json数据访问

[root@zy ~]# curl -s https://testerhome.com/api/v3/topics.json | less | jq .topics[0].id
24631

查看log

awk '$9~/200/{print $0}' nginx.log    检索状态为200
awk '$9!~/200/{print $0}' nginx.log   检索状态不是200

grep ' 404 | 500 ' a.txt
awk '$9==" 404"' a.txt

grep ' 404 | 500 ' a.txt

去重并排序统计

awk '$9!~/200/{print $0}' nginx.log |sort|uniq -c|sort -nr   

# 替换( ])为(])并以空格切割
tail -fn 100 catalina.out | awk '{gsub(/ ]/,"]");print $13}'

cpu/mem

[root@zy ~]# ps -ef |grep aliyundun
root     25819 25773  0 00:21 pts/0    00:00:00 grep --color=auto aliyundun
[root@zy ~]# ps -o %cpu -o %mem 25773
%CPU %MEM
 0.0  0.1

# 持续监测服务
[root@zy ~]# while true;do ps -o %cpu -o %mem 29857;sleep 1;done;
%CPU %MEM
 0.0  0.1
%CPU %MEM
 0.0  0.1
%CPU %MEM
 0.0  0.1
 
 
 # 性能监控
 [root@zy ~]# for i in $(seq 1 5);do time=`date +"%Y-%m-%d %H:%M:%S"`;per_info=`ps -o %cpu -o %mem 1`;echo $time $cpu;done
2020-06-21 13:15:31 %CPU %MEM 0.0 0.1
2020-06-21 13:15:31 %CPU %MEM 0.0 0.1
2020-06-21 13:15:31 %CPU %MEM 0.0 0.1
2020-06-21 13:15:31 %CPU %MEM 0.0 0.1

[root@zy ~]# for i in $(seq 1 5);do time=$(date +"%Y-%m-%d %H:%M:%S"); pid=$(ps -ef | grep mysqld | awk '{print $2}') ; per_info=$(ps -o %cpu -o %mem $pid);echo $time $pid $per_info;done
2020-07-04 23:45:49 4002 29758 %CPU %MEM 0.4 22.6
2020-07-04 23:45:49 4002 29764 %CPU %MEM 0.4 22.6
2020-07-04 23:45:49 4002 29770 %CPU %MEM 0.4 22.6
2020-07-04 23:45:49 4002 29776 %CPU %MEM 0.4 22.6
2020-07-04 23:45:49 4002 29782 %CPU %MEM 0.4 22.6



# 性能并发压测-1
[root@zy ~]# for i in $(seq 1 20);do { time curl https:www.dmall.com &>/dev/null & };sleep 1;done
[15] 22466

real	0m0.008s
user	0m0.003s
sys	0m0.002s
[15]   Exit 6                  time curl https:www.dmall.com &>/dev/null
[15] 22470

# 性能并发压测-2.保持并发,持续循环
[root@zy ~]# while true;do count=$(jobs -l | grep Running |wc -l);[ $count -le 10 ] && { time curl https://www.dmall.com &>/dev/null & } || echo $count waiting;done



# top
[root@zy ~]# top -b -d 1 -n 20 -p 4002 | grep --line-buffered '^ 4002' | awk '{cpu+=$9;mem+=$10}{print $9,$10,$cpu/NR,$mem/NR}'
6.7 22.7 426808 0
1.0 22.7 8884 0
0.0 22.7 5922.67 0
...

# top + 可视化
[root@zy ~]# top -b -d 1 -n 20 -p 4002 | grep --line-buffered '^ 4002' | awk '{cpu+=$9;mem+=$10}{print $9,$10,$cpu/NR,$mem/NR}'|gnuplot -e "set terminal dumb;plot '<cat' using 1 with line"

图形化

[root@zy ~]# echo '1
2
3
4
1
3
6'|gnuplot -e "set terminal dumb;plot '<cat' using 1 with line"



  6 ++----------+-----------+-----------+----------+-----------+----------+*
    +           +           +           +          + '<cat' using 1 *******+
    |                                                                    * |
    |                                                                   *  |
  5 ++                                                                 *  ++
    |                                                                **    |
    |                                                               *      |
    |                                                              *       |
  4 ++                                ***                         *       ++
    |                             ****   *                       *         |
    |                         ****        *                     *          |
  3 ++                     ***             *                   *          ++
    |                   ***                 *                **            |
    |                ***                     *              *              |
    |             ***                         *           **               |
  2 ++         ***                             *         *                ++
    |       ***                                 *      **                  |
    |    ***                                     *    *                    |
    + ***       +           +           +         *+**         +           +
  1 **----------+-----------+-----------+----------*-----------+----------++
    0           1           2           3          4           5           6

App shell自动化

## monkey
monkey -p com.xueqiu.android 500


# 自动化
## 获取app页面文件
adb shell uiautomator dump

## 获取app页面元素坐标
adb shell "uiautomator dump && cat /sdcard/window_dump.xml"| grep -oE "<node[^>]*>" | grep -v 'resource-id=""' | grep -oE 'bounds=".*?]"'


## 点击
adb shell input tap x y

## 输入
adb shell input text xx

## 获取屏幕尺寸
adb shell wm size

## 上滑
adb shell
/while true;do input swipe 540 1536 540 576;done;


adb 性能监控

# 性能监控
[C:\~]$ adb shell
grus:/ $ while true;do pid=$(ps -ef |grep com.xueqiu.android |head -1|awk '{print $2}');ps -o %cpu -o %mem $pid;done

dumpsys

# 列出所有可查看下项
dumpsys -l

# 查看cpu/内存信息
dumpsys cpuinfo|grep xueqiu
dumpsys meminfo|grep xueqiu

# linux下cpu/mem文件位置
ls /proc/$pid


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值