As a result of the work, several bash scripts were written in the previous months, primarily for automated testing, packaging, installation packages, and so on. Although compared to C + + programming, to be simple, stupid, but it in the Unix-like system can greatly improve the efficiency of the work. So here's a brief summary of some of the considerations in scripting.
1. Shell
Overview
The shell is an interface between the user and the Unix-like operating system kernel (kernel) to protect the kernel from misuse by the user and to create a shell (Shell)around the kernel.
By making a request to the shell, the shell interprets and passes the request to the kernel. While multiple shell requests can be written in a single file, they form a shell script that is itself an interpreted programming language (a relatively compiled programming language), and shell scripts support most of the program elements seen in high-level languages, such as functions, variables, arrays, and program control structures, At the same time, any command that can be typed at the Unix terminal prompt can be placed in a shell script file.
The more widely used shells are: Bourne Again shell (bash), Bourne Shell (sh), C Shell (csh), Korn Shell (ksh ) and so on, the default Shell in most Unix - like systems today is bash.
View the SHELL used by the current environment: "Echo $SHELL".
View the shell that the current release can use: "Cat/etc/shells".
2. Bash
Programming
Shell script file format,
1#!/bin/Bash2 3 function1 ()4 {5 }6 7 function2 ()8 {9 }Ten One # Main program A Echo"Hello World" # Print Hello world - ... - ... the...
A variable that starts with $ in bash,
$# |
The number of arguments passed to the script |
$* |
Values passed to all parameters of the script |
[Email protected] |
With $* |
$$ |
The process number of the script |
$! |
Process number of the last process running in the background |
$- |
Displays the current options used by the shell |
$? |
Displays the exit status of the command (or function), 0 is correct, and 1 is the error |
$ |
The program name of the shell script (file name) |
$n |
The nth parameter option for a script (or function) (starting with 1) |
String manipulation,
${#str} |
Returns the length of the string variable str |
${STR:N} |
Returns STR from nth character to last string (n starts from 0) |
${str:n:len} |
Returns STR starting with the nth character, Len length (n from 0) |
${str#pattern} |
Remove the first part of STR that matches the pattern at the beginning |
${str# #pattern} |
Remove most of the first part of STR that matches pattern |
${str%pattern} |
Remove the minimal portion of the end of STR that matches the pattern |
${str%%pattern} |
Remove the most part of the end of STR that matches the pattern |
${str/old/new} |
Replace the first occurrence of the old in Str with new |
${str//old/new} |
Replace all old in Str with new |
${str/#old/new} |
Replace the STR prefix with new with the old |
${str/%old/new} |
Replace the str suffix with new with the old |
= |
Determine if two strings are the same |
!= |
Determine if two strings are not the same |
-N |
Determines whether the string length is greater than 0 |
-X |
Determines whether the string length equals zero |
Note: The shell's built-in string operations perform better than awk and SED for various operations (at the very least, there is no need to start processes such as awk, SED, and so on). |
Test the status of the file,
-D |
is a directory |
-F |
is a file |
-L |
Whether it is a symbolic link |
-R |
Whether it is readable |
-W |
Whether it can be written |
-X |
is executable |
-A |
is not empty |
Numerical tests,
-eq |
equal |
-ge |
greater than or equal to |
-le |
less than equals |
-ne |
Not equal to |
-gt |
< P> greater than |
-lt |
less than |
String test,
-eq |
equal |
-ge |
greater than or equal to |
-le |
less than equals |
-ne |
Not equal to |
-gt |
< P> greater than |
-lt |
less than |
logical operators,
! |
Logical inversion |
-A |
Logic and |
-O |
Logical OR |
3.
script Debugging
Sh-x a.sh |
Trace the execution of the script, perform a replacement for each command in the script, then display, then execute |
Sh-v a.sh |
Print each line in the script as entered, and print one line |
Sh-n a.sh |
The script is checked for syntax, but does not perform |
4.
some tips
Masking command Printing |
cmd >/dev/null # Mask normal output CMD 2>/dev/null # Mask Error Output cmd >/dev/null 2>/dev/null # Mask normal output and error output cmd >/dev/null 2>&1 # Mask normal output and error output CMD &>/dev/null # Mask normal output and error output |
Whether the command executed successfully |
CMD && echo "Failed" | | echo "OK" |
Connection file 1 All Lines |
Paste–sd "," 1 # Connect with commas Paste–sd "" 1 # Connect with a space |
Filter Files 1 Duplicate rows |
Sort-u 1 |
Setting temporary environment variables |
In the script file, export acts on the current script, VAR=ENV_STR CMD environment variable only for this cmd (no need to export first, then unset) |
String Default value |
${str:-0} # If STR is not declared or is empty, set to 0, can be used to determine the command line or function parameters |
Special Parameters-- |
echo "Abc-a" | grep---I #--Subsequent arguments are not resolved as options |
Get a soft link to a file |
Readlink file |
Emptying a file (or creating a file) |
echo "" > File Cat/dev/null > File > File |
Multi-process in shell |
Use (cmd) to fork a child shell to execute the command, and wait for all child processes to end with the wait command |
...... |
...... |
To be supplemented in the future |
|
[Shell] Bash Programming Summary