catalogue
1, What is a shell
2, Application scenario of shell script
3, Common shell scripts
4, Summary
1, What is a shell
Shell is an application program, which connects users and Linux kernel, so that users can use Linux kernel more efficiently, safely and at low cost. This is the essence of shell.
In short, we operate Linux through the shell. Let me share some shell scripts I used in my previous work.
2, Application scenario of shell script
Mastering the use of shell script is very helpful in our environment management.
For example, we can detect whether the application process of the test development environment exists through the shell script. If there are exceptions, we can send nail notification or email notification; Check whether the application starts normally; Regularly clean the log files and cache files of the test development environment; A series of environmental problems can be realized through the shell.
3, Common shell scripts
1. for loop
#!/usr/bin/env bash #----------------------------------------------------------------------------- # author: wmh # Script description: for loop #----------------------------------------------------------------------------- for1() { array=(a b c) for i in ${array[@]}; do echo "for loop: ${i}" done } for2() { array1=(a1 b1 c1) array2=(a2 b2 c2) for ((i = 0; i < ${#array1[@]}; i++)); do for ((i = 0; i < ${#array2[@]}; i++)); do echo "for Cycle No i=${i}second: ${array1[i]} ${array2[i]}" done done } for3() { array1=(a1 b1 c1) array2=(a2 b2 c2) #If the array position needs to be added! for i in ${!array1[@]}; do for j in ${!array2[@]}; do # ${i} -eq ${j}: indicates that the positions of parameter i and parameter j are the same if [ ${i} -eq ${j} ]; then echo "for Cycle No i=${i} j=${j}second: array1=${array1} array2=${array2}" echo "for Cycle No i=${i} j=${j}second: array1=${array1[$i]} array2=${array2[$j]}" echo " " fi done done } for1 for2 for3
2. Testing service
#!/bin/bash #----------------------------------------------------------------------------- # author: wmh # Script description: check whether the service process exists. If it does not exist, perform nailing notification #----------------------------------------------------------------------------- time=$(date +%Y-%m-%d_%H:%M:%S) # The parameter in content needs to be added '' funDingTalk() { curl 'https://oapi.dingtalk.com/robot/send?access_token=1234567890' \ -H 'Content-Type: application/json' \ -d '{"msgtype": "text", "text": { "content": "'$time' '${HOSTNAME}' '$1' has been killed" } }' } pid_check() { pid=$(ps aux | grep $1 | grep -v grep | awk '{print $2}') if [ "$pid" ]; then echo -e "\033[32m$time $1 $pid Existing process\n\033[0m" else echo -e "\033[31m$time $1 No process\n\033[0m" funDingTalk $1 fi } app_list=(app1 app2 app3 app4) for i in ${app_list[@]}; do pid_check $i done
3. Read txt
#!/bin/bash #----------------------------------------------------------------------------- # author: wmh # Script description: read txt content and cut string #----------------------------------------------------------------------------- array1=$(cat ./demo.txt) array2='a,b,c' for arr in ${array1}; do vars=(${arr//,/ }) echo "Get variable 1: ${vars[0]} ${vars[1]} ${vars[2]}" for var in ${vars[@]}; do echo "Get variable 2: ${var}" done echo " " done demo.txt The contents are as follows: a1,a2,a3 b1,b2,b3 c1,c2,c3
4. Check whether the output contains keywords
#!bin/bash #----------------------------------------------------------------------------- # author: wmh # Script description: check whether the log output after startup contains keywords, which are included in the exit program. If not, continue #----------------------------------------------------------------------------- var=`cd /tmp/bin && su username stop.sh; su username start.sh` echo "var keyword: $var" if [[ $var = *not* ]];then echo "Contains keywords, sign out" exit 1 else echo "Does not contain keywords, Normal output" fi
5. Clear file
#!/bin/bash #----------------------------------------------------------------------------- # author: wmh # Script description: clear file #----------------------------------------------------------------------------- echo "Clear 3 days ago tmp|zip file" find /tmp/*/logs -mtime +3 | egrep "\.(tmp|zip)$" | xargs rm -f echo "Clear gz Files 1 day ago“ find /tmp/ -mtime +1 -name "*.gz" | xargs rm -f echo "Clear redirect large log file“ for i in $(find /tmp/*/logs/catalina.out); do cat /dev/null >$i done echo "Clear zombie process" for n in $(lsof | grep delete | awk '{print $2}'); do echo $n kill -9 $n done
6. Prefix file names in batch
#!/usr/bin/env bash #----------------------------------------------------------------------------- # author: wmh # Script description: prefix file names in batches. Only files are supported, not folders Note: cp means copy and mv means cut #----------------------------------------------------------------------------- cd /tmp for i in $(ls); do cp -rf $i "prefix_"$i done
I have put the relevant scripts in the warehouse. If you need them, you can get them yourself:
https://github.com/WEIMHaaa/wei-notebook.git https://gitee.com/weimenghua/wei-notebook.git
4, Summary
Mastering the ability of environmental management is one of the very important skills as a test engineer. Dig the pain points encountered in the process of team testing, take all problems hindering R & D efficiency as the entry point, and think of solutions.
Focus on [Gaga software testing]
Do the test and don't get lost
Quack King Ben quack takes you to fly!
Gaga software test
This official account will share personal growth, team management, software testing skills, etc., and update frequency of two articles a week, so that we can have ideas, opinions and depth, and welcome subscriptions.