01 Shell Scripting getting started

Linux Shell language background
  is currently the world is in the "Internet +" era, businesses are linked to the Internet, such as education Internet + Internet + sales and so on. Almost all goods, services, who can find online, and the Internet is the pillar of support behind Linux system. Linux operation and maintenance technical skills necessary to become IT technicians.
  Linux wants to reduce operation and maintenance costs necessary to carry out the operation and maintenance of automation, automated scripting language is required. The most popular scripting language that Shell and Python Linux systems. Python popular time is short, high learning costs. Shell is an automated language and almost all operation and maintenance company must use a lot of work is essential in session.
  Shell has the best programming learning related infrastructure, such as vim editor, SSH terminal, Linux command (focus on mastering regular expressions and grep, sed, awk command), Linux network deployment, log analysis troubleshooting, network services and so on.
Common types of network services: Crond, Rsync, Inotify, Nginx , PHP, MySQL, Keepalived, Memcached, Redis, NFS, Iptables, SVN, Git and so on.

Other scripting languages
  PHP web programming language. Perl language (thing of the past). python.

Linux Shell command role in the programming of
  each language has its own library, Shell is not alone, Shell use Linux commands, it can be seen as a Linux Shell command library.

Shell What is
the definition: Shell is a Linux / Unix command interpreter is responsible for translating the commands to the Linux kernel and returns the result of the operation, human-computer interaction is the intermediary interface.
Shell Category: Command Line Shell (CLI Shell), Graphical Shell (GUI Shell). Graphical Shell is little point with the mouse, the kind of the desktop interface. Shell command line similar to the windows of Dos.
Command Line Shell Category: There are two major categories, B Shell C Shell series and series, B Shell for Linux systems, C Shell for Unix systems.
Category B Shell: a Bourne Shell (sh), Korn Shell (ksh), Bourne Again Shell (bash) three types. Master key bash, it is the variety of Linux distributions default of Shell, is the mainstream Linux Shell. bash and ksh combines the advantages of sh, sh and ksh is developed out of.
  C Shell and include csh, tcsh two types. tcsh is an enhanced version of csh, have been used in Apple MAC system, but the general or csh.
A system can store multiple CLI Shell, CentOS 6 to see what the system can use the Shell command: cat / etc / shells

Command execution mode classification

  Interactive, batch. (Interactive that can only enter a command, the machine returns the corresponding result, and then enter a command and then return once the results. Batch is time to write a set of script commands, and then the machine disposable executed. Command Line Shell used Linux commands implementation is interactive, while Shell script of course is to use the batch execution)

What is Shell script
is defined: Shell scripts are using some kind of Linux Shell command corresponding Linux operating system as well as a batch operation programs written statement.
(Windows with Dos interpreter written in batch file operation is .bat file)
(Linux Shell interpreter with written documents that Shell script bulk operations, suffix .sh)
(all programming languages are the library functions and statements , and the statement by the keyword and variable composition and as for expression, not in the language category, such as 1 + 2 who can run, which belongs to the language).?
Shell script reads: computers and other similar language, Shell scripting language It consists of two parts, Linux command and control statements. Also includes controlled variable definition statement statements, assignment statements, input / output statement, branch statement, a loop statement.
Shell scripting language: a weakly typed, interpreted language.
Shell scripting language advantage: more compatible with the system, command more suitable for partial processing system underlying business. Simple, easy to use and efficient.

Shell script to setup and execution
to vim editor surnamed
  editor has vi and vim, recommended vim.
  In the terminal vim editor from a permanent another name vi, the following two commands are input:

echo "Alias = VI 'Vim'" >> / etc / Profile
Source / etc / Profile
first line of the script
  end edit command cat path may correspond to a file, adding a single line of code. automatically saved after enter. Vi or vim alias to open the editor to start writing the script.
  The first line of the script is generally used to specify which Shell to execute the script. Selection input either of the following line of code specify which Shell.

#! / bin / bash
#! / bin / SH
#! / usr / bin / awk
#! / bin / sed
#! / usr / bin / TCL
#! / usr / bin / the Expect
#! / usr / bin / perl
#! / usr / bin / env python
  which expect an interactive language interpreter, perl was perl language interpreter, env python, python interpreter.
  If the first line does not specify Shell, use the default bash, in order to regulate, specify Shell.
  If you want the script to run on a variety of Shell, the first line can not specify Shell. First enter the corresponding runtime Shell, open the script in the Shell, which also can be.
  #! Characters, also known as the magic number, the kernel reads #! Path after selecting Shell.
  Why must write in the first line? We all know that after the # are comments except for the first line, so the magic number must be placed on top of the first line of the script, if the above code is not valid on other lines into comments.
  Attachment: Check bash version. Input Terminal:

bash --version
  to detect whether there are loopholes bash. Input Terminal:

env x = '() {: ;}; echo be careful' bash -c "echo this is a test"
  if the display be careful this is a test, the need to upgrade bash. Upgrade method is the following two commands are input:

Update the bash -Y yum
RPM -qa the bash
  If no display be careful, do not require upgrading bash.

Execute the script.
  Method One (recommended method): Input terminal command "Shell script name names", such as

bash script_name
  using this method specifies not only Shell, but also to avoid the problem of script does not execute privileges.

  Second, the method of a terminal enter the command "path / script name or ./ script name." The path can be absolute or relative path.
  This method of execution but the script must have execute permissions. Some people run regularly script, the script does not execute permissions cause the results to the time did not execute, resulting in significant losses.
Add execute permission to the script by using the command:

chmod + x script name
  Method three is the "source script name" or. "Script Name", there must be a space after the point.
  The method of point source or different from other methods. Methods I, II (Shell and path) to run the script will open a new Shell process, executing the script file is closed, the script data is not left on the original Shell process.
  The script uses the source and the point of execution will not open the new process, the implementation of direct current Shell terminal in the process, after the implementation of closing the file without closing the current Shell process, the data still exists in the current Shell.
  And a method of three methods, illustrate two different (after # are comments):

[root @ andy ~] # cat test.sh #cat command to view the test file, there are bar output statements
echo "userdir =` pwd` "
format [root @ andy ~] # bash test.sh # use" Shell Script "of execute the script.
/ home / root pwd path # is successful, the representative of the output variables
[root @ andy ~] # echo $ userdir # this time has been finished, and then try to output variable
# result is empty
[root @ andy ~] # source test.sh # using "source script" format to execute the script
/ home / root # successful execution
[root @ andy ~] # echo $ userdir # this time has been finished, and then try to output variables
/ home / root # again to display the contents of variables
  so, remember live source and point script execution method is carried out in the current Shell. After the implementation of the variables are still in Shell.
  The fourth method is: "sh <script name" or "cat Script Name | sh"
  This method is not used, this is a style all their own, sometimes less can write a few cycles, but not the standard, is not recommended.

  After the execution of the script system is to find the environment variable ENV, then turn ENV variable load several environmental files, start executing the script.
  The order of execution: from top to bottom, left to right. If you are sub-script calls the statement is executed, the first complete implementation of the sub-script, and then return to continue with the parent script.

Shell script development specification
  first line specifies Shell.
  Plus the beginning of the script version, copyright and other information.
  Try not to use Chinese.
  The script file extension is .sh.
  Script files are saved in a fixed path, such as / server / scripts.

Writing skills:
  a pair of symbols hit out to add content inside.
  Flow control statements to finish formatting, and then add content.
  Indent suitably used to make the code legible.
  Conventional string enclosed in double quotes, with a strong reference single quotes, anti command reference marks.

Guess you like

Origin www.cnblogs.com/Nonstopcoding/p/11183100.html