Linux Shell Overview

Table of contents

Linux Shell Overview

Reasons to learn shell

what is shell

Origin of shell

Check the shells supported by the current system

View the current system default shell

Shell concept

Shell programming language

Shell is also a scripting language

use

Basic elements of shell scripts

Basic element composition:

Comments and style in shell scripts

Shell script writing specifications

Beginning of script

        Script automatically adds annotation copyright information

        Case --- Create a screensaver script

Try not to use Chinese comments in scripts

Use internal commands more often

There is no need to use the cat command

Read the error message carefully

Filename ends with sh

Code indentation:

shell script execution

method 1

Method 2 

Method 3

Method 4

bash shell basic functions

echo print command

Format

parameter

Colored fonts can be output

printf command

Format


Linux Shell Overview

Reasons to learn shell

        Easy to learn

        Interpreted language, which does not require compilation to execute

        For a qualified system administrator, it is very important to learn and master Shell programming. Through the shell program, daily maintenance work can be simplified to a great extent, freeing the administrator from simple repetitive work.

what is shell

Origin of shell

        In 1964, AT&T's Bell Labs, the Massachusetts Institute of Technology, and General Electric Company jointly began to develop a multi-user, multi-task operating system that could be installed on mainframes. The operating system was named Multics.

        In 1970, Dennis Ritchie and Thompson launched another new multi-user, multi-tasking operating system project, which they called UNICS

        In 1973, Unix was rewritten using C language. Through this writing, Unix can be ported to other minicomputers.

        In 1979, the first important standard UNIX Shell was launched in the 7th version of Unix and was named after the author Stephen Bourne. It was called Bourne Shell, or sh for short.

        In the late 1970s, C Shell was released as part of 2BSD UNIX, referred to as csh. Later, many other Shell programs appeared, mainly including Tenex C Shell (tcsh), Korn Shell (ksh) and GNU Bourne-Again shell (bash)

Check the shells supported by the current system

[root@localhost ~]# cat /etc/shells 

View the current system default shell

[root@localhost ~]# echo $SHELL

Shell concept

        Shell --- is a command interpreter program that can recognize various commands entered by the user and pass them to the operating system

Structure diagram:

        Only the operating system . The graphical interface and command line are only a bridge between the user and the kernel. Due to reasons such as security, complexity, and cumbersomeness, users cannot To directly contact the kernel (not necessary), you need to develop another program to allow users to use this program directly; the function of this program is to receive user operations (click icons, enter commands), perform simple processing, and then pass it to kernel so that users can indirectly use the operating system kernel

        The user interface and command line are this separately developed program, which is this layer of "agent". Under Linux, this command line program is called Shell. Shell is an application that connects users and the Linux kernel, allowing users to use the Linux kernel more efficiently, safely, and at low cost. This is the essence of Shell.

        The Shell itself is not part of the kernel. It is just an application written on the basis of the kernel. It is no different from other software such as QQ, Thunder, Firefox and so on. However, Shell also has its particularity, that is, it starts immediately after booting and is presented to the user; users use Linux through Shell. Without starting Shell, users cannot use Linux .

Shell programming language

Shell is also a scripting language

        Any code must eventually be "translated" into binary form before it can be executed on the computer

        In some programming languages, such as C/C++, Pascal, Go language, assembly, etc., all codes must be translated into binary form before the program is run, that is, an executable file is generated. What the user gets is the final generated executable file. , the source code cannot be seen. This process is called compilation, such a programming language is called a compiled language, and the software that completes the compilation process is called a compiler.

        Some programming languages, such as Shell, JavaScript , Python, PHP , etc., need to be translated while executing and will not generate any executable files. Users must obtain the source code to run the program. After the program is run, it will be translated immediately. After part of the translation is completed, part of the code will be executed. There is no need to wait until all the code is translated . This process is called interpretation, such a programming language is called an interpreted language or script language (Script), and the software that completes the interpretation process is called an interpreter.

        The advantages of compiled languages ​​- fast execution speed, low hardware requirements, good confidentiality , suitable for developing operating systems, large applications, databases, etc.

        The advantages of scripting language--- flexible use, easy deployment, good cross-platform performance , very suitable for Web development and gadget production

        Shell is a scripting language. After we write the source code, we don’t need to compile it, we can just run the source code directly.

use

        The advantage of shell scripts is to handle the underlying business of the operating system (applications within the Linux system are completed by shell scripts) because there are a large number of Linux system commands to support it. More than 2,000 commands are strong support for shell script programming, especially grep, awk, sed, etc. For example: one-click software installation, optimization, monitoring and alarm scripts, regular business applications, shell development is simpler and faster, and conforms to the principles of simplicity, ease of use and efficiency in operation and maintenance.

        The advantages of PHP and Python lie in the development of operation and maintenance tools, web interface management tools , web business development, etc. Handle one-click software installation, optimization, and alarm scripts. Regular business applications and other php/python can also be done. But the development efficiency and complexity are much worse than using the shell.

Proficient in basic knowledge of shell programming

        Proficient in using vi (vim) editor

        Proficient in basic Linux commands

        Proficient in the three text musketeers tools (grep, sed, awk)

        Familiar with common server deployment, optimization, logging and troubleshooting

suggestion

        Master the basic syntax of Shell scripts

        Develop your own script development style

        Start simple, simple judgment, simple cycle

        Imitate more, practice with more reference materials, and think more

        Learn to analyze problems and gradually form programming thinking

        Programming variable names should be standardized and expressed in camel case syntax.

        Don't use it as an ism, especially for novices

Basic elements of shell scripts

Basic element composition:

"#!/bin/bash"         on line 1

        Comments --- describe certain code functions

        Executable statements---implement the functions of the program

Comments and style in shell scripts

        Function ---By adding comments in the code, the readability of the program can be improved

        The traditional shell only supports single-line comments, which are represented by a pound sign "#" . From the beginning of this symbol to the end of the line, the content of the comment belongs

#comment1
#comment2
#comment3
...

         Multi-line comments---use colon ":" with here document, the syntax is as follows:

:<<'xxxx'
comment1
comment2
comment3
……
xxxx

        xxxx can be a character or a number , and the single quotation marks do not need to be added, but in order to prevent inexplicable accidents, such as character expansion and command substitution,

Shell script writing specifications

Beginning of script

        Set up the script interpreter at the beginning --- #!/bin/sh  or #!/bin/bash

        Other lines # represent comments

         Version, copyright and other information need to be added at the beginning of the program, such as:

# Date:创建日期
# Author:作者
# Mail:联系方式
# Function:功能
# Version:版本

        Script automatically adds annotation copyright information

[root@localhost ~]# vim  ~/.vimrc
autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()"

func SetTitle()
    if expand("%:e") == 'sh'
        call setline(1,"#!/bin/bash")
        call setline(2,"##############################################################")
        call setline(3, "# File Name: ".expand("%"))
        call setline(4, "# Version: V1.0")
        call setline(5, "# Author: Andy_Sun")
        call setline(6, "# Email: [email protected]")
        call setline(7, "# Organization: http://www.cnblogs.com/Andy_Sun/")
        call setline(8, "# Created Time : ".strftime("%F %T"))
        call setline(9, "# Description:")
        call setline(10,"##############################################################")
        call setline(11, "")
    endif
endfunc

 

        Case --- Create a screensaver script

[root@localhost ~]# vim cmatrix.sh
wget http://archive.ubuntu.com/ubuntu/pool/universe/c/cmatrix/cmatrix_1.2a.orig.tar.gz


tar xvf cmatrix_1.2a.orig.tar.gz

cd cmatrix-1.2a

yum install -y  ncurses-devel

yum install -y  gcc

./configure && make && make install

echo "Program installation complete !"
echo "Program installation complete !"
echo "Program installation complete !"

[root@localhost ~]# bash cmatrix.sh 

[root@localhost ~]# cmatrix 

Try not to use Chinese comments in scripts

        Don’t be stingy about adding comments. Necessary comments will help you and others understand the script logic and functions.

        Use English comments as much as possible to avoid the trouble of Chinese garbled characters after switching the system environment.

        Single-line comments can be placed at the end of the code line or at the top of the code line.

        Multi-line comments are used to annotate complex function descriptions. They can be placed in the program body or at the beginning of the code block. When the code is modified, the modified content

Use internal commands more often

        No matter what situation you encounter, please try to consider using internal commands instead of external commands

        Internal command execution is efficient and has good performance

There is no need to use the cat command

        This is one of the topics we often discuss in the forum. There is no need to use the cat command. This means that sometimes, we will find that there is no need to use the cat command at all. Using redundant cat commands will make your code look ugly and cause performance problems.

        For example---the following two commands have the same result

[root@localhost ~]# cat /etc/passwd | grep root

[root@localhost ~]# grep root /etc/passwd

Read the error message carefully

        One mistake that programmers often make is that when the command we type reports an error, most of us just glance at the error message without reading it carefully. Many times, in the error message It contains the solution

        Sometimes after we correct an error and run it again, the system will still report an error. Then we modified it again, but the system reported an error again. This can go on for a long time. But in fact, the old error may have been corrected, only to cause the system to report an error again because of some other new errors. And we are still wondering why the modified code still cannot run normally.

Filename ends with sh

        The shell script file name should be as the name implies, with the extension bit sh , such as: backup_mysql.sh

Code indentation:

        The shell has no mandatory requirements , but it is recommended to indent , which can improve readability and make the program more layered.

[root@localhost ~]# vim 99.sh
#!/bin/bash
for((i=1;i<10;i++))
do
        echo  -ne  "$i\t"
done

echo

for((i=1;i<70;i++))
do
        echo  -n  "="
done

echo

for((i=1;i<10;i++))
do
        for((j=1;j<=i;j++))
        do
                echo  -en  "$i*$j=$[i*j]\t"
        done
        echo
done
[root@localhost ~]# bash 99.sh 

shell script execution

method 1

        Use the sh or bash command to execute the script. Execution permission is not required (recommended). The interpreter does not need to be specified in the script.

[root@localhost ~]# vim test.sh 
#!/bin/bash
echo  "china"
[root@localhost ~]# bash test.sh 
[root@localhost ~]# sh test.sh 

        You can use bash -n script name to perform syntax detection without executing the script.

        You can use bash -x script name to track script execution and track execution statement by statement.

Method 2 

        Switch to the directory where the script is located and use ./ to execute the script , which requires execution permissions.

[root@localhost ~]# ./test.sh
-bash: ./test.sh: Permission denied #权限不够

[root@localhost ~]# chmod +x test.sh 
[root@localhost ~]# ./test.sh 

Method 3

        Executing the script with an absolute path requires execution permissions

[root@localhost ~]# vim /t1.sh
#!/bin/bash
echo  "china"

[root@localhost ~]# /t1.sh
-bash: /t1.sh: Permission denied    #权限不够

[root@localhost ~]# chmod +x /t1.sh
[root@localhost ~]# /t1.sh 

Method 4

         Use dot (.) or source to execute the script, no execution permission is required

[root@localhost ~]# source /t1.sh 

[root@localhost ~]# . test.sh 

Notice:

        Methods 1, 2, and 3 all start a subshell and execute the script in the subshell. The variables set in the script will not be saved after the script is executed.

        Method 4: Execute the script in the current shell process instead of restarting a shell. Execute the script in a sub-shell process, and the variables set in the script will be saved after the script is executed.

bash shell basic functions

echo print command

Format

        echo -parameter content

parameter

        

parameter effect
-n

Suppress the newline character at the end of the line after

-e Enable escape characters

Colored fonts can be output

        Format --- echo -e "\e[Font control; font color or background color string content\e[0m"

\e[ indicates the start of control , \e[0m indicates the end of control

Font control options--- 1 means highlight , 4 means underline , 5 color flashes

The colors are as follows: text color 30-37 , background color 40-47

[root@localhost ~]# vim color.sh
#!/bin/bash
echo -e "\e[30m 黑色字 \e[0m]"
echo -e "\e[1;31m 红色字 \e[0m"
echo -e "\e[32m 綠色字\e[0m"
echo -e "\e[33m 黃色字\e[0m"
echo -e "\e[34m 藍色字\e[0m"
echo -e "\e[35m 紫色字\e[0m"
echo -e "\e[36m 天藍字\e[0m"
echo -e "\e[37m 白色字\e[0m"
echo -e "\e[40;37m 黑底白字\e[0m"
echo -e "\e[41;37m 紅底白字\e[0m"
echo -e "\e[42;37m 綠底白字\e[0m"
echo -e "\e[43;37m 黃底白字\e[0m"
echo -e "\e[44;37m 藍底白字\e[0m"
echo -e "\e[45;37m 紫底白字\e[0m"
echo -e "\e[46;37m 天藍底白字\e[0m"
echo -e "\e[47;30m 白底黑字\e[0m"

[root@localhost ~]# bash color.sh 

printf command

        The printf command imitates the printf() program in the C library and is defined by the POSIX standard. Therefore, scripts using printf are more portable than using echo . printf uses quoted text or space-separated parameters , which can be used outside of printf. To format a string, you can also specify the width, left and right alignment, etc. of the string. By default, printf does not automatically add newlines like echo. We can add \n manually.

Format

        printf format control string parameter list

[root@localhost ~]# echo "Hello, shell"
[root@localhost ~]# printf "Hello, shell\n"

[root@localhost ~]# printf "Hello, shell"

[root@localhost ~]# printf "%d %s \n" 1 "abc"

[root@localhost ~]# printf '%d %s \n' 1 "abc" # 单引号双引号效果一样

[root@localhost ~]# printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg

%s %c %d %f are all format replacement characters . %s outputs a string, %d outputs an integer, %c outputs a character, and %f outputs a real number in decimal form.
 %-10s refers to a character with a width of 10 characters ( - means left-aligned , without means right-aligned). Any character will be displayed within the 10-character width. If it is insufficient, it will be automatically filled with spaces. If it is exceeded, the content will be show all

[root@localhost ~]# printf "%-10s %-8s %-4s\n" 姓名 性别 体重kg
[root@localhost ~]# printf "%-10s %-8s %-4.2f\n" 郭靖 男 66.1234

%-4.2f 指格式化为小数,其中 .2 指保留2位小数

Guess you like

Origin blog.csdn.net/qq_57289939/article/details/132752970