When to use shell?
1. Shell is suitable for developing gadgets and packaging scripts
2. Act as a watering language, only calling other programs or doing very little data processing
3. If you need to use hash or nested array, it is recommended to use other programming languages.
4. For scenarios with higher performance requirements, it is recommended to use other languages.
Variable
Define variables
# Do not leave a space between a and 1, for example: it cannot be written as a = 1 a=1 echo $a
Quote variable
a=1 ? # Writing method one: echo $a ? # Writing method two (recommended): echo ${a} ? # The advantage of using ${} is that strings can be spliced echo ${a}b # => output 1b
Quotation marks
# Try to use double quotes. Double quotes can output variables and escape, but single quotes are completely literal. a=1 echo "${a}" # => Output 1 echo '${a}' # => output ${a}
Environment variables
# Environment variables are not required to be capitalized, but it is recommended to write them in capitals. ? export A=1 # or declare -x B=2 ? # Ordinary variables can also be exported as environment variables C=3 export C
Define constants
# Constants are generally written at the beginning of the file readonly CONST1 # or declare -r CONST2
Default global variable
# View the current directory $PWD ? # The directory where you were last located $OLDPWD ? #PATH environment variable $PATH
Special variables
# Script name $0 ? # Script parameters, $1 is the first parameter... $1 $2 $3 ? #Number of parameters $# ? # The result of the previous command, 0 means normal completion, non-0 means there is an exception $?
Basic syntax
Conditional judgment
For shell judgment, you can use []
or [[]]
, but it is recommended to use [[]]
if [[ $1 -eq "1" ]];then echo "ok" elif [[ $1 -eq "2" ]];then echo "2333" else echo "no"
Judgment can be divided into number comparison, string comparison and file judgment.
Number comparison
Judgment of equality: =
, ==
, -eq
(It is recommended to use ==
or -eq
, the semantics are relatively clear)
The judgment is not equal: !=
, -ne
Judgment is greater than: >
(requires double brackets (())
), -gt
([[]]
i.e. Can)
Judging less than: <
(double brackets are required), -lt
([[]]
is enough)
nb=4 ? (( $nb > 4 )) & amp; & amp; echo "yes" || echo "no" # or [[ $nb -gt 4 ]] & amp; & amp; echo "yes" || echo "no"
Determine greater than or equal to: -ge
Determine less than or equal to: -le
String judgment
Judgment of equality: =
, ==
, -eq
The judgment is not equal: !=
, -ne
Judgment is greater than: >
(requires double brackets (())
), -gt
([[]]
i.e. Can)
Judging less than: <
(double brackets are required), -lt
([[]]
is enough)
Determine greater than or equal to: -ge
Determine less than or equal to: -le
Determine if the string is not null (the length is not 0): -n
Determine whether the string is null (the length is 0): -z
str="" # Undefined and zero-length strings are considered empty strings [[ -z $str ]] & amp; & amp; echo "yes" || echo "not" # Output yes [[ -n $str ]] & amp; & amp; echo "yes" || echo "not" # Output not
File Judgment
Determine whether it exists (no restriction on type): -e
Determine whether file exists: -f
Determine whether folder exists: -d
Logical operator
Negation: !
Or: ||
With: & amp; & amp;
a=1 [[ ! $a == 1 || 1=1 ]] & amp; & amp; echo "yes" || echo "no" # Output => yes [[ ! $a == 1 & amp; & amp; 1=1 ]] & amp; & amp; echo "yes" || echo "no" # Output => no
case statement
# | Equivalent to or, # ;; Equivalent to break, must be present # *) Equivalent to default ? case $1 in s|start) echo "start" ;; stop) echo "stop" ;; restart) echo "restart" ;; *) echo "Usage:[start|stop|reload]" exit 1 ;; esac exit 0
Loop
for loop
# Writing method one # Output all files in the current folder for f in `ls`;do echo $f done ? # Writing method two # Output odd numbers from 1 to 100 for i in {1..100..2};do echo $i done ? # Writing method three # Calculate the addition from 1 to 100 sum=0 for(( i=1;i<100;i + + ));do echo $(( sum + =i )) done
while loop
nb=0 while [[ $nb -lt 10 ]];do echo $(( nb + =1 )) done
Function
Note: The return value of a function can only be a number
# Function definition function foo() { echo 'output' } ? # Function call foo ? # Function parameters function sum(){ echo $1 echo $2 res=$1 + $2 if [[ $res==3 ]];then return 0 else: return -1 fi } ? # Use $? to receive the function return value. The return value of the function can only be a number! sum 1 2 echo $? # output => 0
shell error handling
set -e
: Exit when an error is reported
set -u
: Detect undefined variables
set -x
: debug execution, output the original content of the shell script, commonly used when writing Jenkins scripts
set -x echo "hello world" ? # Output: # + echo "hello world" # + followed by the source file content, the output format is determined by the PS4 environment variables # hello world
Finally: The following are supporting learning materials. For those who are doing [software testing], it should be the most comprehensive and complete preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you. ! [100% free without any tricks]
Software testing interview applet
A software test question bank that has been used by millions of people! ! ! Who is who knows! ! ! The most comprehensive interview test mini program on the Internet, you can use your mobile phone to answer questions, take the subway, bus, and roll it up!
Covering these interview question sections:
1. Basic theory of software testing, 2. web, app, interface function testing, 3. network, 4. database, 5. linux
6. Web, app, interface automation, 7. Performance testing, 8. Programming basics, 9. HR interview questions, 10. Open test questions, 11. Security testing, 12. Computer basics