Shell Note 2

Source: Internet
Author: User
Tags bit set

1.Linux default shell is bash,unix default shell is sh

Bash is fully compatible with SH

2. Define variables:

When defining a variable, the variable name does not have a dollar sign ($), and a variable is used before the variable name plus $

There can be no spaces between the variable name and the equals sign

Variable names can use characters that are numbers, letters, and underscores only

The first character must be a letter

The curly braces outside the variable name are optional, plus the curly braces are used to help the interpreter identify the bounds of the variable, for example: ${skill}script

3. Thereadonly command can define a variable as a read-only variable

unset command to delete a variable

4.

List of special variables
variables meaning
$ File name of the current script
$n Arguments passed to the script or function. N is a number that represents the first few parameters. For example, the first parameter is $ $, and the second argument is $ A.
$# The number of arguments passed to the script or function.
$* All parameters passed to the script or function.
[Email protected] All parameters passed to the script or function. When enclosed by double quotation marks (""), it is slightly different from $*, as will be mentioned below.
$? The exit state of the last command, or the return value of the function.
$$ The current shell process ID. For Shell scripts, this is the process ID where the scripts are located.

5.shell variable Substitution

ECHO-E represents the substitution of escape characters:

    1. A=ten
    2. echo- e "Value of A is $a \ n"

Output:

   Value of A is 10

Command substitution:

' Command ' (note is the anti-quote, not single quote, this key is located below the ESC key.) )

6. Arithmetic operations using expr

Expr 3 + 2 (spaces between expressions and operators)

Expr 3-2

Expr 3 \* 2 (multiplication sign front plus backslash \)

Expr 3/2

Output is: 4

7. Conditional expressions

List of relational operators
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.

Conditional-expression Syntax:

 1  if  [$a-lt 100 -a $b-gt 15  ]  2  then  3  echo   "  $a-lt 100-a $b-gt 15:returns true   " 4  else  5  echo   "  $a-lt 100-a $b-gt 15:returns false   6  fi  

Conditional expressions are placed between square brackets and have spaces, such as [$a = = $b] is wrong and must be written as [$a = = $b]

List of Boolean operators
operator Description Example
! Non-operation, the expression is true returns False, otherwise true. [! false] returns TRUE.
-O Or operation, there is an expression of true to return true. [$a-lt 20-o $b-GT 100] returns TRUE.
-A With an operation, two expressions are true to return true. [$a-lt 20-a $b-GT 100] returns FALSE.

List of 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.

List of file test operators
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.

Shell Note 2

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.