Linux Shell Usage Summary

Source: Internet
Author: User
Tags bit set

Why do scripts use the./+ script name to execute the script
Understand:
Because if the script name is used directly, then the Linux system will go to path path to find
such as go to/bin usr/bin, such as find, this time will not find the script name, will be error.
Use./This is the equivalent of telling the system to look under the current path.


Using simple examples to analyze the use of a For loop

#!/bin/bash
For skill in Ada coffe Action Java
Do
echo "I am good at ${skill}script"
Done


Find a For loop from the actual use process if the following parameter is added "" or cannot be reached with {}
The purpose of the loop required. If you use "" then the actual output is only once, if you use {} will be {}
Output as normal characters.

Discuss variables and how scripts are defined to make them look more beautiful
Myurl
MyNum
My_list
Personally, you can make the variable definition look better.

Variable Increment {} is in order to add bounds to $
If you define a variable Myurl
${myurl} personally feel more like writing this way
ReadOnly Myurl
Variables defined with ReadOnly are read-only variables and cannot be modified after the definition of the variable is complete.

List of special variables
The 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.

Variable substitution
${var} variable Original value
${var:-word} If the variable var is empty or has been deleted (unset), then return to word, but do not change the value of var.
${var:=word} If the variable var is empty or has been deleted (unset), return to Word and set the value of Var to word.
${var:?message} If the variable var is empty or has been deleted (unset), then send message messages to the standard error output, which can be used to detect whether Var can be assigned to a normal value.
If this substitution appears in the shell script, the script will stop running.
${var:+word} If the variable var is defined, then return to word, but do not change the value of var.


Relational Expressions:
-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-hand number is less than or equal to the right, and returns true if it is. [$a-le $b] returns True

Relational expressions support only numbers that do not support strings


List of Boolean operators
! Non-operation, the expression is true returns False, otherwise true. [! false] returns TRUE.
-O or operation, which returns true if an expression is true. [$a-lt 20-o $b-GT 100] returns TRUE.
-A with operation, two expressions are true to return true. [$a-lt 20-a $b-GT 100] returns FALSE.


List of string operators
= 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 if 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 if the string is empty and does not return true for null. [$a] returns TRUE.

The


file tests the operator
-B to detect if the file is a block device file, and if so, returns True. [-B $file] returns FALSE. The
-C file detects whether the files are character device files and, if so, returns True. [-B $file] returns FALSE. The
-D file detects whether the files are directories and, if so, returns True. [-D $file] returns false. The
-F file detects whether files are normal files (neither directories nor device files), and returns True if they are. [-F $file] returns TRUE. The
-G file detects if the SGID bit is set, and returns True if it is. [-G $file] returns false. The
-K file detects whether the files have a sticky bit set (Sticky bit) and, if so, returns True. [-K $file] returns false. The
-P file detects whether the files are named pipes and, if so, returns True. [-P $file] returns false. The
-U file detects whether the file has a SUID bit set and returns True if it is. [-U $file] returns false. The
-R file detects whether the files are readable and, if so, returns True. [-R $file] returns TRUE. The
-W file detects whether the files are writable and, if so, returns True. [-W $file] returns TRUE. The
-X file detects whether the file can be executed and, if so, returns True. [-X $file] returns TRUE.
-S file detects whether the files are empty (the file size is greater than 0) and does not return true for null. [-S $file] returns TRUE. The
-e file detects the existence of files (including directories) and returns True if it is. [-e $file] returns TRUE.


Use of strings
1. Stitching strings
Two strings together is a concatenation of the string
2. Get the string length
string= "ABCD"
echo ${#string} #输出 4
To get the string length, precede the string with the # sign
[Email protected]:~/linux$ str= "mystring"
[Email protected]:~/linux$ echo "${#str}"
8

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 '

Linux Shell Usage Summary

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.