shell script vernacular knowledge (original)

Article handwriting fight to fight, of course, is not content himself created, the purpose is to use the vernacular and understand better record SHELL script, use of them! 

 

   ! (Exclamation point) can not be printed in double quotation marks, single quotation marks can only be used to print the output. But at the bash environment, to output an exclamation point must use single quotes. This is because the use of the exclamation point to open the history command reference memory settings by default, you can use set + H turn off the setting, then you can use the exclamation point output. (Done)

 

If the echo without any quotation marks, it obviously can not output the semicolon ";", because the semicolon will be parsed as command connection symbol shell. If you; do not need to add a line break, ""
but only in single quotation marks can not expand variables, use double quotation marks and bad output exclamation point, so the echo command to overcome incurable diseases method is a reference to a special symbol separately.
echo -e recognizes escape symbols and special significance, such as line breaks, n, tab \ T, escape \ and the like.
By default, without the echo -n end of each row will add line break, using echo cancellation branch -n output. (Done)

 

echo can control the font color and background color output.
Common font color: Reset = 0, 30 = black, red = 31, green = 32, 33 = yellow, blue = 34, 35 = purple, blue = 36, 37 = white.
Common background color: Reset = 0, 40 = black, red = 41, green = 42, 43 = yellow, blue = 44, 45 = purple, blue = 46, 47 = white.
Font control options: 1 for highlighting, underlining 4, 5 represents flicker.
Because of the need to use special symbols, so the need to meet the -e option to recognize special symbols. (Done)

 

Use printf output can be more regular and more formatted. It refers to the C language printf command, but slightly different.
Printf can be specified using the width of the string, to achieve left alignment (using the minus sign -), right-aligned (the default), and the like fractional output format.
Note that most of printf two points:
(. 1) printf default is not the end of a line feed, it is not the same echo, so to manually add "\ n" change number;
(2) printf format output only, does not change any result, the floating-point format at the output, the result is the same float, just change the results displayed. (Done)

 

Linux can be used in a semicolon ";", and the double number "&&" and the double pipe "||" is connected to a plurality of commands. Single "&" symbol is also connected to a symbol count command, except that it is placed in front of the background command, the command can be executed in parallel achieved disguise.
There is no logical relationship between the command: semicolon. Semicolon connection command sequence will be performed in order from front to rear, but there is no relationship between the logical command ends semicolon, all write commands will eventually be executed, even if the leading semicolon command error does not affect subsequent commands in . (DONE)

&& and symbols: logic. && command connection will be back in the order of execution in the past, but only when properly executed before the execution command1 command2, if command1 is not performed correctly, is not performed command2. In bash, to judge by the predefined variable "$?" Command is executed correctly, if the previous command, "$?" The value of 0 indicates the correct implementation, any other value indicates not executed correctly. (DONE)

logical OR. Command || connection will be back in the order of execution in the past, but only if command1 incorrect execution before the implementation of command2, command1 is not performed correctly execute command2. && and || symbols are short-circuited, having a logical relationship between the left and right command symbols.
Summary: The semicolon is no logical relationship between the two at the time of command execution, regardless of the success of failure will be executed, && and sign only the previous command correctly, will perform back. || double vertical lines only previous command is incorrect, fails will be followed by the second command.
Both commands execute semicolon. No logical connection relationship between a command
&& properly before performing a first second. There are connections between the command logic
|| before the first execution of the second failure. Logically related (DONE) is connected between the command


when the operation itself of the variable, $ is not used, when the operating variable values, use $. (Done)

Scripting languages are weakly typed language, variables usually do not require specially sound that even do not need to initialize the interpreter interpreted by the operator when the script runs, the interpreter know at what time variable is what type, so you can use direct assignment. bash, the variables are the default string type, regardless of whether the use of quotation marks assignments are stored as a string. (DONE)

unset unset --- str variable is released, the front str without ¥
interim upgrade ordinary variable as an environment variable: export str or export str = "value" on assignment, so $ str current shell and can be sub-shell in use, but quit script or re-login shell will cancel export effect. (Done)

the definition of read-only variable: readonly str. At this time will not be able to modify variable values can not be unset variables, only to re-login shell to continue using read-only variables. (Done)


When using the "$ {}" reference variable, the variable name before you can view with the # character length of the variable. Are also considered the space length (DONE)

DECLARE [-aixr] Define variable type variable is changed to declare -x environment variable (DONE)

location specific variables and the variable
$ a return instruction code is executed on ?:, standard output return 0 , that is performed properly, otherwise the standard error output.
$$: The current PID shell of. In addition to executing bash commands and shell scripts, $$ will not inherit the value of the parent shell, and other types of sub-shell are inherited.
$ BASHPID: current PID shell, this and "$$" is different, because each shell's $ BASHPID are independent. The "$$" Sometimes a parent will inherit the value of the shell.
Background process PID $ !: The most recent execution.
$ #: The number of statistical parameters.
$ @: All individual parameters, such as "a" "b" "c " "d".
$ *: Overall all parameters, such as "abcd".
$ 0: script name.
$ 1 ...... $ n: parameter location. (DONE)

variable assignment:
$ {parameter: -word}: If the parameter is null or undefined, to expand the variable "word"; otherwise expand to parameter values;
$ {} parameter-Word: and $ {parameter: -word} is almost equivalent, but in addition to the parameter set is empty, the result variable will be null, rather than word. There /etc/init.d/httpd in this usage.
$ {parameter:
$ {parameter: = word}: If the parameter is null or undefined, the variable assignment (assignment note is not expanded) as "word", otherwise parameter itself;
$ {parameter = Word}: This is not in man bash in defined, but tested, equivalent to $ {para: = word}.
$ {parameter: offset}: Take substring, a character begins to take from the offset at the last character;
$ {Parameter: offset: length}: Take substring, starting from the offset at a character, take lenth length substring; (done)


in the configuration file variable values can be used in a script
to invoke the configuration file in the script, direct the use of source config_file or config_file.
service startup script supports the configuration file: / etc / sysconfig / service script of the same name profile (DONE)


scale. 4 = mean retention time is calculated to four decimal places scale of decimal places are decimal places (DONE)

BC << EOF EOF is redirected to the reverse input command execution results to the BC (DONE)

echo $? is an integer end SHELL script script code executes successfully, then get is 0 if the script fails then it is other than 0 integer. (Done)

 


LINUX $? Exit status
0 The command completed successfully
1 General Unknown Error
2 is not suitable SHELL command
126 is not executable command / command execution error
127 not found command
128 Invalid parameter Exit
128 + X X associated with LINUX signal of serious errors
130 CTRL + C command terminated
exit status code 225 outside the normal range (DONE)

 

Structured command allows you to change the order of program execution!
if command
then command
fi - it can be understood as if. . on. . Of course, if then after the command to be executed successfully. It will then perform back then of.
if the command; then
command
fi --- command behind the semicolon on the same line then allows only format will be better spectacle
if then back fi then you can execute multiple commands. Provided that if the return value is 0 that is executed successfully. (DONE)

IF (execute a command)
the then (the first command is executed successfully execute here)
the else (the first command is executed successfully executed here)
fi (statement ends) --- if-then-else ( done)

-n -z and can check whether a variable containing the data (DONE)


for var in List
do
Commands
DONE   

for nothing noted loop point format, the 'single quotation marks to escape or by double quotes.
Special environment variable internal field separator IFS = ¥ '\ n' means the script is recognized only IDS line breaks, tabs, spaces other is not recognized. (DONE)

IFS.OLD = $ IFS
IFS = $ '\ n-'
<the new IFS value>
IFS = $ IFS.OLD thus ensuring large scripting environment subsequent use the default IFS value
IFS = $ '\ n': ; "in this script is the semicolon colon double quotes also be assigned as the field separator (DONE)

 

C language for loop
for ((I =. 1; I <= 100; I ++))
do
echo "the this Number IS Next $ I"
DONE
$ 0 an until when performing the return value is not 0 stops loop
break execution to break ends the number of cycles of execution cycles break nn = (DONE)

loop is part of the programming. bash shell script that can be used to provide three cycles in order.
(1) for command allows you to traverse a series of values, provide good, contained in a variable by file extension or get matching file names and directory names either on the command line.
(2) while using the common command or commands provide a test command based on the command cycle conditions. Only generate a set of command exit status of 0, while the iteration loop will continue specified in the command (or condition).
(3) until the command also provides a method for iterative command, but it's iteration is to establish the foundation to produce a non-zero exit status on the command (or condition), this feature allows you to set a front end of an iteration must be met condition.
(4) circulation can be combined in a shell script, to generate a multilayer cycle. Providing bash shell and continue break command allows you to change depending on the value of the cycle of the normal process cycle.
(5) bash shell also allows the use of standard command redirection and piping loop to change the output. You can use redirection to the cycle of the output redirected to a file or another command. This is a control shell script provides a wealth of features. (DONE)

basename command returns the script name does not contain a path.
For example, bash /home/dp/dp.sh
this file is /home/dp/dp.sh how to get the file path name does not get it?
dp.sh CAT
name = $ (basename $ 0)
echo at The IS File: $ name (DONE)

$ 0 is the name of the program is the file name is a special variable
$ # is a special variable added to the script, the script can be executed after the added draw the total amount of parameters.
Column: CAT dp.sh
the this dp $ # Fiel
./dp.sh 1 2 3 4 5 6
the this dp 5 File
$ variable plus special braces also test script execution several parameters of the command {#)!}
$ And @ * $ special variables are:
(1) $ @ will be a separate process for each parameter.
(2) $ * All parameters will process the data as a single parameter.
Column: dp.sh 2. 5. 4. 3. 1
$ * = 2. 5. 4. 3. 1
$ $ @ = @ =. 5. 4. 3 = $ @ $ 2 $ @ = @ =. 1 (DONE)

Shift command (special variables)
Use this command parameters will be to
shitf 2
echo "Number the File: $ 1"
./dp.sh 1 2 3 4 5 0 $ move does not move left
column: CAT dp.sh
echo "IS at the File: * $"
at the File IS: 1 . 5. 4. 3 2
Number File:. 3
Use the above parameters shift2 will perform two move to the left, so that you easily skip the unnecessary parameters! (Done)



- double dash is to be separated from the parameters and options
listed: -d) the this DP Number -d
-C) -C Number the this DP
-) Shift
BREAK ;;
redirection:
0 stdin standard input
1 stdout standard output
2 stderr standard error
Liezi: ll 2> dp.txt 1> dp1.txt redirected to the corresponding output the corresponding file
ll &> dp.txt, whether correct errors are entered in the file but this disadvantage is inconvenient to view the error message, all mixed together. Better still separate. (DONE)

Exec
Exec command tells the shell during script execution, the redirection of a specific file descriptor.
Example: exec 1> dp.txt

 

# Emptied the contents of the file
CAT / dev / null> testfile
# redirect write output teeecho "the sun" | tee $ file (done)

 

Guess you like

Origin www.cnblogs.com/Michaelblue/p/11441874.html