Fortune Telling Collection - Comprehensive fortune-telling - Linux uses external command expr to realize calculator function, and uses pipes and processes.

Linux uses external command expr to realize calculator function, and uses pipes and processes.

Test Instructions (Ask the Explanator)

Function: Check file types and compare values.

Various parameters and usage of the test.

Test expression1–a expression 2

When the expressions 1 and 2 are both true, the value is true.

Test expression1–o expression 2

When the expression 1 or expression 2 is true, the value is true.

Test–n string

or

Test string

True when the length of the STRING is not zero.

Test–z string

This value is true when the string length is zero.

Test string 1 = string 2

True when the time values of STRING 1 and STRING2 are the same.

Test string 1! = STRING2

When the different values of STRING 1 and STRING2 are true.

Test integer1–eq integer 2

When the integer 1 equals the integer 2, the value is true.

Test integer1–GE integer 2

True when INTEGER 1 is greater than or equal to INTEGER2.

Test integer1–gt integer 2

True when the integer 1 is greater than the integer 2.

Test integer 1- integer 2

True when INTEGER 1 is less than or equal to INTEGER2.

Test integer1–lt integer 2

True when the integer 1 is less than the integer 2.

Test integer1–ne integer 2

True when INTEGER 1 is not equal to INTEGER2.

Test file1–ef file 2

True when file 1 and file 2 have the same device number and inode number (please refer to the linux file study notes for details).

Test file1–nt file 2

When the modification time of file 1 is true than the new value of file 2.

Test file1–ot file 2

When the modification time of file 1 is true than the old value of file 2.

Test -b file

The file exists, and the content belongs to block type.

Test–c file

The file exists and is of character type.

Test -d file

The file exists and is a directory.

Test–e file

Does the file exist?

Test–f file

The file exists and is a document of regular expression type.

Test–g file

The file exists, and it is set-group-ID, that is, SGID (please refer to the later study notes for details).

Test–g file

The file exists and is owned by a valid group id (please refer to the later study notes for details).

Test–h file

The file exists and is a symbolic link (see the study notes later).

Test -k file

The file exists and the sticky bit is set (please refer to the later study notes for details).

Test–l file

The file exists and is a symbolic link.

Test -O file

The file exists and is owned by a valid user id.

Test–p file

The file exists and is a named pipe (see the study notes later).

Test -r file

The file exists and has been granted read permission.

Test file

The file exists and the size is greater than 0.

Test file

The file exists and is a socket.

Test–Test the fault diagnosis.

Open the descriptor FD of the file in the terminal.

Test -u file

File exists, SUID has been set.

Test–w file

The file exists and has been granted write permission.

Test–x file

The file exists and has been granted executable permission.

-

expr

Expr is a manual command line counter of linux, which can help us to complete some basic expression value operations. It is also a string processing tool.

(1) integer operation

$expr ARG 1 | ARG2

$ expr arg 1 & amp; ARG2

$ expr arg 1 & lt; ARG2

$ expr arg 1 & lt; = ARG2

$expr ARG 1 = ARG2

$expr ARG 1! = ARG2

$ expr arg 1 & gt; = ARG2

$ expr arg 1 & gt; ARG2

$expr ARG 1 + ARG2

$ expr arg 1–arg 2

$expr ARG 1 * ARG2

When using multiplication, you need to use backslashes to escape.

$expr ARG 1 % ARG2

(2) String operation

$ expr length "XXX "// Calculate the string length.

$ expr substr "This is a substring of" pos length// truncated from the pos bit.

$ expr index "tesr" e// Gets the position where e first appears in the main string.

(3) Incremental counting

example

Cycle =3

loop = ' expr $ loop+ 1 '

echo $loop

The result is 4. In the second line of code, the back quotation marks are used, and the shell will take the contents in the back quotation marks as the system command. In this way, it is as if we enter expr $loop+ 1 at the command line, and the return result of this command is assigned to this loop.

(4) Pattern matching (matching strings according to regular expression patterns)

Count the number of characters in a string by specifying the colon option. . * means that any character is repeated 0 times or more.

& gtVALUE=account.doc

& gt expression $ value:'. *'

eight

You can use string matching operations in expr, where you use patterns to extract. Doc file.

$ expr $ VALUE:“\(。 * \). Doctor

account

(5) Others

+Token

Interpret TOKEN as a string, whether it is a keyword or an operator.

Expand knowledge:

The function of quotation marks

1 double quotation marks ("")

1) Use ""to refer to any character or string except $ (dollar sign), ` (back quote) and \ (backslash). Double quotation marks do not prevent the shell from doing special treatment for these three characters (indicating variable name, command replacement, backslash escape).

eg:name = gezn; Echo "User name: $ name "// User name: gezn will be printed.

Echo "Date is: `date+date-%d-%m-%y`"//Date is: 03-05-2009 and will be printed.

Echo–e "$ user \ t $ uid"//gezn500 will be printed.

2) If you want to search for a new string containing spaces, double quotation marks are often used.

2 single quotation marks ("")

1) If the string is enclosed in single quotation marks, the special meaning of any special characters in the string in dayi9nhao will be shielded.

2) For example: echo–e' $ user \ t $ uid'/$ user $ uid will be printed (it will not be blocked due to the option "-e").

Echo' USER \ t $ UID '// will print $USER\t$UID.

3 back quotes (``)

1) shell shell takes the content in the back quotation marks as a system command and executes its content. Use this method to replace the output with a variable.

2) For example: a=`date+date-%d-%m-%Y` // The printing date will be 03-05-2009.

4. Backslash (\)

1) If the next character has a special meaning, the backslash prevents the shell from misunderstanding its meaning, that is, it shields its special meaning.

2) Dependent characters have special meanings: *+$` "|?

3) When you want to add octal characters (ASCII characters) when printing a string, you must add a backslash in front of it, otherwise the shell will be used as an ordinary number.

Example: bj = Beijing; Echo "variable \ $ bj = $ bj "// The variable $bj = beijing will be printed.

-

Shell special variable

In the Shell, several Shell variables with special meanings are predefined, and their values can only be assigned by the Shell according to the actual situation, and cannot be reset by the user. Special variables of the shell include its position and some system variables.

(1) Common positional variables:

The number of actual parameters on the command line, excluding the Shell script name.

$? The return value after the last command is executed (also called "exit code"). This is a decimal number. When most Shell commands are executed successfully, the return value is 0; If the execution fails, a non-zero value will be returned.

The process number of the current process.

$! The process number corresponding to the last background command is a number string consisting of 1~5 digits.

$-A string consisting of the execution flag name set by the current Shell. For example:

Command line set -xv sets flags -x and -v (for tracing output) for the Shell.

$ * indicates all the parameter strings actually given on the command line, not limited to 9 parameters.

The basic function of $ @ is the same as that of $ *, except that it is used in quotation marks, and each parameter is returned in quotation marks.

$0 script name

$ 1 ...$9 The nth parameter

The following aabbb is the variable name.

${aaa:-bbb} If $aaa is empty or undefined, the value is $bbb. Otherwise, the value is $aaa.

${aaa:+bbb} If $aaa is not empty, the value is $bbb, otherwise it is empty.

${aaa:=bbb} If $aaa is not empty, take the value of $aaa, otherwise take the value of $bbb and assign it (aaa=bbb).

${aaa:3} If aaa=abcdefg, the value of ${aaa:3} is: defg, which is equivalent to substr, and the counting starts from 0.

${aaa:3:2} same as above; The value of $ {AAA: 3: 2} is: de, which is equivalent to substr.

The length of ${#aaa} string $aaa.

(2) Common system variables:

Home directory of $HOME user

$USER user name

The name of the $ TERM group to which the $ TERM user belongs

$PATH default search path

Host name of $HOSTNAME

$ TERM tz time zone

The path name of the $ TERM where the $ TERM is stored

Practitioner:

#! /bin/bash

Echo $0

Echo $ *

echo $@

echo $#

echo $$

Echo $ _

Execute in a terminal window:

. /test . sh-a-b–c/home

. /test.sh

-a -b -c /home

-a -b -c /home

four

3250

/home page

Distinguish between $ * and $ @ Write the following test.sh script:

#! /bin/bash

Functional test mark

{

Echo "$# args"

}

testargs "$* "

Test token "$ @"

The test flag is not set.

Execute in a terminal window:

. /test.sh -a -b /home

There is obviously only one 1 args //, and the string $ * is passed in instead of the interpreted parameter.

3 args //$@ must match the quotation marks, so that the result is correct.

#! /bin/bash

Functional test mark

{

Echo "$# args"

}

testargs $*

testargs $@

The test flag is not set.

Similarly, there are:

. /test.sh -a -b /home

Three parameters

Three parameters

Author: Aga. J

Source: blogs.com/aga-j.