介绍shell
- Shell价值:任务流程自动化、数据处理、不被技术栈所约束
一个简单的任务引入shell三剑客
任务:检查首页是否有死链?
-
确定检查的接口:
curl https://testing-studio.com/
-
将返回的响应存储到某文件:
curl https://testing-studio.com/ > /tmp/testingStudio.html
-
正则匹配响应中的链接:
curl -s https://testing-studio.com/ | grep href | grep -o "http[^\"']*"
其中curl -s 表示不展示网络相关信息 -
检查所有的链接是否有死链
curl https://testing-studio.com/t/topic/45 -v 2>&1
将标准错误重定向到标准输出
1表示标准输出1
2表示标准错误2
0表示标准输入0
>file表示输出重定向到另一个文件curl https://testing-studio.com/t/topic/45 -v 2>&1 | less
less 表示所有的输出进入一个交互环境里curl -I https://testing-studio.com/t/topic/45
-I表示发送一个简单的请求,展示返回码,可以直接根据返回码判定当前是否为死链curl -s https://testing-studio.com/ | grep href | grep -o "http[^\"']*" | while read line;do curl -s -I $line | grep 200 && echo 200 $line || echo ERR $line;done
while read line;do curl -s -I $line
对响应中匹配的url发送请求
grep 200
:匹配200 ,输出结果:HTTP/1.1 200 OK
&& echo 200 $line || echo ERR $line;done