The sample

Test. Sh file

#! /bin/bash indicates the type of script
echo "Hello World !"
Copy the code

run

/bin/sh test.sh
./test.sh
Copy the code

variable

named

yourName="aaa" 
echo $yourName
echo ${yourName}
Copy the code
  • There can be no Spaces between variable names and equals signs
  • The name can only be usedEnglish,digital,The underline
  • You can’t start with a number
  • You can’t usebashThe keyword

The read-only variable readonly

yourName="aaa" 
readonly yourName
Copy the code

Delete variables (cannot delete read-only variables)

unset yourName
Copy the code

string

Single quotes

  • Variables in single quotes are invalid
  • Single quotes can be used for concatenation
yourName='aaa'
echo 'bb${yourName}cc'
bb${yourName}cc
echo 'bb'$yourName'cc'
bbaaacc
echo 'bb'${yourName}'cc'
bbaaacc
Copy the code

Double quotation marks

  • You can have variables in double quotes
  • Escape characters can appear in double quotes
yourName="aaa"
echo "bb${yourName}cc"
bbaaacc
echo "bb"$yourName"cc"
bbaaacc
echo "bb\"${yourName}\"cc"
bb"aaa"cc
Copy the code

Get the length of the string

yourName="aaa"
echo ${#yourName}
Copy the code

Extract substring

${string:index:leng}

aa="abcd"
echo ${aa:1:2} bc
Copy the code

Find substrings

aa="abcdef"
echo `expr index "$aa"F '6 find f in string (index+1)Copy the code

An array of

Define an array

arr=(1 2 3 4)
arr[6]=6
echo ${arr[6]}
6
echo ${arr[@]}All the elements 1, 2, 3, 4Copy the code

The length of the array

arr=(1 2 3 4)
echo ${#arr[@]}
4
echo ${#arr[*]}
4
Copy the code

annotation

Single-line comments

# single-line comment
Copy the code

Multiline comment

: < <EOF Multi-line comment EOF: < <'Multi-line comment': < <! Annotated content... !Copy the code

Parameter passing

/bin/sh test.sh 1 2 3
$0Test. Sh file nameThe $11 The first argument$22 Second Parameter$33 The third parameter$#3 Number of parameters $x 1 2 3 All parameters$@1 2 3 All parameters $$order before the script process number $? Displays the exit status of the last command. 0 indicates no errors, and any other value indicates an error.Copy the code

The operator

Arithmetic operations

There should be Spaces between expressions and operators
a=10
b=20
+  echo `expr $a + $b30 - ` / /echo `expr $b - $a10 * ` / /echo `expr $bA \ *$a` / / 300 /echo `expr $b / $a` / / 2%echo `expr $b % $a` / / 0 = =echo `expr $b= =$a` / / 0! =echo `expr $b! =$a'//1 = assign a=$b
Copy the code

Relationship between operation

Only numeric or all-numeric strings are supported
a=10
b=20
-eq	[ $a -eq $bAre two numbers equalfalse
-ne	[ $a -ne $bWhether two numbers are not equaltrue
-gt	[ $a -gt $b[left > rightfalse
-lt	[ $a -lt $b[left < righttrue
-ge	[ $a -ge $b] Left >= rightfalse
-le	[ $a -le $b] Left <= righttrue
Copy the code

Boolean operation

a=10 b=20 ! Non-operation [!false ] true. -o or operation [$a -lt 20 -o $b -gt 100 ] true-A and operation [$a -lt 20 -a $b -gt 100 ] false
Copy the code

Logical operations

&&logical AND [[$a -lt 100 && $b-gt 100]] Returnsfalse| | logic OR [[$a -lt 100 || $b-gt 100]] Returnstrue
Copy the code

String operation

a="aa" b="bb"= Are strings equal [$a = $b ] false! = Are strings unequal [$a! =$b ] true-z Indicates whether the character string is 0 [-z$a ] false-n Indicates whether the string length is not 0 [-n"$a" ] trueWhether the $string is empty [$a] is not nulltrue
Copy the code

File detection operation

-f file Checks whether a file is common. -r file Checks whether a file is readable. -w file Checks whether a file is writableCopy the code

echo

  • Output to file
echo "It is a test" > myfile 
Copy the code
  • Display execution results
echo `date`
Copy the code

Process control

if

if[10 == 20]then 
  echo 1
elif[20 == 20]then
  echo 2
else 
  echo 3
fi
Copy the code

for

for a in a b c
do 
  echo $a
done
Copy the code

while

i=3
while (($i< = 5))do
  echo $i
  let i++
done
Copy the code

Until (true)

i=8
until  (($i< = 5))do
  echo $i
  let i--
done
Copy the code

case… esac

i=2
case $i in
  1) echo1;;# break the whole case... Esac statement
  2) echo2;; 3)echo 3
esac # closing tag
Copy the code

Break out of loop: break out of all loops continue Break out of current loop

function

function fun() {# define function
  echo "Function 1"
  echo The $1 1 # 1 parameter
  return 1 The return value must be 0-255
}

fun1() {# define function
No return Returns the execution result of the last statement
}
fun 1
echo $? The return value of the function
Copy the code

Importing additional files

# test1.sh
echo "test1"

# test.sh
./test1.sh
echo "test"
# test1 test
Copy the code