Shell from getting started to discarding step two + +

Source: Internet
Author: User
Tags arithmetic arithmetic operators bit set

This article takes the shell from getting started to giving up the first step


Thus, continue from small white to give up the second part of @[email protected]

First, the shell operator

The shell supports a number of operators, including: Shell arithmetic operators, relational operators, Boolean operators, string operators, and so on

Arithmetic operations
Arithmetic operators:
+,-
*, /
%: Take the remainder
* *: Sub-party

Arithmetic operation format:
(1) Let var= arithmetic expression
(2) var=$[arithmetic expression]
(3) var=$ (arithmetic expression)
(4) var=$ (expr $ARG 1 $OP $ARG 2)

For example, two numbers are added:

#!/bin/bash


val=' expr 2 + 2' #或者 var=$ (expr 2 + 2)

echo "Total value: $val"

Run the script output:

Total Value:4

Two points Note:

    • There are spaces between the expression and the operator, such as the 2 + 2, which is different from most of the programming languages we are familiar with.

    • The complete expression is to be contained, note that this character is not a common single quote, below the ESC key. Represents a command substitution, or uses $ ()


#!/bin/sha=10b=20val= ' expr $a + $b ' echo ' A + B: $val ' val= ' expr $a-$b ' echo ' A-B: $val ' val= ' expr $a \* $b ' echo ' A *  : $val "val=" expr $b/$a ' echo ' b/a: $val "val=" expr $b% $a ' echo ' B% A: $val "if [$a = = $b]then echo" A is equal to B "Fiif [$a! = $b]then echo" A is not equal to B "fi run Result: A + b:30a-b: -10a * b:200b/a: 2b a:0a is not equ Al to B

Attention:

    • Multiplication sign (*) must be preceded by a backslash escape (\) in order to achieve the multiplication operation;


Relational operators

relational operators only support numbers, and strings are not supported unless the value of the string is a number.
operator Description Example
-eq Detects whether two numbers are equal and returns true for equality. [$a-eq $b] returns TRUE.
-ne Detects whether two numbers are equal and returns true if they are not equal. [$a-ne $b] returns TRUE.
-gt Detects if the number on the left is greater than the right and, if so, returns True. [$a-gt $b] returns false.
-lt Detects if the number on the left is less than the right and, if so, returns True. [$a-lt $b] returns TRUE.
-ge Detects if the number on the left is large equal to the right, and returns true if it is. [$a-ge $b] returns false.
-le Detects if the left number is less than or equal to the right, and returns true if it is. [$a-le $b] returns TRUE.

#!/bin/sha=10b=20if [$a-eq $b]thenecho "$a-eq $b: A is equal to B" Elseecho "$a-eq $b: A was not equal to B" fi run result is 10 -eq 20:a is isn't equal to B

Boolean operator

operator example
non-operation, the expression is true returns False, otherwise returns true. [! false] returns TRUE.
-o or operation, there is an expression of true to return true. -a [$a-lt 20-a $b-GT 100] return to  false.

Example

#!/bin/sha=10b=20if [$a-lt 100-a $b-gt]then echo "$a-lt 100-a $b-gt 15:returns true" else echo "$a-lt 100 -A $b-gt 15:returns false "fi run result is 10-lt 100-a 20-gt 15:returns True


String operators

operator Description Example
= Detects whether two strings are equal and returns true for equality. [$a = $b] returns FALSE.
!= Detects whether two strings are equal and returns true if they are not equal. [$a! = $b] Returns TRUE.
-Z Detects whether the string length is 0 and returns true for 0. [-Z $a] returns false.
-N Detects whether the string length is 0 and does not return true for 0. [-Z $a] returns true.
Str Detects whether the string is empty and does not return true for null. [$a] returns TRUE.
#!/bin/sha= "abc" b= "EFG" if [$a = $b]then echo "$a = $b: A is equal to B" else echo "$a = $b: A was not equal to B" fi run Result ABC = efg:a is not equal to B


File Test Operators

file test operators are used to detect various properties of Unix files.

operator Description Example
-B File Detects if the file is a block device file, and returns True if it is. [-B $file] returns FALSE.
-C file Detects if the file is a character device file, and returns True if it is. [-B $file] returns FALSE.
-D File Detects if the file is a directory, and returns True if it is. [-D $file] returns false.
-F File Detects if the file is a normal file (neither a directory nor a device file), and returns True if it is. [-F $file] returns TRUE.
-G file Detects if the file has a SGID bit set, and returns True if it is. [-G $file] returns false.
-K File Detects if the file has a sticky bit set (Sticky bit), and returns True if it is. [-K $file] returns false.
-P File Detects if the file is a named pipe, and returns True if it is. [-P $file] returns false.
-U file Detects if the file has a SUID bit set, and returns True if it is. [-U $file] returns false.
-R File Detects if the file is readable and returns true if it is. [-R $file] returns TRUE.
-W File Detects if the file is writable and returns true if it is. [-W $file] returns TRUE.
-X File Detects if the file can be executed and, if so, returns True. [-X $file] returns TRUE.
-S file Detects whether the file is empty (the file size is greater than 0) and does not return true for null. [-S $file] returns TRUE.
-E File Detects whether the file (including the directory) exists and, if so, returns True. [-e $file] returns TRUE.

This is very simple, don't give an example.


Second, string and comment

strings are the most common and useful data types in shell programming (except numbers and strings, and no other type works well), strings can be in single quotes or double quotes, or without quotes. The difference between single and double quotes is similar to PHP.


Single quotation marks

str= ' This is a string '
Single-Quote string restrictions:

Any character in a single quotation mark is output as is, and the variable in the single-quote string is not valid;

Single quotation marks cannot appear in single quote strings (not after using escape characters for single quotes).

Double quotes

your_name= ' qinjx ' str= "Hello, I know your is \" $your _name\ "! \ n"
Advantages of double quotes:

You can have variables in double quotes.

Escape characters can appear in double quotes

Stitching strings

Your_name= "QINJX" greeting= "Hello," $your _name "!" greeting_1= "Hello, ${your_name}!" echo $greeting $greeting _1


Get string length

string= "ABCD" Echo ${#string} #输出 4


Extract substring

String= "Alibaba is a great company" Echo ${string:1:4} #输出liba

Finding substrings

String= "Alibaba is a great company" Echo ' Expr index "$string" is "

Not to be continued ...

This article from the "mood refined, quiet Zhiyuan" blog, please be sure to keep this source http://zhaoyongtao.blog.51cto.com/10955972/1770847

Shell from getting started to discarding step two + +

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.