Shell Scripting quick start


What is a shell script?
Shell script (English: shell script), also known as shell command script and programmed script, is a computer program and text file. The content is composed of a series of shell commands, which operate after being directly translated by UNIX shell. It is designed as a script language, and its operation mode is equivalent to that of the literal translation language. UNIX shell plays the role of command-line interpreter when reading shell scripts After, run the shell commands in sequence, and then output the results. Shell script can be used for system management, file operation, etc.

The script example is as follows:

cd ~
mkdir shell_tut
cd shell_tut

for ((i=0; i<10; i++)); do
        touch test_$i.txt

What does the content of this example mean?

Line 1: specify the script interpreter. Here, / bin / SH is used as the interpreter
Line 2: switch to the current user’s home directory
Line 3: create a directory shell_ tut
Line 4: switch to shell_ Tut directory
Line 5: cycle condition, 10 cycles in total
Line 6: create a test_ 1…10. Txt file
Line 7: end of loop

CD, MKDIR, and touch are all built-in programs of the system. They are usually in the / bin or / usr / bin directory. For, do and done are keywords of SH scripting language.

What is the conceptual difference between shell and shell script?

Shell refers to an application that provides an interface through which users access the services of the operating system kernel. Ken Thompson’s SH is the first UNIX shell, and windows explorer is a typical graphical interface shell.
Shell script is a kind of script program written for the shell. The shell in the industry usually refers to shell script, but readers should know that shell and shell script are two different concepts. For the sake of habit and brevity, the “shell programming” in this article refers to shell script programming, not developing the shell itself (such as Windows Explorer extension development)

Shell programming is the same as Java and PHP programming, as long as there is a text editor that can write code and a script interpreter that can explain and execute.

The current mainstream operating systems support shell programming. The shell programming described in this document refers to the shell under Linux. It basically talks about the functions under the POSIX standard. Therefore, it is also applicable to UNIX and BSD (such as Mac OS).

Linux comes with a shell interpreter by default.

Mac OS
Mac OS not only brings the two most basic interpreters, SH and Bash, but also has built-in interpreters such as KSH, CSH and Zsh.
Emulator on Windows
Windows does not have a built-in shell interpreter when it leaves the factory. It needs to be installed by itself. In order to use grep, awk, curl and other tools at the same time, it is best to install a cygwin or MinGW to simulate the Linux environment.

script interpreter
Bourne shell, the POSIX (portable operating system interface) standard shell interpreter, whose binary file path is usually / bin / sh, is developed by Bell Labs.
This article is about sh. if you use other languages as shell programming, please refer to the documentation of the corresponding language.

Bash is a substitute for Bourne shell and belongs to GNU Project. The binary file path is usually / bin / bash. Bash, SH, and shell are often mixed in the industry. For example, you will often see in the copybook of recruiting operation and maintenance engineers: familiar with Linux bash programming and proficient in shell programming.
In CentOS, / bin / SH is a symbolic link to / bin / bash:

[[email protected]~]# ls -l /bin/*sh
-rwxr-xr-x. 1 root root 903272 Feb 22 05:09 /bin/bash
-rwxr-xr-x. 1 root root 106216 Oct 17  2012 /bin/dash
lrwxrwxrwx. 1 root root      4 Mar 22 10:22 /bin/sh -> bash

But not on Mac OS, / bin / SH and / bin / bash are two different files, although their sizes differ by only about 100 bytes:

iMac:~ keke$ ls -l /bin/*sh
-r-xr-xr-x  1 root  wheel  1371648  6 Nov 16:52 /bin/bash
-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52 /bin/csh
-r-xr-xr-x  1 root  wheel  2180736  6 Nov 16:52 /bin/ksh
-r-xr-xr-x  1 root  wheel  1371712  6 Nov 16:52 /bin/sh
-rwxr-xr-x  2 root  wheel   772992  6 Nov 16:52 /bin/tcsh
-rwxr-xr-x  1 root  wheel  1103984  6 Nov 16:52 /bin/zsh

High level programming language
Theoretically, as long as a language provides an interpreter (not just a compiler), it can be competent for script programming. Common interpreted languages can be used for script programming, such as Perl, TCL, python, PHP and ruby. Perl is the oldest script programming language, and python has become a preset interpreter for some Linux distributions over the years.
Compiled languages can also be used as script programming as long as there is an interpreter. For example, C shell is built-in (/ bin / CSH), Java has a third-party interpreter jshell, and ADA has a charged interpreter adascript.
The following is an example of a PHP shell script (assuming the file name is test. PHP):

for ($i=0; $i < 10; $i++)
        echo $i . "\n";


/usr/bin/php test.php


chmod +x test.php

How to choose a shell programming language

If you have mastered a programming language (such as PHP, python, Java, JavaScript), it is recommended that you directly use this language to write scripting programs. Although some places will be a little wordy, you can make use of your experience in this language field (unit testing, single step debugging, IDE, third-party class library).

The new learning cost is very small. Just learn how to use shell interpreters (jshell, adascript).

Simple and advanced
If you feel that writing shell scripts in familiar languages (such as Java and C) is too verbose, you just want to do things such as backing up files, installing software and downloading data. Learning to use SH and Bash will be a good idea.
Shell only defines a very simple programming language. Therefore, if your script program is complex or the data structure to be operated is complex, you should still use scripting languages such as Python and Perl, or high-level languages you are already good at. Because SH and Bash are weak in this aspect, for example:

Its function can only return strings, not arrays
It doesn’t support object-oriented, and you can’t implement some elegant design patterns
It is interpreted and executed at the same time. It is not even a precompiled version of PHP. If your script contains errors (such as calling non-existent functions), as long as it is not executed to this line, there will be no error.

Environmental compatibility
If your script is provided to other users, using SH or bash, your script will have the best environmental compatibility. Perl has long been a standard configuration for Linux, and python has become a standard configuration for some Linux distributions in recent years. As for Mac OS, it installs mainstream programming languages such as Perl, python, ruby, PHP and Java by default.

First shell script

to write
Open the text editor and create a new file with the extension sh (SH stands for shell). The extension does not affect the execution of the script. Just see the meaning of the name. If you write a shell script in PHP, the extension is in PHP.
Enter some code. The first line is generally as follows:

“#!” Is a convention tag that tells the system what interpreter this script needs to execute.


There are two ways to run shell scripts:

As an executable program

chmod +x

Note that it must be written in English/ test. SH, not test SH, running other binary programs is the same. Write test directly SH, the Linux system will go to the path to find out if it is called test SH, and only / bin, / SBIN, / usr / bin, / usr / SBIN, etc. are in the path. Your current directory is usually not in the path, so it is written as test SH will not find the command. Use it/ test. SH tells the system to find it in the current directory.
To run the bash script in this way, the first line must be written correctly so that the system can find the correct interpreter.

The “system” here is actually the shell application (imagine Windows Explorer), but I deliberately wrote it as a system for easy understanding. Since the system refers to the shell, can a script using / bin / sh as the interpreter omit the first line? Yes.

As an interpreter parameter
This operation mode is to directly run the interpreter, and its parameters are the file name of the shell script, such as:

/bin/php test.php

The script running in this way does not need to specify the interpreter information in the first line, and it is useless to write it.


Define variables
When defining a variable, the variable name does not add a dollar sign ($), such as:


Note that there can be no spaces between variable names and equal signs, which may be different from all programming languages you are familiar with.
In addition to explicit direct assignment, variables can also be assigned with statements, such as:

for file in `ls /etc`

Use variables
A defined variable can be used as long as the dollar sign is added in front of the variable name, such as:

echo $your_name
echo ${your_name}

Curly braces outside the variable name are optional, whether they are added or not. Curly braces are added to help the interpreter identify the boundary of the variable, such as the following:

for skill in Ada Coffe Action Java; do
        echo "I am good at ${skill}Script"

If you do not add curly braces to the skill variable and write echo “I am good at $skillscript”, the interpreter will treat $skillscript as a variable (its value is empty), and the code execution result will not be what we expect.

It is recommended to add curly braces to all variables, which is a good programming habit. When IntelliJ idea writes shell script, the IDE will prompt for curly braces.

Redefine variables
Defined variables can be redefined, such as:

echo $your_name

echo $your_name

This is legal, but note that $your cannot be written in the second assignment_ Name = “Alibaba”, the dollar sign is added when variables are used.

Lines beginning with “#” are comments and will be ignored by the interpreter.
multiline comment
There are no multiple lines of comments in SH, only one # sign can be added to each line. Like this:

#This is a script for automatically typing IPA, which is written based on IPA build of weblogs:

#Function: automatically package etao IOS app, and the output is IPA package of 14 channels
#Features: automatic packaging, no need to enter any parameters

#####Start of user configuration area#####
#Project root directory. It is recommended to put this script in the project root directory. There is no need to change it here
#Application name, and ensure that it matches the target under product in Xcode_ name. App name is consistent
#####End of user configuration area#####

What if you encounter a large piece of code that needs to be annotated temporarily during the development process and cancel the annotation later? Adding a # symbol to each line is too laborious. You can enclose this section of code to be annotated with a pair of curly braces and define it as a function. If there is no place to call this function, this code will not be executed and achieve the same effect as the annotation.

character string
String is the most commonly used and useful data type in shell programming (there are no other types to use except numbers and strings, ha ha). String can use single quotation marks, double quotation marks or no quotation marks. The difference between single quotation marks and double quotation marks is similar to PHP.

Single quotation mark

str='this is a string'

Restrictions on single quote strings:
Any character in single quotation marks will be output as is, and the variable in single quotation marks string is invalid
A single quotation mark cannot appear in a single quotation mark string (nor after using an escape character for a single quotation mark)

Double quotation mark

str="Hello, I know your are \"$your_name\"! \n"

You can have variables in double quotes
Escape characters can appear in double quotation marks

String operation

Splice string

greeting="hello, "$your_name" !"
greeting_1="hello, ${your_name} !"

echo $greeting $greeting_1

Get string length:

Echo ${#string} # output: 4

Extract substring

string="alibaba is a great company"
Echo ${string: 1:4} # output: Liba

Find substring

string="alibaba is a great company"
Echo ` expr index "$string" is ` # output: 8. This statement means to find out the position of the word is in this sentence

The Conduit

Conditional judgment
Process control
Unlike Java, PHP and other languages, the process control of SH cannot be empty, such as:

if (isset($_GET["q"])) {
else {
        //do nothing

You can’t write this in SH / bash. If the else branch has no statement execution, don’t write this else.
Also note that if [$foo – EQ 0] in SH is different from the parenthesis after if in Java / PHP. It is an executable program (like CD, LS and grep). Can’t you think of it? On CentOS, it is in the / usr / bin directory:

ll /usr/bin/[
-Rwxr-xr-x. 1 root 33408 June 22, 2012 / usr / bin/[

Because square brackets are an executable program here, spaces must be added after square brackets, which cannot be written as if [$foo – EQ 0]

If else statement


if condition

Write in one line (for terminal command prompt):

if `ps -ef | grep ssh`;  then echo hello; fi

Fi at the end is if spelled upside down,

if else

if condition

if else-if else

if condition1
elif condition2

for while

As demonstrated in the example at the beginning:

for var in item1 item2 ... itemN

Write in one line:

for var in item1 item2 ... itemN; do command1; command2… done;

C style for

for (( EXP1; EXP2; EXP3 ))


while condition

Infinite loop

while :

Recommended Today

MySQL related

Indexes Bottom structure Disadvantages of hash table index: Using hash storage requires adding all files to memory, which consumes more memory space If all queries are equivalent queries, the hash is indeed fast, but in the actual scene, more data is found, and not all queries are equivalent queries, so the hash table is not […]