A minimalist tutorial on shell scripting

1. Shell inscription

Programmers who do not understand shell are not good programmers. Learning shell is for automation. Using automation can effectively improve work efficiency. There is no large company that does not require basic Linux skills, but different positions require different degrees of mastery.

2. Introduction to shell

Shell itself is a program written in C language, it is a bridge for users to use Unix/Linux, and most of the work of users is done through Shell. Shell is both a command language and a programming language. As a command language, it interactively interprets and executes commands entered by the user; as a programming language, it defines various variables and parameters, and provides many control structures that are only available in high-level languages, including loops and branches.

Although it is not part of the Unix/Linux system kernel, it invokes most of the functions of the system kernel to execute programs, create files and coordinate the execution of various programs in a parallel manner. Therefore, for users, the shell is the most important utility program, and it is the key to make good use of the Unix/Linux system to deeply understand and master the characteristics and usage of the shell.

It can be said that the proficiency of shell usage reflects the user's proficiency in using Unix/Linux.

Shell has two ways to execute commands:

Interactive (Interactive): Interpret and execute the user's command, the user enters a command, and the Shell interprets and executes one.

Batch (Batch): The user writes a Shell script (Script) in advance, which has many commands, so that the Shell executes these commands at a time without having to type commands one by one.

Shell scripts are very similar to programming languages. They also have variables and flow control statements, but shell scripts are interpreted and executed without compilation. The shell program reads and executes these commands line by line from the script, which is equivalent to a user putting the commands in the script. Type line by line to the Shell prompt to execute.

Shell beginners, please note that in ordinary applications, it is recommended not to run Shell with the root account. As a normal user, you can't break the system, whether you intentionally or not; but if you're root, it's different, and just typing a few letters can lead to catastrophic consequences.

Three, several common shells

As mentioned above, Shell is a scripting language, so there must be an interpreter to execute these scripts.

Common shell script interpreters on Unix/Linux include bash, sh, csh, ksh, etc., which are customarily called a kind of shell. We often talk about how many kinds of shells there are, but we are actually talking about shell script interpreters.

bash: bash is the standard default shell of Linux, and this tutorial is also based on bash. Bash is jointly completed by Brian Fox and Chet Ramey. It is the abbreviation of BourneAgain Shell, and there are a total of 40 internal commands.

Linux uses it as the default shell because it has features such as:

You can use the functions similar to doskey under DOS, and use the arrow keys to view and quickly enter and modify commands.

Automatically give commands starting with a string by looking for a match.

Contains its own help function, you only need to type help below the prompt to get related help.

sh: Developed by Steve Bourne, sh is the abbreviation of Bourne Shell, sh is the default shell of the Unix standard.

ash: ash shell is written by Kenneth Almquist, a small shell that occupies the least system resources in Linux, it only contains 24 internal commands, so it is very inconvenient to use.

csh: csh is a relatively large kernel of Linux. It is compiled by a total of 47 authors represented by William Joy, and has a total of 52 internal commands. The shell actually points to a shell such as /bin/tcsh, that is to say, csh is actually tcsh.

ksh: ksh is an acronym for Korn shell, written by Eric Gisin, and has a total of 42 internal commands. The biggest advantage of this shell is that it is almost completely compatible with the commercial distribution of ksh, so that you can try the performance of the commercial version without paying for the commercial version.

Note: bash is the abbreviation of Bourne Again Shell, which is the default shell of linux standard. It is based on Bourne shell and absorbs some features of C shell and Korn shell. Bash is fully compatible with sh, that is, scripts written in sh can be executed in bash without modification.

Fourth, the difference between programming and interpreted languages

In general, programming languages ​​can be divided into two categories: compiled languages ​​and interpreted languages.

compiled language

Many traditional programming languages, such as Fortran, Ada, Pascal, C, C++, and Java, are compiled languages. Such languages ​​require pre-conversion of our written source code (source code) into object code (object code), a process called "compilation".

When running the program, the object code is read directly. Since the compiled object code (object code) is very close to the bottom layer of the computer, the execution efficiency is very high, which is the advantage of compiled languages.

However, since compiled languages ​​mostly operate at the bottom layer and deal with bytes, integers, floating-point numbers or other machine-level objects, it often requires a lot of complex code to implement a simple function. For example, in C++, it is difficult to do something as simple as "copy all the files in one directory to another".

interpreted language

Interpreted languages ​​are also known as "scripting languages". When executing such programs, the interpreter needs to read the source code we wrote and convert it into object code, which is then run by the computer. Because each time the program is executed, there are more compilation processes, so the efficiency is reduced.

The advantage of using scripting languages ​​is that they mostly run at a higher level than compiled languages ​​and can easily handle objects like files and directories; the disadvantage is that they are generally not as efficient as compiled languages. However, the trade-off is that scripting is usually worth it: a simple script that takes an hour to write, the same functionality written in C or C++, can take two days, and generally the script executes fast enough , fast enough to ignore its performance issues. Examples of scripting languages ​​are awk, Perl, Python, Ruby, and Shell.

Fifth, when to use the shell?

Because Shell seems to be a common function between UNIX systems and has been standardized by POSIX. Therefore, shell scripts can be applied to many systems as long as they are "written with heart" once. So the reason to use shell scripts is based on:

Simplicity: Shell is a high-level language; with it, you can express complex operations concisely.

Portability: Using the functions defined by POSIX, scripts can be executed on different systems without modification.

Easy to develop: a powerful and useful script can be completed in a short time.

However, considering the command limitation and efficiency of Shell scripts, Shell is generally not used in the following situations:

Resource-intensive tasks, especially when efficiency is a concern (eg, sorting, hashing, etc.).

Mathematical operations that need to handle large tasks, especially floating-point operations, exact operations, or complex arithmetic operations (this is usually handled in C++ or FORTRAN).

There is a need for cross-platform (operating system) portability (usually using C or Java).

Complex applications, when structured programming must be used (requires type checking of variables, function prototypes, etc.).

For mission-critical applications that affect the globality of the system.

For tasks with high security requirements, such as you need a robust system to prevent intrusion, cracking, malicious damage, etc.

A project consists of a series of dependent parts.

Requires large-scale file operations.

Multidimensional array support is required.

Requires the support of data structures, such as linked lists or data structures such as numbers.

Need to generate or operate a graphical interface GUI.

Direct OS hardware is required.

An I/O or socket interface is required.

Interfaces that require the use of libraries or legacy legacy code.

Private, closed-source applications (shell scripts put code in text files that the world can see).

If your application fits any of the above, then consider a more powerful language -- perhaps Perl, Tcl, Python, Ruby -- or a higher-level compiled language like C/C++, or Java. Even so, you will find that using the shell to prototype your application can be very useful during the development step.

Six, the first shell script

Open a 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. It is good to know the name. If you use php to write shell scripts, the extension should be php.

Enter some code:

#!/bin/bashecho "Hello World !"

"#!" is a convention mark that tells the system what interpreter this script needs to execute, i.e. what kind of shell to use. The echo command is used to output text to the window.

There are two ways to run a shell script.

as an executable program

Save the above code as test.sh and cd to the corresponding directory:

chmod +x ./test.sh #Make the script have execute permission./test.sh #Execute the script

Note that it must be written as ./test.sh, not test.sh. The same is true for running other binary programs, write test.sh directly, the Linux system will go to the PATH to find out if there is a 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 if you write test.sh, the command will not be found. Use ./test.sh to tell the system to look for it in the current directory.

To run a 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 application of the shell (imagine Windows Explorer), but I deliberately write it as the system to facilitate understanding. Since the system refers to the shell, is a script that uses /bin/sh as the interpreter? Can the first line be omitted? Yes.

as interpreter argument

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

/bin/sh test.sh/bin/php test.php

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

Let's look at another example. The following script uses the read command to take input from stdin, assign it to the PERSON variable, and finally output on stdout:

#!/bin/bash# Author : mozhiyan# Copyright (c) http://see.xidian.edu.cn/cpp/linux/# Script follows here:echo "What is your name?"read PERSON

echo "Hello, $PERSON"

Run the script:

chmod +x ./test.sh

$./test.sh

What is your name?

mozhiyan

Hello, mozhiyan

Seven, shell variables

Shell supports custom variables.

define variable

When defining a variable, do not add a dollar sign ($) to the variable name, such as:

variableName="value"

Note that there can be no spaces between the variable name and the equals sign, which may be different from all programming languages ​​you are familiar with. At the same time, the naming of variable names must follow the following rules:

The first character must be a letter (az, AZ).

There can be no spaces in between, you can use an underscore (_).

Punctuation marks cannot be used.

You cannot use keywords in bash (reserved keywords can be viewed with the help command).

Example of variable definition:

myUrl="http://see.xidian.edu.cn/cpp/linux/"myNum=100

use variables

To use a defined variable, just add a dollar sign ($) in front of the variable name, for example:

your_name="mozhiyan"echo $your_name

echo ${your_name}

The curly braces around the variable name are optional and can be added or not. The curly braces are added to help the interpreter identify the boundaries of the variable, such as the following:

for skill in Ada Coffe Action Java

do

echo "I am good at ${skill}Script"done

If you don't add curly braces to the skill variable and write it as echo "I am good atskillScript", the interpreter will treat skillScript", the interpreter will treat skillScript as a variable (its value is empty), and the code execution result is not what we expect looks like it.

Curly braces are recommended for all variables, which is good programming practice.

redefine variable

Defined variables can be redefined, such as:

myUrl="http://see.xidian.edu.cn/cpp/linux/"echo ${myUrl}

myUrl="http://see.xidian.edu.cn/cpp/shell/"echo ${myUrl}

It is legal to write this way, but note that you cannot write myUrl="http://see.xidian.edu.cn/cpp/shell/" in the second assignment, and only add the dollar sign (myUrl=" http://see.xidian.edu.cn/cpp/shell/", only add the dollar sign () when using variables.

read-only variable

Use the readonly command to define a variable as a read-only variable, and the value of a read-only variable cannot be changed.

The following example attempts to change a read-only variable, and results in an error:

#!/bin/bashmyUrl="http://see.xidian.edu.cn/cpp/shell/"readonly myUrl

myUrl="http://see.xidian.edu.cn/cpp/danpianji/"

Running the script yields the following results:

/bin/sh: NAME: This variable is read only.

delete variable

Variables can be deleted using the unset command. grammar:

unset variable_name

After a variable is deleted, it cannot be used again; the unset command cannot delete a read-only variable.

for example:

#!/bin/shmyUrl="http://see.xidian.edu.cn/cpp/u/xitong/"unset myUrl

echo $myUrl

The above script does not have any output.

variable type

When running the shell, three variables exist at the same time:

1) Local variables

Local variables are defined in scripts or commands and are only valid in the current shell instance. Programs started by other shells cannot access local variables.

2) Environment variables

All programs, including those started by the shell, have access to environment variables, and some programs require environment variables to function properly. Shell scripts can also define environment variables when necessary.

3) shell variables

Shell variables are special variables set by the shell program. Some of the shell variables are environment variables, and some are local variables. These variables ensure the normal operation of the shell.

Eight, shell special variables

As mentioned earlier, variable names can only contain numbers, letters and underscores, because some variables containing other characters have special meanings, and such variables are called special variables.

For example, $ represents the ID of the current Shell process, that is, pid, see the following code:

$echo $$

Running result: 29949

 

list of special variables

Variable meaning

$0 The filename of the current script

$n Arguments passed to scripts or functions. n is a number indicating the number of parameters. For example, the first parameter is 1, the second parameter is 1, and the second parameter is 2.

$#Number of arguments passed to the script or function.

$* All arguments passed to the script or function.

$@All parameters passed to the script or function. When enclosed in double quotes (" "), it is slightly different from $*, which will be described below.

$? The exit status of the last command, or the return value of a function.

$$ Current shell process ID. For shell scripts, this is the process ID in which the scripts reside.

Nine, shell replacement

If the expression contains special characters, the shell will replace it. For example, using a variable in double quotes is a substitution, as is an escape character.

for example:

#!/bin/basha=10echo -e "Value of a is $a \n"

operation result:

Value of a is 10

Here -e means to replace the escape character. If the -e option is not used, it will output as-is:

Value of a is 10\n

The following escape characters can be used in echo:

escape character meaning

\\ backslash

\aAlarm, bell

\bbackspace (delete key)

\f form feed (FF), move the current position to the beginning of the next page

\nNewline

\rEnter

\tHorizontal tab (tab key)

\vvertical tab

You can use the -E option of the echo command to suppress escaping, which is also unescaped by default; use the -n option to suppress the insertion of newlines.

command substitution

Command substitution means that the shell can execute the command first, save the output temporarily, and output it in an appropriate place.

Syntax for command substitution:

`command`

Note the backticks, not the single quotes, this key is below the Esc key.

In the following example, the result of the command execution is stored in a variable: #!/bin/bashDATE=`date`

echo "Date is $DATE"USERS=`who | wc -l`

echo "Logged in user are $USERS"UP=`date ; uptime`

echo "Uptime is $UP"

operation result:

Date is Thu Jul  2 03:59:57 MST 2009Logged in user are 1Uptime is Thu Jul  2 03:59:57 MST 2009

03:59:57 up 20 days, 14:03,  1 user,  load avg: 0.13, 0.07, 0.15

variable substitution

Variable substitution can change the value of a variable based on its state (empty, defined, etc.).

Variable substitution forms that can be used:

Form description

The original value of the ${var} variable

${var:word} If variable var is empty or has been deleted (unset), returns word without changing the value of var.

${var:=word} If the variable var is empty or has been deleted (unset), returns word and sets the value of var to word.

${var:?message} If the variable var is empty or has been deleted (unset), then the message message is sent to the standard error output, which can be used to check whether the variable var can be assigned normally.

If this substitution occurs in a shell script, the script will stop running.

${var:+word} Returns word if variable var is defined, but does not change the value of var.

See the example below:

#!/bin/bash echo ${var:-"Variable is not set"}

echo"1 - Value of var is ${var}"echo ${var:="Variable is not set"}

echo"2 - Value of var is ${var}"unset var

echo ${var:+"This is default value"}

echo"3 - Value of var is $var"var="Prefix"echo ${var:+"This is default value"}

echo"4 - Value of var is $var"echo ${var:?"Print this message"}

echo"5 - Value of var is ${var}"

operation result:

Variable is not set1 - Value of var isVariable is not set2 - Value of var is Variable is not set3 - Value of var isThis is default value4 - Value of var is Prefix

Prefix5 - Value of var is Prefix

Ten, shell operators

Bash supports many operators, including arithmetic operators, relational operators, Boolean operators, string operators, and file test operators.

Native bash does not support simple mathematical operations, but can be achieved through other commands, such as awk and expr, expr is the most commonly used.

expr is an expression evaluation tool that can be used to evaluate expressions.

For example, adding two numbers:

#!/bin/bashval=`expr 2 + 2`

echo "Total value : $val"

Running the script outputs:

Total value : 4

Two points of note:

There should be spaces between expressions and operators, for example 2+2 is wrong, it must be written as 2 + 2, which is different from most programming languages ​​we are familiar with.

The complete expression should be enclosed by ` `, note that this character is not the usual single quote, below the Esc key.

arithmetic operators

Let's first look at an example using arithmetic operators:

#!/bin/sh a=10 b=20

val=`expr $a + $b`

echo "a + b : $val"

operation result:

a + b : 30

Notice:

The multiplication sign (*) must be preceded by a backslash (\) to achieve multiplication;

if...then...fi is a conditional statement, which will be explained later.

List of arithmetic operators

operator description example

+Addition `expra+b` results in 30.

- Subtraction `expra−b` results in 10.

*Multiplication `expra\**b` results in 200.

/The result of division `exprb/a` is 2.

% The remainder of `exprba` results in 0.

= assignment a=$b will assign the value of variable b to a.

== is equal. Used to compare two numbers, returns true if they are the same. [a==b] returns false.

!= not equal. Used to compare two numbers, returns true if they are not the same. [a!=b] returns true.

Note: Conditional expressions should be placed between square brackets and spaces. For example, [a ==b] is wrong and must be written as [ a ==b].

relational operator

Relational operators only support numbers, not strings, unless the value of the string is a number.

Let's first look at an example of a relational operator:

#!/bin/sh a=10 b=20

if [ $a -eq $b ]

then

echo "$a -eq $b : a is equal to b"

else echo "$a -eq $b: a is not equal to b"

operation result:

10 -eq 20: a is not equal to b

List of relational operators

operator description example

-eq checks whether two numbers are equal, and returns true if they are equal. [a−eqb ] returns true.

-ne checks whether two numbers are equal, and returns true if they are not equal. [a−neb ] returns true.

-gt checks if the number on the left is greater than the number on the right, and if so, returns true. [a−gtb ] returns false.

-lt checks if the number on the left is less than the number on the right, and if so, returns true. [a−ltb ] returns true.

-ge checks whether the number on the left is greater than the number on the right, and if so, returns true. [a−gea−geb ] returns false.

-le checks if the number on the left is less than or equal to the number on the right, and if so, returns true. [a−leb ] returns true.

boolean operator

Let's look at an example of a boolean operator:

#!/bin/sh a=10 b=20 if [ $a != $b ]

then

echo "$a != $b : a is not equal to b"

else echo "$a != $b: a is equal to b"

operation result:

10 != 20 : a is not equal to b

list of boolean operators

operator description example

! Not operation, returns false if the expression is true, otherwise returns true. [ ! false ] returns true.

-o OR operation, returns true if one of the expressions is true. [a−lt20-ob -gt 100 ] returns true.

-a AND operation, returns true only if both expressions are true. [a−lt20−ab -gt 100 ] returns false.

string operator

Let's look at an example first:

#!/bin/sh a="abc" b="efg" if [ $a = $b ]

then

echo "$a = $b : a is equal to b" else

echo "$a = $b: a is not equal to b"

operation result:

abc = efg: a is not equal to b

list of string operators

operator description example

= Checks if two strings are equal, returns true for equality. [a=b] returns false.

!= Checks whether two strings are equal, and returns true if they are not equal. [a!=b] returns true.

-z checks if the string length is 0, and returns true if it is 0. [ -z $a ] returns false.

-n checks whether the length of the string is 0, and returns true if it is not 0. [ -z $a ] returns true.

str checks whether the string is empty, and returns true if it is not empty. [ $a ] returns true.

file test operator

File test operators are used to test various attributes of Unix files.

For example, the variable file represents the file "/var/www/tutorialspoint/unix/test.sh", which is 100 bytes in size and has rwx permissions. The following code will detect various properties of the file:

#!/bin/sh file="/var/www/tutorialspoint/unix/test.sh"

if [ -r $file ]

then

echo "File has read access"

else echo "File does not have read access"

operation result:

File has read access

List of file test operators

operator description example

-b file detects if the file is a block device file, and returns true if it is. [ -b $file ] returns false.

-c file detects if the file is a character device file, and returns true if it is. [ -b $file ] returns false.

-d file detects if the file is a directory, and returns true if it is. [ -d $file ] returns false.

-f file detects if the file is a normal file (neither a directory nor a device file), and returns true if it is. [ -f $file ] returns true.

-g file detects if the file has the SGID bit set, and returns true if it is. [ -g $file ] returns false.

-k file Checks whether the file has the Sticky Bit set, and returns true if it is. [ -k $file ] returns false.

-p file detects if the file is a named pipe, and returns true if it is. [ -p $file ] returns false.

-u file detects if the file has the SUID bit set, and returns true if it is. [ -u $file ] returns false.

-r file Checks if the file is readable, and returns true if it is. [ -r $file ] returns true.

-w file checks if the file is writable, and returns true if it is. [ -w $file ] returns true.

-x file checks if the file is executable, and returns true if it is. [ -x $file ] returns true.

-s file detects whether the file is empty (whether the file size is greater than 0), and returns true if it is not empty. [ -s $file ] returns true.

-e file detects if the file (including the directory) exists, and returns true if it does. [ -e $file ] returns true.

 


 

 

Architect's small secret circle, a small circle of 100,000 architects! Share technical dry goods and industry secrets from time to time! Bringing together all kinds of wonderful and fun topics and popular trends! Scan the code to join the WeChat group!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326360549&siteId=291194637