Bash basics, gitbash
1 concept
Bash (sh, pdsh extension): GNU Bourne-Again Shell
Sh: Bourne Shell, excellent programming and inconvenient user interaction.
Csh: C Shell. The programming interface is not as good as sh. the user interface is friendly and similar to the C language syntax. --> Tcsh
Ksh: Korn Shell, which integrates the advantages of sh and csh and is fully compatible with sh. --> Pdsh (Public Domain Korn Shell)
Zsh
2 syntax 2.1 variables, Data Type
Variable definition: it does not contain reserved words (such as "-, *"), and does not contain spaces.
Variable reference: assign a value. $ is not required before a for loop. In other cases, the referenced variable must contain "$" (for example, print, or computation ).
Data Type: shell variables do not have data types.
Shell statement: = left and right cannot have spaces; shell statements do not end.
2.1.1 integer calculation (+-*/%): let, expr
1) let: Double quotation marks
For example, let "x = $ x + 1"
2) expr: ', space. escape characters must be used before special characters \
For example, x = 'expr $ x + 4', x = 'expr $ x \ * 4'
[PS] does not support floating-point calculation. It does not have a floating-point data type.
[Example: arithmetic operation, character concatenation, and no data type of shell]
#! /Bin/bash
X = 2007
Let "x = $ x + 1"
Echo $ x
X = 'expr $ x + 4'
Echo $ x
Echo $ (expr $ x/2)
X = 'olmpic '$ x
Echo x = $ x
Y = 12
Y = 'expr $ y \ * 2'
Echo $ y
2.1.2 assignment
X = 200
Let "x = $ x + 1"
X = 'hello' $ x
Y = $ (expr $ x/2)
Y = 'expr $ y \ * 2'
Result = $ ($ result + $ num ))
Num = $ (num + 1 ))
2.1.3 comparison
1)-eq,-ne,-gt and-ge,-lt and-le,-z (empty),-n (non-empty)
2) = ,! =,> (None> =), <(none <=)
[PS]
When comparing strings, try not to use-z! -N instead.
Can use 1) Try not to use 2 ).
2.1.4 bitwise operation
&, |
Bitwise operation :]
Ckm @ bidc :~ /ZzhTest> echo $ (expr 1 \ & 0)
0
Ckm @ bidc :~ /ZzhTest> echo $ (expr 1 \ | 0)
1
#! /Bibn/bash
If [$ str! -N]; then
Echo null !!
Fi
2.2 process control 2.2.1 if condition control
If [expression]; then statements
Fi
If [expression]; then statements
Else statements
Fi
If [expression]; then statements
Else if [expression] then statements
Else statements
Fi
If [expression]; then statements
Elif [expression]; then statements
Else statements
Fi
[PS]
If the then line breaks, the then does not need to be added before it ;.
There are spaces before and after [and.
2.2.2 case Branch Structure
Case "$ var" in
Cindition1) statements ;;
Cindition2) statements ;;
...
*) Statements ;;
Esac
2.2.3 for Loop
For var in [list]; do
Statements
Done
[PS]
The var variable reference does not use $.
<List> elements are separated by spaces.
2.2.4 while LOOP
While [condition]
Do
Statements
Done
2.2.5 until Loop
Until [condition]
Do
Statements
Done
2.2.6 break/continue
3. Execute the bash script example "hello world": hello
#! /Bin/bash
# Use of Variables
Str = 'Hello world'
Echo $ str
Echo str
-- Execution File
Bash hello
Sh hello
Chmod u + x hello
Hello
Common Linux commands]
Vi zzhdir
#! /Bin/bash
Ls-
-- Execute
Sh zzhdir
[Local variables: scope of local variables (in the function body )]
#! /Bin/bash
Function test {
Local var = world
Echo $ var
}
Var = hello
Echo $ var
Test
Echo $ var
[PS] local variable: When a value is assigned for the first time, the variable is prefixed with local.
[If condition: Compare operation, if condition control structure]
#! /Bin/bash
A = 10
If [$ a-gt 0]
Then echo 1
Else echo 2
Fi
[If condition: script with parameters]
#! /Bin/bash
If [$1 = 10]; then echo right!
Else echo wrong!
Fi
-- Execute
Sh test 123
[PS] $1 is used to match the 1st parameters of the command line. (Reserved Words in bash)
[Case Branch]
Var = 12
Case "$ var" in
[A-z]) echo 'lowercase letter! ';;
[0-9]) echo 'number ';;
*) Echo other character !;;
Esac
[For Loop: No in [list]
For var; do
Echo OK
Done
-- Execute
Sh test
Sh test 1 "hello world"
[For Loop: Scope of I] inside the loop body (between do and done)
For I in $ (seq 5)
Do
Echo $ I
Done
Echo $ I
-- Execute
Sh test
[For Loop: seq]
For I in 'seq 1 5'; do
Echo TEST!
Done
[For Loop: Scope of I] block after Definition
For (I = 1; I <= 5; I ++); do
Echo $ I
Done
Echo $ I
-- Execute
Sh test
For Loop: command line parameters ].
For param
Do
Echo $ param
Done
-- Execute the script
Sh test 123 456 789
[PS] If list is not specified, all command line parameters are printed. If no command line parameter is specified, nothing is printed.
[While loop]
Num = 1
Result = 0
While [$ num-le 3]
Do
Result = $ ($ result + $ num ))
Num = $ (num + 1 ))
Done
Echo $ result
[Until loop]
Num = 1
Result = 0
Until [$ num-ge 3]
Do
Result = $ ($ result + $ num ))
Num = $ (num + 1 ))
Done
Echo $ result
[Break/continue]
For I in 'seq 1 5'; do
If [$ I = 3];
Continue
Else echo $ I
Fi
Done
4 Vi Editor
Naming mode: Esc key, ":" colon, "wq" Save and exit, "q !" Force exit
Insert mode: aAiIuU
Delete a word: dw
Delete the entire row: dd
String SEARCH: slash (/)
5. Related Expansion
Reserved Words
Regular Expression
Function
Input, output:>, >>,> 2,> 1,> &
For Loop
Function of a variable: integer, String, count, and file attribute
Cube
[Bash] $ what does it mean?
Is a prompt, bash is a type in shell scripts. In linux, $ code is a common user and # represents the root user.
It's all about linux. Learn more.
Concepts of Linux, Bash, and Shell
1. Yes
2. The difference is big. Writing bash shell doesn't mean you can write c shell, but I think you can write a shell script.
In addition, you can get started quickly. At least it should be difficult to understand. You can learn bash shell first because it is used more often.
It is also highly portable. bash is a type of shell.
3. As a shell, only bash shell is installed to run the bash shell script. It is not for a specific system or release version.
Shell can be installed at will. Which shell do you like to use? You can install a shell independently even if it is not in the initial installation system.
4. I recommend two books for you.
Bash shell advanced script Guide
Practical LINUX + SHELL programming and Server Management
Shell programming can be said to directly reflect your proficiency in linux commands, and under what circumstances which commands are better suited
How can you simplify the script running process and optimize the script to make it more efficient.
You will inevitably be exposed to stream editors like sed and awk in your learning process. Then you have to learn sed and awk.
Gradually it is estimated that you should choose a more powerful programming language, instead of simply writing shell scripts, such as perl. python and php.
C, or what language... is basically this process.