Three types of shell script execution

Difference between the three methods of executing shell scripts: (sh, exec, source)

When executing a script using $shscript.sh, the current shell is the parent process, which spawns a child shell process that executes the script in the child shell. When the script is executed, it exits the child shell and returns to the current shell.

. /script.sh is equivalent to shscript.sh.

Using the $sourcescript.sh method executes the script in the current context without spawning a new process. The script finishes executing and returns to the current shell.

The sources approach is also called point commands.

.script.sh is equivalent to sourcescript.sh.

Using the execcommand approach replaces the current shell process with the command process and leaves the PID unchanged. When execution is complete, it exits without returning to the previous shell environment.

Tests

viloop.sh

Display the current process

sh way: execute loop.sh to print the execution process

source way: execute loop.sh to print the execution process

exec way: execute loop.sh to print the execution process

Press ctrl+C

sh way: the parent process is 6770, and the child process when executing loop.sh is 13736. after execution, it will go back to the parent shell.

source way: the parent process and the child process are both 6770 (there is no new process when execution is done), and it will go back to the parent shell when execution is done.

exec way: process PID did not change are 6770, the execution is completed (ctrl + C force close) directly quit the shell. script execution replaced the parent process shell, the execution is completed directly quit, did not go back to the previous shell.

Which is the incorrect way to execute a shell file in the current directory

file.

The correct way to execute a shell file in the current directory is: shfile, /file, sourcefile.

A shell file is a file obtained by putting a number of commands together in a certain way, often called a Shell scripts. There are various ways to execute a shell script: dot + slash + filename, which requires the file to have executable permissions.

shell script writing method

In the shell learning process, feel quite useful a comparison, slightly organized to share with you.

First of all, the role of the shell

The user’s login shell login default shell program is: /bin/bash

Different shell internal commands, the runtime environment will be different

Common shells are

< p>

/bin/sh

/bin/bash

/sbin/nologin

1. Write script code:

Using the vi editor, one Linux command per line, executed in order <

2. Then give the script file executable attributes

3. Three ways to execute the script file

. /chao.sh

sh/chao.sh

source/chao.sh

Better Script Composition

Commentary Information

Executable Statements

Redirection Operations

Type Operator Usage

Redirect Input <Read from a specified file instead of typing from the keyboard

Redirect Output >Save the output file to a specified file (overwriting the original content)

>>Save the output file to the specified file (overwriting the original contents)

Standard Error Output 2> saves the error message to the specified file (overwriting the original contents)

2>> saves the error message to the specified file

Mixed Output &> saves the standard output, and standard error content into the same file

Pipeline operation symbol “|”

The output of the command on the left side is used as the processing object of the command on the right side

The role of shell variables

< p>The specific parameters provided for flexible management of the Linux system have two meanings

Variable name: a fixed name, preset by the system or defined by the user

Variable value: the ability to change according to the user’s settings, the system environment

Types of variables

Custom variables: the output of the left command as the processing object of the right command

Shell variables are used for the following purposes

Shell variables are used for the purpose of managing specific parameters provided for flexible management of the Linux system.

Custom variables: defined, modified and used by the user

Environmental variables: maintained by the system, used to set up the working environment

Positional variables: parameters passed to the script program through the command line

Predefined variables: a category of variables built into BASH that cannot be modified directly

Define a new variable

Format: variable name = variable value

Variable name starts with a letter or an underscore, case-sensitive, all capitals recommended

To see the value of a variable: echo$variable name

When assigning a value, use Quotation marks:

Double quotation marks: the $ character is allowed to be used to quote the value of other variables

Single quotation marks: quoting the value of other variables is prohibited, and $ is considered an ordinary character

The back-apostrophe: a command substitution that extracts the output of a command after it has been executed

The assignment of a variable value from the keyboard.

Format: read[-p “prompt message”] variable name

Set the scope of the variable

export variable name

export variable name = variable value

The two formats can be mixed! Use

Operations on integer variables:

expr variable 1 operator variable 2 [operator variable 3]

Common operators

Addition operator +

Subtraction operator:-

Multiplication operator: \*

Division operator: //

Redundancy operation: %

Operations on integer variables (2)

((variable=variable operator variable));

Variable following the equal sign can be the specific values

Example:

((a=a+3));

Environmental variables:

Created by the system ahead of time to set the user’s working environment

Configuration files:

For example, built-in usernames or passwords for some services

E.g., the startup user for apache is a predefined variable

Conditional test operations

Tests whether a particular expression holds, and when the condition is true, the return value of the test statement is 0, otherwise it is some other value

Format: test conditional expression

Application example:

test-z to test the string length of 0 when the result returns 1

If the value of the variable is 0 returns 0

Conditional test operation

Conditional test operation

[Conditional test operation

[Conditional test operation

[Conditional test operation

[Operator file or directory]

The [5] part is a judgment expression, and the -d indicates whether it is a directory or not

&& is a “logical and” operator, so that only if the judgment in front of && is valid, the statements that follow it are executed

& is a “logical and” operator. execute

-b means to determine if the path that follows is a directory

echo$? means to judge the expression just executed

0 means true, 1 means not true

-efilename is true if filename exists [-e/var/log/syslog]

-dfilename is true if filename is a directory, then true [-d/tmp/mydir]

-ffilenameIf filename is a regular file, then true [-f/usr/bin/grep]

-LfilenameIf filename is a symbolic link, then is true [-L/usr/bin/grep]

-rfilename is true if filename is readable [-r/var/log/syslog]

-wfilename is true if filename is writable [-w/var/ mytmp.txt]

-xfilename True if filename is executable [-L/usr/bin/grep]

filename1-ntfilename2 True if filename1 is newer than filename2 [ /tmp/install/etc/services-nt/etc/services]

filename1-otfilename2 true if filename1 is older than filename2 [/boot/bzImage-otarch/i386/ boot/bzImage]

String Comparison Operators (note the use of quotes, it’s a good way to prevent spaces from cluttering up the code)

-zstring true if string length is zero [-z”$myvar”]

– nstringTrue if string has non-zero length [-n”$myvar”]

string1=string2True if string1 is the same as string2 [“$myvar”=”onetwothree”]

string1!=string2 is true if string1 is different from string2 [“$myvar”! = “onetwothree”]

Arithmetic Comparison Operator

num1-eqnum2 is equal to [3-eq$mynum]

num1-ltnum2 is not equal to [3-ne$mynum]

num1-ltnum2 is less than [3-lt$mynum]

num1-lenum2 is less than or equal to [3-le$mynum]

num1-gtnum2 is greater than [3-gt$mynum]

num1- genum2 is greater than or equal to [3-ge$mynum]

Integer value comparisons

Format: [integer1 operator integer2]

String comparisons

Format 1: [string1=string2]

[string1 = string 2]

Format 2: [-z string]

Logic test:

Format 1: [expression 1] operator [expression 2]

Format 2: command 1 operator command 2

The structure of the if statement

Syntactic structure of while statement

Shell scripts of several ways to execute the difference

1, bash script file or sh script file, through this way you can run the script does not have execution privileges or in the script does not specify the interpreter;

2, . / script file or the full path to the script file, through this way to execute the script file requires the script file has execution privileges, and in the script file to specify the correct interpreter;

3, source script file or . script file (with a space between the . and script file with a space between), this way of running in the current shell to run, run the script defined in the variables will not be released, which is this way of running scripts and the first three of the biggest difference. For example, if you define name=GG in a script, echo$name will still display GG after the script finishes running.