Shell programming (i)--common variables and expressions

Source: Internet
Author: User
Tags arithmetic

$? Represents the return value for executing the previous command: If 0 means execution succeeds, non 0 means execution fails
$, $ 、。。。 Represents the parameter 1, parameter 2, respectively 、。。。
$# indicates the number of parameters for this program
$*/[email protected] Denotes all parameters of this program
$$ represents the PID of this program
$! Represents the PID that executes the previous background command
$_ indicates that this variable holds the value of the last parameter of the command executed before

Execute script with-X to show the execution of the script
such as Sh-x stats.rd.task.sh

Do not execute script, just check syntax pattern, will return all syntax errors
such as Sh-n stats.rd.task.sh

Command substitution '

The case structure is the same as the switch structure in C + +. It allows you to choose multiple paths in a block of code by judging
One of the tracks. Its function is the same as the function of multiple if/then/else statements, which is their simplified structure, especially for the
Create a menu.
Case ' $variable ' in
$condition 1 ")
TT class= "replaceable" >command ...
;
$condition 2 ")
TT class= "replaceable" >command ...
;
Esac
1, the use of the variable "" is not mandatory, because the word segmentation will not occur
2, each sentence of the test line, with the right parenthesis to end
3. Each conditional judgment statement block ends with a pair of semicolons;
4. Case block ends with ESAC (reverse spelling of case)

Expr
General evaluation expressions: The parameters must be concatenated by the given action (the arguments should be separated by a space), and the parameters evaluated. It can be an arithmetic operation, a comparison operation, a string operation, or a logical operation.
Expr 3 + 5
#返回8
y= ' expr $y + 1 '
Increasing the value of a variable is the same as let Y=y+1 and y=$ (($y + 1)). This is an example of using an arithmetic expression.
z= ' expr substr $string $position $length '
Extracts a substring of $length length on the location $position.

While
This structure at the beginning of the loop to determine whether the condition is satisfied, if the condition has been satisfied, then the cycle continues (return 0 as the exit status code). The difference from A for loop is that the while loop is more suitable for use in situations where the number of loops is unknown.
While ["$var 0"-lt "$LIMIT"]
Do
Echo-n "$var 0" #-n will prevent new rows from being generated.
# ^ spaces, separating between numbers.

var0= ' expr $var 0 + 1 ' # var0=$ (($var 0+1)) is also possible.
# var0=$ ((var0 + 1)) is also possible.
# let "var0 + = 1" can also.
Done # Use other methods as well.

2>&1
Sends the output of the error message (2) to the place pointed to by standard output ($).

/dev/null
Think of/dev/null as a "black hole". It is very equivalent to a write-only file. All content written to it will be lost forever. While trying to read from it, nothing can be read. However,/dev/null is very useful for command lines and scripts.
Cat $filename >/dev/null
# file content is missing, not output to standard output.
RM $badname 2>/dev/null
# This error message [standard ERROR] has been thrown into the Pacific Ocean.

Shell Programming (i)--common variables and expressions

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.