Shell Script Programming Learning Note-shell Scripting Basics Introduction

Source: Internet
Author: User
Tags php language posix

A Shell script Introduction and First specification shell Script Description 1.1 Shell script Introduction What is 1.1.1 shell?

The shell is a command interpreter which is responsible for directly working with the user in the outermost layer of the operating system to interpret the input from the user to the operating system and to process the output of various operating system inputs to the screen to return to the user, This dialog can be interactive Yes (from the keyboard input command can immediately get the shell's response) or non-interactive (script way).

Part of XXX is where the shell is in the operating system.

1.1.2 What is a shell script

When a Linux command or statement is not executed under the command line (the statement executed by the command line is also a shell script), the program is executed as a shell script or shell program, and the shell program is similar to a batch program (extension *) under the DOS system. BAT) Strictly speaking the statement executed under the command line is also the shell as for loop.

Example 1. Clear a simple command script for messages log files under/var/log

Put all the commands in one file and build up the script, here is the simplest command to stack up the script, empty the log script.

Cd /var/logCat /dev/null > messagesEcho “Logs cleaned up.”

Expand: Three ways to empty logs and file contents

[[email protected] ~]# echo >test.log [[email protected] ~]# >test.log [[email protected] ~]# cat /dev/null >test.log

Scenario: Preserve files and empty content.

Two Shell language and category Introduction 1.1 Types of scripting languages

Types of Shell scripting languages

There are two main categories of UNIX and Linux

(1) Bourne shell includes (SH, ksh and bash)

Bourne Shell (SH)

Kor n Shell (Ksh)

Bourne Again Shell (bash)

POSIX Shell (SH)

(2) C shell includes (CSH and TCSH)

C Shell (CSH)

Tenec/tops C Shell (tcsh)

Shell scripts are weakly typed languages the most commonly used shells are the standard Bourne shell (SH) and C shell (CSH). Where the Bourne shell (SH) has been replaced by the bash shell. But we are accustomed to call it sh.

In Linux can be viewed under the/etc/init.d/shells system support shell, the first three are more commonly used, the most commonly used is/bin/sh, in the work to execute scripts with/bin/sh we can use the first one. /sbin/nologin indicates login.

1.2 The differences and advantages of the shell and the python|perl|php language

The advantage of the shell is that it handles the underlying business of the operating system because there are a lot of system commands to support it. Python,php's advantages lie in the development of operational tools, Web interface management tools, and the development of Web services.

1.3 Default shell for common operating systems

Linux is Bourne Again shell (bash)? View method Echo $SHELL

Salaris and free BSD default is the Bourne shell (SH)

Under Aix is Korn Shell (Ksh)

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

Three establishment and execution of shell scripts 1.1 shell scripts

Shell scripts are typically done in the Shell editor using the Vim editor, where the first line of the script indicates which program (interpreter) executes the contents of the script, usually using the #! /bin/bash or #!/bin/sh, if the first line is not written, it is interpreted with the default shell of the system.

The difference between 1.2 sh and bash

Use ls–l/bin/sh to see the soft connection that SH is bash

It is recommended to use standard notation #! /bin/bash, bash–version View Shell version

1.3 Bash Vulnerability (shell break)

The vulnerability is a vulnerability in software that controls the command prompt of a Linux computer.
Bash is a Unix shell written for the GNU program. Its name is a series of abbreviations: Bourne-again shell, Bourne Shell is an early and important shell, written by Steve Burn around 1978, and released with version 7 Unix.
Cyber security experts have warned that a frequently used fragment "Bash" in open source software Linux found a security vulnerability that could threaten computer users more than the "Heart Bleed" (Heartbleed) flaw that erupted in April 2014. The above from the official, you can search in Baidu "shell hole" view, the following is to view the address. a0%b4%e5%a3%b3%e6%bc%8f%e6%b4%9e

1.4 Execution of Shell scripts

When a shell script runs in a non-interactive manner (file mode), it first finds the environment variable env, which specifies an environment file (. BASHRC bash_profile/etc/bashrc/etc/profile) and then executes from that environment variable, The contents of the shell script are then executed.

The system environment variables do not need to be defined, but the system environment variables are redefined in the script when using Crond scheduled tasks, because Crond can recognize few system environment variables.
Variables can be defined in the/ETC/PROFILE system environment variable, and can be referenced directly in the script once added. For example, add export Name=zhangsan at the bottom of the/etc/profile, and note that the NAME here is capitalized. We're done. We use the source to restart the script to make the changes result, without restarting the system.

So sometimes the things you want to define are used in all scripts and can be defined in/etc/profile.

Three ways to execute 1.5 shell scripts

(1) Bash.script-name and Sh.script-name (recommended) Take the shell full path when performing timed tasks.

(2) Path/script-name full path plus script or./script-name (Execute script under current path) with the second must have execute permission.

(3) Source Script-name or./script-name (note ".") Point number)

1.6 The difference between source and SH, bash

SOURCE or "." The point number loads the executed script, and the variables (including function) values in the script after the end of the script still exist in the current shell and SH and bash do not. So when it comes to shell scripting, if there is a requirement in the script to reference the content in other scripts or the configuration file is best used "." The dot or source loads the script or configuration file at the beginning of the script and then the contents of the script and the variables and functions in the file can be called from the source.

For example: Execute a script with sh, such as.

Here echo $user the content of the output is empty, because the user variable is not saved with the SH call script.

Let's take source, for example, as

We can also refer to source and "." In the script. No.

Some services are also used "." When invoking the system function library under/etc/init.d/. Point number and source, let's look at the system function library/etc/init.d/functions

You can also call the library with source, and the action prints out very flashy results, such as

The action in/etc/init.d/functions is a function defined in the

We can also define the functions ourselves in the functions, and then we can invoke the custom function. Such as

You can also send a reference, such as

Four Basic specifications and habits of shell script Development 1.1 specifying the script interpreter
1.2 Opening plus copyright information
#Date: 2018-01-03 时间#Author: Create by zbf谁创作的#Mail: [email protected] 邮箱地址#Function:This scripts function is ... 脚本是干啥的#Version: 1.1  版本
1.3 Do not use Chinese comments in the script (can use, best not to use)

Try to use English comments to prevent the computer or switching system environment after the confusion of Chinese characters.

1.4 script with. sh extension


1.5 paired content once written to prevent omissions such as

[] brackets, {} curly braces, ' single quotes, ' ' double quotes, ' '
Brackets [] At both ends, a space will be preceded by the parentheses [] move the cursor to the empty two spaces in brackets and then back one, then write the code.

1.6 Process Control Statement writing method

Process Control statements Write once and then add content such as if statement, for loop

Shell Script Programming Learning Note-shell Scripting Basics Introduction

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: 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.