Shell Programming Test Command

Source: Internet
Author: User
Tags readable

The testing Test command in shell programming can basically be divided into 3 data types, each of which is different. Individuals are more inclined to understand the rules of writing conditional statements, that is, test plus conditional plus judgment statement.

1 Numeric types

Basic can be divided into 6 judgments:-eq equals,-ne not equal to,-gt greater than,-lt less than,-ge greater than equals,-le less than equals

Example: a=1;b=2

if test $[a]-eq $[b] #如果a等于b, then ...,-NE,-GT,, and so on can, in addition, if not a formula, you can not add square brackets, $a + $b

Then

echo "A=b" #缩进4个4个都行, habitually indented 4

Else

echo "A!=b"

PS: Formulas are written in two ways: $[a + b] and ' expr $a + $b '

2 string

There are 4 judgments: =,! =,-Z (length 0) and-n (length not 0)

Example: astr= ' hehehehe '; Bstr= ' Hahahaha '

if test $astr = $bstr

Then

echo "ASTR = BSTR"

Else

echo "Astr!=bstr"

Fi

3 File Test

Basically there are 9 commonly used parameters, the basic format is Test + parameter + filename, the parameters are:

-e:exists, there is such a file;

-r:readable, the document is readable;

-w:writable, a document that can be written;

-X: The file is executable,

-S: exists and is not empty

-d:directory, Existence directory

-f:file, exists and is an ordinary file,

C: File exists and is a special file of character #这个地方怎么理解呢, can be associated with the first character of the LS command : '-' represents a file. D is the directory,l represents the connection file,b represents the storage interface device in the device file,C represents the serial port settings inside the device file. Https://zhidao.baidu.com/question/1820545103813500748.htm  

Instance:

Cd/etc

                If test-f/etc/passwd        #此处可以结合与或非逻辑语句: Shaped like if test - e ./notfile -o -e ./ bash indicates presence./notfile or./bash

Then

echo "Yes"

        else 

echo "No"

Fi

Shell Programming Test Command

Related Article

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.