Shell scripting steps

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 shell have

/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 contents)

>>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: a variable that can be set by the user, and can be used to change the system environment.

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

How to write shell scripts from scratch

1, first use cat> or vim to create a new file tss.

2, and then in the new file to write the appropriate script, as follows:

[plain]viewplaincopy

<span style=” font-size:18px;”>#! /bin/sh

echo hello</span></span>

Note: To write shell scripts, #! /bin/sh must be present, this is to tell the Linux kernel which shell to use to execute the specified shell script.

3. chmod+xtss gives the file tss execute permission

4. . /tss to execute the corresponding script program

How to write a simple shell script

1, first open the programming interface and check the permission information of the files in the dog directory now.

2. Now b.txt has user permissions RW, group permissions RW, and other permissions R. Now I add X run permissions to the user permissions.

3, the same way to the group, other people add run permissions use chmodu/g/o+xb.txt.

4, if you want to cancel the corresponding permissions, use the “-” sign can be.

5, in addition to the use of RWX characters can also be represented by a number after the R = 4 W = 2 X = 1, chmod777b.txt for all permissions are added to the 3 permissions.

6, R=4 W=2 X=1, 7=R+W+X,6=R+W, etc., if you use the data to set up the permissions need to be calculated, I personally feel that it is still better to use the characters to understand a little more familiar with that with the numbers will be faster. And 3 numbers must be written in full to be able to.

How to write a shell script

Create a new file shell script generally use x.sh as a suffix of course courage his can also. Open a terminal and type touchfirst.sh to create a new shell script named first.

Writing a simple linux shell script

Using vim to edit first.sh can also be used with other text editors, the recommended use of vim

Using the command vimfirst.sh to open, enter i into edit mode.

Writing a simple linux shell script

We write a simple shell script, pay attention to the first line of the code to specify the interpreter, the use of /bin/bash/interpreter can also be used according to the personal circumstances of their choice.

Explanation of the script:

echo//display a string of characters with automatic line breaks

readNAME//get a string of characters from the screen and assign it to NAME

$NAME//take the value of the NAME variable

#//use only a # to indicate the text of the comment

Write a simple linuxshell script

When the file is written press esc to exit insert mode, then type: wq to save the text and exit the text editor.

Writing a simple linuxshell script

Type sh+script name to run the script or chmod+x to give the file runnable permissions and type . /first.sh to run the script.

How to write a shell script

First open the programming interface and look at the permissions information for the files now in the dog directory. Now b.txt has user permissions RW, group permissions RW, and other permissions R. Now I add X run permissions to the user permissions.

Create a new file shell script generally use x.sh as a suffix of course courage his can also. Open a terminal and type touchfirst.sh to create a new shell script named first.

There are three types of commands that can be used in shell scripts: 1) Unix commands: Although you can use any unix command in a shell script, there are some relatively more commonly used commands. These commands are usually used for file and text operations.

Writing a SHELL script program under UNIX/Linux is not difficult, as long as the full-screen editing program vi is used proficiently, i.e. you can write a SHELL script program.