"Shell script Programming Series" Knowledge reserves and scripting of building specifications

Source: Internet
Author: User
Tags posix rar rsync


Objective


Learn about the knowledge reserves required for Shell script programming:



Vi/vim Editor Command VIMRC set to be proficient



Basic commands, more than 100 skilled



Basic and common Network Service commands to be: NFS, rsync, INotify, Lanmp, Sersync, SSH key


I. Shell scripting concepts and examples 1, what is a shell


The shell is a command interpreter that, at the outermost end of the operation attraction, is responsible for directly talking to the user, explaining the user's input to the operating system, and processing the output of a wide variety of operating systems, and outputting the output to the screen back to the user. This type of conversation can be interactive (from the keyboard input command to get the Shell's response immediately), or a non-interactive (script) way.


2. What is a shell script


When a command or statement is not executed at the command line, but is executed through a program file, the program is called Shell scripts or shell programs, shell programs are similar to a batch program (*.bat) under a DOS system. A powerful shell script can be formed by the combination of a series of commands or command statements, these commands, variables, and process control statements in a shell script.



Scripts for backup sites and databases developed under Windows using the batch program bat.



@echo off



Set date=%date:~0,4%-%date:~5,2%-%date:~8,2%



Mysqldump-uroot-poldboy-a-B > D:\bak\ "%date%". sql



Rar.exe a-k-r-s-ml D:\bak\ "%date%". Sql.rar D:\bak\ "%date%". sql



Del D:\bak\*.sql



Rar.exe a-k-r-s-ml D:\bak\ "%date%" \htdocs.rar D:\work\PHPnow\htdocs



example One, clear a simple command file for the message log file under/var/log


1 #Put all the commands in a file to form a script. Below is a simple shell script formed by commands
2 #To run this script as root
3 Clear log script, version 1
4 cd / var / log
5 cat / dev / null> messages
6 echo "Logs cleaned up."
7 Tip: / var / log / messages is the system's log file, which is very important 


Disadvantages:



1) not root user cannot execute



2) No Process control, no logic.



example two, including command, variable, and Process Control statement /var/log the message log script


1 #! / Bin / bash
  2 #Clear log script, version 2
  3 LOG_DIR = / var / log
  4 ROOT_UID = 0 #When $ uid is 0, the user has root user permissions
  5 #To run as root user
  6 if ["$ UID" -ne "$ ROOT_UID"]
  7 then
  8 echo "Must be root to run this script."
  9 exit 1
10 fi
11 cd $ LOG_DIR || {#If the cd is not executed successfully, execute the content in the braces
12 echo "Cannot change to nessarg directory"> & 2
13 exit 1
14}
15 cat / dev / null> messages
16 echo "Logs cleaned up"
17 exit 0
18 # 0 before exit means success, 1 means failure 


Three ways to empty a log


1 echo> test.log #Enter empty content
2> test.log
3 cat / dev / null> test.log #View black holes, redirect black holes to files
4 Application: Keep files and empty content 
3. The role of Shell program in operation and maintenance work


Shell scripts are good at handling plain text types of data, and almost all configuration files, log files (such as NFS, rsync, httpd, Nginx, LVS, etc.) in Linux are plain text-type files, so if you learn the Shell scripting language Well, can use it to play a huge role in the Linux system.



Below is Role of shell script in operation and maintenance image diagram:


4. Types of scripting languages


1) Types of Shell scripting languages



There are two main types of shells in unix/linux



Bourse Shell (SH ksh bash)



Bourne Shell (SH)



Korn Shell (Ksh)



Bourne Again Shell (bash)



POSIX Shell (SH)



C Shell (csh tcsh)



C Shell (CSH)



Tenex/tops C Shell (tcsh)



The Shell scripting language is a weakly typed language, and the more general Shell has the standard Bourne shell and C shell. Where the Bourne shell has been replaced by the bash shell.



2) What is the difference between shell script and Php,perl, Python language?



The Shell's advantage lies in handling the underlying business of the operating system. Can be executed with a large number of Linux commands and functions. One-click installation, alarm scripting, general business applications, shell development is simpler and faster.



Php,python Advantage lies in the development of operational tools, Web interface management tools.


5, the default shell of the common operating system


linux/is Bourne Again Shell (bash)



Solaris and FreeBSD default is the Bourse shell (SH)



Under Aix is Korn Shell (Ksh)



The default for HP-UX is the POSIX shell (SH)



The shell type in Centos Linux is (bash)



How to view: 1.echo $SHELL



2. grep root/etc/passwd



Ii. creating a canonical shell script



Creation of shell scripts:



Shell scripts are typically written in the editor (vi vim) and consist of UNIX/LINUX commands, bash shell commands, Program structure control statements, and annotation content



Script start (first line)



The first line of a canonical shell script indicates that the program (parser) executes the contents of the script, which is typically in Linux bash programming:



#!/bin/bash or #!/bin/sh



#! is also called the magic number, when executing a bash script, the kernel determines which program to use to interpret the contents of the script, which must be the first line at the top of the script, or a comment if it is not the first line.


 
 
1 #!/bin/bash
2 
3 echo "test start"
4 
5 #!/bin/bash
6 
7 #!/bin/sh
8 
9 echo "test end"


The following is the beginning of the common scripting language in Linux coding, the beginning of different language scripts should be added to the following corresponding language of the identity of the content:


    • #!/bin/sh
    • #!/bin/bash
    • #!/usr/bin/awk
    • #!/bin/sed
    • #!/usr/bin/tcl
    • #!/usr/bin/expect
    • #!/usr/bin/perl
    • #!/usr/bin/env python


if the script header does not specify an interpreter, then it is necessary to execute the script with the corresponding interpreter, for example:bash test.sh



Script comments



In the shell script, followed by the (#) of the comment, used to comment on the script, the comments are not executed, only to show that the comments can be self-contained, or follow the script command in line with the command, development script, if there is no comment, It's hard for others to understand what the script is doing and forget about it for a long time.



Execution of shell scripts:



When a shell script runs in a non-interactive manner, it first looks for an environment variable, env, which specifies an environment variable (usually. bashrc) and then executes from the environment variable file, and the shell starts executing the contents of the shell script when the env file is read.






"Shell script Programming Series" Knowledge reserves and scripting of building specifications


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.