Moment For Technology

10 minutes to get started with Shell scripting

Posted on Sept. 27, 2022, 8:54 a.m. by Benjamin Begum-Clark
Category: The back-end Tag: linux shell The command line


This article is a summary of my own learning and use. My writing style is not very good. If you have any questions, please feel free to communicate with me


What is the Shell

A Shell is an application that provides an interface through which users can access the services of the operating system kernel. A Shell script is a type of script written for a Shell. When we say shell, we usually mean shell scripts.

Environment and Tools

The Shell, like Java, PHP, and Python programming, requires only a text editor that writes the code and a script interpreter that interprets the execution.

Mac OS, Linux with shell interpreter, Windows more trouble, because Win7 professional edition and flagship edition of the default installation of PowerShell, standard edition and home edition is not installed, for convenience, it is recommended to install Cygwin

PHP and Python can also be used as Shell programming

PHP and Python are high-level programming languages, but you can also do Shell programming because you can also do scripting as long as you have an interpreter

Here is an example of a Python Shell Script (assuming the file name is :

#! /usr/bin/env PYTHon3 // Tells Python to look for Python in the system environment
# -* -coding: utF-8 -*- // set to UTF-8 encoding

for index in range(10):
Copy the code

Source: op_python_base

Here is an example of a PHP Shell Script (assuming the file name is op_php_base.php) :

#! /usr/bin/php? phpfor($i=0 ;$iThe  10;$i{+ +)echo $i;


Copy the code

Source: op_php_base

Why learn Shell

There are several reasons why you would want to learn a strange, obscure Shell when you can write scripts in PHP and Python

  • Environment compatibility, Win7 professional edition and flagship edition install PowerShell by default, standard edition and home edition is not installed, other mainstream operating systems are premade Shell interpreter, so use sh, bash, provided for others to use is very convenient, But PHP, Python, and so on need to be installed

  • If you want to do scheduled tasks such as checking whether processes exist, automatically backing up, or automatically deploying environments, synchronizing data between servers, etc. Sh and bash are your best choices

Sh and bash

Sh: Bourne Shell, a standard shell interpreter of Portable Operating System Interface (POSIX). Its binary file path is usually /bin/sh

Bash: Bash is a Bourne shell replacement, belonging to GNU Project, and the binary path is usually /bin/bash

The first shell script

Let's start with an example

I'm sure any of you who have written code will be familiar with the following code (assuming the file name is :

#! /usr/bin/env bash
mkdir code
cd  code
for ((i=0; i3; i++)); do
    touch test_${i}.txt
    echo "The shell is simple."  test_${i}.txt
Copy the code

Line 1: Find the interpreter of the specified script from the system path Line 2: Create folder named code Line 3: enter the folder created Line 4: for loop 3 times Line 4: create file Line 5: write information to the created file Line 6: end the loop

Mkdir, touch, CD, touch, echo are system commands. In the command line, you can directly execute for, do, done is the syntax of shell scripting language for loop

Source: op_base. Sh

Write a Shell

Create a new file with the extension sh (sh for shell). The extension does not affect the execution of the script. If you use PHP, the extension is PHP, and if you use Python, the extension is Python

The first line usually looks something like this:

#! /usr/bin/php
#! /usr/bin/env python3
#! /usr/bin/env bashCopy the code

#!" Is a convention flag that tells the system what interpreter the script needs to execute /env is looking in the system's PATH directory

There are two ways to run Shell scripts:

As executable program

chmod +x
Copy the code

In the first line, set the executable permission. In the second line, run

As a parameter

Copy the code


When you define a variable, you do not need to add a sign before the variable name, as in Python, but in PHP you need to add $to the variable name. For example:

Copy the code

Ps: there must be no space between the variable name and the equal sign, and no space after the variable;

Quotes in the Shell are similar to those in PHP; strings can be in single or double quotes

Restrictions on single quoted strings:

  • Any character in a single quote is printed as is, and variables in a single quote string are invalid
  • Single quotes cannot occur in a single quote string (not even if you escape single quotes)

Double quotation marks:

  • You can have variables in double quotes
  • Escape characters can appear in double quotes

There is no difference between single and double quotes in Python, but Python also has three quotes, within which characters are not escaped

Use the variable

For variables that have already been defined, use the appropriate $in front

echo $my_name
echo ${my_name}
Copy the code

The curly braces around the variable name are optional. The second form is recommended


Lines beginning with "#" are comments and are ignored by the interpreter.

Multiline comment

There are no multiline comments in sh, only a # sign per line. Like this:

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
# Author: jack
# Notes: 10 minutes to get started with Shell scripting
# Project home page:
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
Copy the code


Strings can be in single, double, or no quotes. The difference between single and double quotes is similar to PHP

Unlike other languages such as PHP and Python, the Shell has many data types. The common data types used in the Shell are strings, numbers, and strings.

Restrictions on single quoted strings:

  • Any character in a single quote is printed as is, and variables in a single quote string are invalid
  • Single quotes cannot occur in a single quote string (not even if you escape single quotes)

Double quotation marks:

  • You can have variables in double quotes
  • Escape characters can appear in double quotes

String operation

Concatenated string

echo $my_name $my_age
echo $my_name$my_age
Copy the code

Get string length

echo ${#my_name}
Copy the code

Intercept string

echo ${my_name:0:2}
Copy the code

Source: op_str. Sh

Shell array

Define an array

In the Shell, arrays are represented by parentheses, and array elements are separated by "Spaces". The general form of defining an array is:

name=(name1 name2 name3)
Copy the code

You can also define the components of an array separately:

Copy the code

Ps: No continuous subscripts can be used, and there is no limit to the range of subscripts

Reads an array of

The general format for reading array element values is:

${array name [subscript]}
Copy the code

Such as:

echo ${name[0]}
Copy the code

Use the @ sign to get all the elements in an array, for example:

echo ${name[@]}
Copy the code

Gets the length of the array

We get the length of an array the same way we get the length of a string, for example:

Get the number of elements in the array
echo $length

# or
echo $length

Get the length of a single element in an array
echo $length
Copy the code

Source: op_arry. Sh

Shell flow control

Unlike Other languages such as Java, PHP, and Python, the flow control of SH cannot be null, for example:

? phpif (isset($_GET["q"])) {
else{// do nothing}Copy the code

Do not write this in sh/bash. Do not write else if there is no statement to execute on the else branch


if condition1
elif condition2 
Copy the code


#! /usr/bin/env bash
if [ $a= =$b ]
        echo "a 等于 b"
 elif [ $a -gt $b ]
        echo "A greater than b"
 elif [ $a -lt $b ]
        echo "A less than b"
    echo "There are no qualifications."


Copy the code

Source: op_if. Sh

The for loop

The Shell's for loop is a bit like Python's

Python's for loop
for index in1, 2, 3, 4, 5:print(index);
Copy the code
Shell for loop, first way
for index in 1 2 3 4 5; do
    echo "index="$index
Copy the code
Shell's for loop, the second way
for ((i=0; i5; i++)); do
    echo "i="$i
Copy the code

Source: op_for. Sh

While statement

The while loop is used to continuously execute a sequence of commands and also to read data from an input file; Commands are usually test conditions.

while(( $int = 5))do
    echo $int
    let "int++"
Copy the code

Source: op_while. Sh

Shell combines system commands

In the field of character processing, there are grep, AWk, and SED. Grep is responsible for finding specific lines, AWK can split lines into multiple fields, and SED can realize write operations such as update, insert, and delete.

For example, periodically check whether nginx and mysql are shut down


name=(apache mysql)

exs_init[0]="service httpd start"
exs_init[1]="/etc/init.d/mysqld restart"

for ((i=0; i2; i++)); do
    echo "Check${name[i]}Does the process exist?"
    ps -ef|grep ${name[i]} |grep -v grep
        pid=$(pgrep -f ${name[i]})
        echo "`date +"%Y-%m-%d %H:%M:%S"` ${name[$i]} is running with pid $pid"  ${log}
        echo "`date +"%Y-%m-%d %H:%M:%S"` ${name[$i]} start success"  ${log}
Copy the code

Check whether the nginx and mysql processes exist. If they do not exist, restart them automatically. The script will write a log every time it is run, so you can check the log file. If the process does not exist frequently, I am afraid to investigate the deep reason.

Source: check_nginx. Sh

Edit the /etc/crontab file

crontab -e
Copy the code

Add a line at the end of the file:

*/5 * * * * /xxx/  /dev/null 21
Copy the code

Run the/XXX / script every five minutes, where XXX indicates the path

/dev/null 21 means that the shell command will not output any information to the console and will not output any information to the file.

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * command to be executed
Copy the code

The added configuration takes effect only after a restart

service crond restart
Copy the code
About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.