shell programming specifications and variables
First, the basic process of writing the script;
1.shell scripting language: to help administrators achieve mass to complete the task, in conjunction with scheduled tasks to automate operation and maintenance;
2. scripting process:
A directory is created, centralized repository script files;.
B scripted content;.
Vi script name. sh ## edit script files
! # / bin / bash ## statement interpreter
# ## annotation content can annotate some information to facilitate follow-up to see
the core command and control statements ## scripts
: wq ## save the file
c given script. file execute permission: chmod + x file
. d run the script;
3. statements script: which statement using an interpreter, all interpreters cat / etc / shells file can be used, the interpreter can be installed;
4. execute the script way:
. a path of execution (must have execute permissions);
relative path
absolute path
absolute or relative path (do not need to execute permissions) b.source script;
path c.sh -x script (do not need to execute permissions), and will display command and output;
Second, the basic knowledge of the script;
- Redirection: changing the state of the standard input and output;
: Redirect output to a location, replace all the contents of the original document;
: Redirect additional output to a location, add at the end of the original contents of the file;
<: Redirect input file to a location;
:; 2> to redirect error output
end of the redirect error output to append file: 2 >> ;
&>: mixing redirect the output, the output will be correct erroneous output;
1,2>: supra;
2. pipe character: the | before the command output as a conduit | an input command;
3.echo command : return the results and display;
syntax: echo [options] "needs to return the value of"
options: option -e if special characters appear not to escape, treated as an ordinary character;
in the return value \ n represents the force a line break, \ t represents tabs;
content options: "content \ t content" insert Tab
"content \ n content" forced lineThird, the application variable in the script;
1. Overview: variable is a basket, the basket is filled with the values of variables, just to take away the basket, then the value along with it taken away, is variable;
2. advantage: simplified input, the script is much more clear, improve the applicability of the script;
3. Category: custom variables, environment variables, a variable position, predefined variables;
4. custom-defined variables:
. a defined variables: variable name = variable value
b view variables: echo. variable name $
c using variables: command directly using the variable name $
d canceled variables: variable name the unset
e scope of variables:
local variables: variable name = variable value, such variables are only valid in the current bash ;
global variables: export variable name = variable value, such variables are valid in the current bash and bash in the current sub-;
Note: available bash bash command to open a child process of the current process, exit to return to the parent bash process;
. f variable value in use parenthesis:
"": weak references, performed together with a command in the special symbols will double quotes, for example: a = "-ld LS '
' ': strong reference, will be in single quotes $,", \ and other special symbol failure, for example: B = "$ A / etc /", B = '$ A / etc /';`:在定义变量时,引用命令,在值中引用变量等同于$(),''不可嵌套,$()可以嵌套;例:A=
-SH du / etc / `
$ (): the definition of variables, the command references, can be nested; Example: AC = $ (rpm -qf $ (which rz))
Note: When the tag value contains' single quotation marks, use \ 'single quote will be escaped;
G variable name can not use special symbols, usually in capital letters and underlined;.
H interactively define variables:.
the Read -p "prompt text messages"
variable names echo $
123
5. Define environment variables:
. A user's work environment environment variable name can not be modified, the value can be modified;
B common environment variables:.
the PATH = storage path provided external commands, separated by colons
TMOUT = set the login timeout in seconds
to set the number of history commands recorded HISTSIZE =
LANG = locale (vi / etc / sysconfig / i18n permanently modify the character set)
Note: when setting up a temporary environment variables (cancellation becomes ineffective) : Export the PATH =
c environment variables see:. env command to view all the current environment variables working environment;
/ etc / profile file for the user profile to perform each landing;
each user's home directory each .bash_profile separate user environment variable profile;
6. location variable and predefined variables:
. a script is used to pass the location variable parameters to the script, with a $ 1 through $ 9, except that all the command script itself or outside;
B predefined variables: users can only use, can not be created, can not be directly assigned;
$ 0: script itself or command itself
$ *: for all the parameters of the script
$ #: script parameter number system
A command $ ?: whether they were successful, the return value is 0 success, or failure
Note: In shell scripts, all paths must be an absolute path;