Shellscript (3) usage of the test command and []

Source: Internet
Author: User

As the name suggests, the formula in shell script is to determine whether a sub-statement meets certain conditions. There are several implementation methods in script:

1. Use the test command

Let's take a simple example. For example, to determine whether a file script1.sh exists in the current directory, you can write the following statement based on the syntax used by test: if test-e script1.sh exists, the returned value is 1, so we can combine & | the logical operation can be constructed:

Test-e script1.sh & echo "exist" | echo "Not exist"

If yes, the & operation will continue and the "exist" will be output ". If it does Not exist, execute | the subsequent operation and output "Not exist ".

By the way, the usage of & | in the script is the same as that in the programming language.

Next, let's take a look at the functions of the test command corresponding to other parameters:

I. test [-efdbcSPL] filename is used to determine whether a file exists and the file type

-F determines whether a file is used. -D determines whether it is a dictionaty.-B (blocl device)-c (character device)

-S (Socket file)-P (FIFo/pipe file)-L (link file)

Ii. test [-rwx] filename

Determines the execution permission of a file and-r determines whether the file is readable. -W is used to determine whether it is writable. -X determines whether it is executable

Iii. test file1 {[-nt] [-ot] [-ef]} file2

It is used to determine whether the two files are old and new.-ef is used to determine whether the files are the same or whether they are linked to the same file. -Nt is the abbreviation of newer than, so we only need to remember the abbreviation.

Iv. test n1 {[-eq] [-ne] [-gt] [-lt] [-ge] [-le]} n2

Used to determine the relationship between two integers n1 and n2. Note that in this case, n1 and n2 are treated as integers. Of course, you can also add double quotation marks.

5. test [-zn] string determines whether the string is null or not

Test str1 = str2 judge whether two strings are equal

Test str1! = Str2 judge whether it is not equal

6.-a returns true only when both conditions are met.

-O returns true when any condition is set.

! If one of them fails, return.

The following is an example to illustrate the use of test: This script can be used to determine whether a file exists based on the file name entered by the user, whether it is a regular file or a document, or the type of the output document.


2. Next let's take a look at how to determine through "[]" <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Export + WyBdILrNdGVzdNPQzazR + export + jrMjno7o8L3A + export/export + 7rFwb2x37750qrT0L/VJiMyNjY4NDuho8/C W + bAtL + 00ru49sD919OjutXiuPa9xbG + v8nS1Lj5vt3Tw7untcTK5MjryscgeQ = "Y or n/N.


3. Next let's take a look at the default variables $0, $1, $...

Here we will talk about the number of parameters that can be included during shell script execution. The number of the parameters and the number of digits can be identified by the default variables of shell script.

For example, sh sh01.sh a B c d, the B C D Here is four c parameters.

$0 = sh01.sh indicates the script file name to be executed.

$ # Indicates the number of subsequent parameters. Here is 4

$ @ Indicates all the parameters here. Here is A B C D

The following is an example of a script:


The shift command can cause the parameter offset.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.