I. Variable assignment
#! /Bin/bash
A = "Hello World"#= No space is allowed on both sides!
Echo"A is$"
UnsetA
Echo"This is $ {A} Nd"#It is best to use {} to expand the variable to avoid confusion.
Declare-I num1 = 10
Declare-I num2 = 20
Declare-I num3 =$ Num1+$ Num2
Important commands:
Ls wc cp mv grep cut cat file read sort uniq expr find Tee basename dirname head tail sed awk
2. Process Control
1. Parameter Control
#! /Bin/bash
Echo$1 $2 # parameter name
Echo$0 #ProgramName
Echo$ @ # List all parameters in a variable $ *
Echo$# Number of parameters
Echo$ # Process ID
Echo$? # Exit status of the previous command
2. If loop (parameters must be separated by spaces !!)
#! /Bin/bash
ForDay in 1 2 3 4 5 6 7
Do
If[$ Day= 6-o$ Day= 7]
Then
Echo"$ DayIs a nice day"
Elif [$ Day= 1]
Then
Echo"Starting to work ..."
Fi
Done
3. Case
Read-P 'Please input a num: 'num
Case $ NumIn
1)
Echo1 ;;
2)
Echo2 ;;
3 | 4)
Echo3 or 4 ;;
*)
Echo'Other ';;
Esac
4.
# ! /Bin/bash
Read Dir
If [-D" $ Dir "-A-r" $ Dir "]; Then
File = $ (LS $ Dir )
For Filename in $ File
Do
If [-U" $ Dir / $ Filename "]; Then
Echo " $ Filename Has uid"
Ls-l" $ Dir / $ Filename "
Fi
Done
Else
Echo "Sorry $ Dir Does not exist or cocould not read"
Fi
In this example, enter/usr/bin.
5. While
#! /Bin/bash
Read Str
While["$ Str"! = 'Ubuntu ']
Do
Echo'Not right'
Read Str
Done
Echo"You have guessed it! "
6.
#! /Bin/bash
Read Str
Until [$ Str= 'Quit']
Do
Echo'Keep on'
Read Str
Done
Printf"You have entered quit! \ N"