Hello everyone: Today to introduce shell:
1.shell know:
shell itself is a command interpreter, similar to the python interpreter that interprets Linux commands, we can write Linux command shell scripts, by running this script to perform a series of Linux commands. This is the shell programming.
Due to historical reasons shell There are many common shell has sh, bash
View shell species: vim / etc / passwd # The last column shows the type of shell
shell NOTE: The beginning #
Linux shell script is essentially terminal: shell will fork a child process, and exec this program, after the implementation will be back to the terminal (built-in command does not create a new process, which can not find a command, not a single man manual )
Check out the built command: man bash-builtins # built-in command does not create a new process, but there are exit code (Exit Status) 0 for success, 1 for failure
View on a program exit code: echo $?
Note: When the shell script to write, how to enforce it?
Method One: chmod a + x test.sh # execute permissions for the script to increase
./test.sh # script execution
Method two: / bin / sh test.sh # specify the interpreter to execute the script, the script name as an argument similar to the interpreter to execute python script with the same python interpreter; python script name
Method three:. Source test.sh or test.sh
If one execution of a Linux command in Terminal:
For example: (cd ..; ls-l) # multiple commands separated by semicolons will fork a child process will not affect operating results terminal
cd ..; ls -l # multiple commands separated by semicolons, affects the terminal
2. Variable:
Naming conventions: the all-capital letters underlined composition.
Environment variables: printenv can see the environment variable environment variables can be passed to the child process from the parent process
Local variables: set to view all the variables include local variables and environment variables, variables are defined on both sides of the equal sign without spaces;
Local variable # export import local variable environment variable.
unset variables # delete variables
All variables are strings
echo $ SHELL # to view the current shell type
3. Replace the file name: [] *?
Similar regular expressions to match the file name
*: 0 or more of any character
? : Match any character
[Az]: matches any character set of the first occurrence of a character
4. Replace command: $ () or ``
The results of a command assigned to another variable
DATE=`date` 或 DATE=$(date)
5. Alternatively arithmetic: $ (()), $ []
BE = 45
echo $ (($ VAR + 45)) # 90 is equivalent to the result echo $ [$ VAR + 45]
$ [Base # n]: represents a base band wherein, n represents a base band according to explain
echo $ [2 # 10 + 11] # 10 is to explain a binary 2, plus 11, the result is 13
6. escape character: \
File touch \ $ \ \ $ \ # created called \ $ \ \ $ \ of
_ Created at the beginning of a file: touch ./-hello or touch - -hello
Continued line: \ behind knockout round
7. single quote: means for holding a character literals
8. double quotation marks: allows the extension
BE = 45
echo "$ VAR" # 45 results
echo '$ VAR' # $ VAR results
9. Test conditions: test, []
VAR = 2
test $ VAR -gt 1 # 00 The results are expressed as true, false representation 1
[$ VAR -gt 5] # 1 Results
Common test:
[-D DIR] # test whether a directory DIR
[-F FILE] # test whether a file FILE
[-Z STRING] # test length is zero STRING
[-N STRING] # STRING testing whether a non-zero length
Whether [STRING1 = STRING2] # test two character strings
[STRING1! = STRING2] # test string are different
[Variable Variable 1 OP 2] #OP may -eq (equal), - ne (not equal), - lt (less than), - le (or less), - gt (greater than), - ge (greater than or equal)
! : Non logic; -a: logical AND; -o: Logical OR
10. branch statement
if branches:
IF [-f ~ / .bashrc]; # if the condition is satisfied then the statements following then performed . ~ / .bashrc Fi
if -else branches:
IF [-f / bin / bash] the then echo " / bin / bash IS A File " # do not write a semicolon to separate statements the else echo " / BNI / bash the NOT IS A File " #else branch fi IF :; the then echo " to true alwalys " ; fi # multiple statements on one line to add a semicolon colon means always true, what is equivalent to True
if -else-if branches:
echo "It is morning? Please answer yes or no." read YES_OR_NO #从屏幕中读取值存于变量中 if [ "$YES_OR_NO" = "yes" ];then echo "Good morning!" elif [ "$YES_OR_NO" = "no" ];then echo "Gooding afternoon!" else echo "Sorry, $YES_OR_NO not recognized. Enter yes or no."0 # program exit code Exit fi1 # program exit code Exit
&& -a equivalent to equivalent if ..then
|| equivalent if not then equivalent to -o
case branches:
#! /bin/sh echo "It is morning? Please answer yes or no." read YES_OR_NO case "$YES_OR_NO" in yes|y|Yes|YES) echo "Gooding Morning!";; [nN]*) echo "Gooding afternoon";; *) echo "Sorry, $YES_OR_NO not recognized. Enter yes or no." exit 1;; esac exit 0
11. loop:
while loop:
echo "Enter password:" read TRY while [ "$TRY" != "secret" ];do echo "Sorry, try again" read TRY done
for loop:
for FRUIT in apple banana pear;do echo "I like $FRUIT" done