S Shell Learning Note v1.0table of Contents
- 1. What's shell
- 2. Hello World Program
- 2.1. SRC
- 2.2. Add run Privilege
- 2.3. Run shell
- 3. variables in Shell
- 3.1. System variables
- 3.2. User Defined variables
- 3.2.1. Define and use
- 3.2.2. Local vs global variable
- 4. Control Structure
- 4.1. Condition judgement
- 4.1.1. Math operation judgement
- 4.1.2. String Compara tion
- 4.1.3. Logic judgement
- 4.1.4. If-else
- 4.1.5. For
- 4.1.6. While
- 4.1.7. Case
- 5. fucntion
- 6. Debug
- 6.1. Echo
- 6.2. Sh-v./shell or sh-x./shell
1
What is shell
Shell is the middle man of kernel and user.
To show shell installed in the system. Different shell may have differnet grammar
Cat/etc/shells
Show current Shell used.
Echo $SHELL
2
Hello World Program2.1Src
# !/bin/ SH Echo "Hello Shell"
2.2Add run Privilege
$ sudo chmod +x./helloshell.sh
2.3Run shell
./helloshell.sh
3
variables in Shell
Variables in shell is System variables and user defined variables.
3.1System variables
Use
$ set
To show the system variables.
3.2User Defined variables3.2.1Define and use
The variables in shell has no data type. Shell would automatically transfer the type. e.g.
msg="Hello World"
Attention:there is no blank spaces before and after ' = '
3.2.2Local vs global variable
Global shell variable stored in ~/.BASHRC, otherwise, they is all local shell variables.
4
Control Structure4.1Condition judgement
Use test or [] to judge a condition in shell.
4.1.1Math Operation judgement
seq |
Example |
explain |
1 |
A-eq b |
A = = B |
2 |
A-ne b |
A! = B |
3 |
A-lt b |
A < b |
4 |
A-le b |
A <= b |
5 |
A-GT b |
A > B |
6 |
A-ge b |
A >= b |
4.1.2String comparation
seq |
Example |
explain |
1 |
STR1 = str2 |
STR1 = = str2 |
2 |
Str1! = str2 |
Str1! = str2 |
3 |
Str1 |
STR1 NOT NULL or not defined. |
4.1.3Logic judgement
seq |
Example |
explain |
1 |
! |
Not |
2 |
Exp1-a EXP2 |
EXP1 && EXP2 |
3 |
Exp1-o EXP2 |
EXP1 or EXP2 |
4.1.4If-else
# !/bin/ SH if [$# Then Echo "$0:you must give one integer" exit 1fiif [$1then echo' is positive "else echo" is negative "fi
4.1.5For
# !/bin/ SH For i in 1 2 3 4 5 does echo"Welcome $i" Done
4.1.6While
#!/bin/SH# test While Loopif[ $#-ne 1]; Then Echo "Usage: $ integer" Exit1fiN=$1I=0 while[ $I-le 10] Do Echo "$n * $i = ' expr $i \* $n '" I=' expr $i + 1 ' Done
4.1.7Case
#!/bin/SH# test Case Statementif[ $#-ne 1]; Then Echo "Usage: $ [a|b|c]" Exit1fi Case$1inch "a")Echo "a Select";;"B")Echo "B Select";;"C")Echo "C Select";; * )Echo "No Action";;Esac
5
fucntion
# !/bin/ SH Demo () { echo"All function args: $*" Echo"The first arg: $" Echo "The second arg: $" Echo"The third arg: $"}Call the functiondemo-f foo barexit 0
6
Debug6.1Echo
Use the Echo to info.
6.2Sh-v./shell or Sh-x./shell
$ sh-v./shell Print the content shell get
$ sh-x./shell show the command then print it.
created:2015-07-23 Thu 22:20
Emacs 24.4.1 (ORG mode 8.3beta)
Validate
Shell Learning Notes (Org-mode production)