Shell programming Syntax (i)

Source: Internet
Author: User
Tags echo command

Steps to create a shell program:

First step: Create a script file. Sh.

Second step: Grant permissions so that it can execute chmod u+x. sh

Step three: Execute./example or SH example

Shell structure:
1. #! Specifies the shell that executes the script
2. #注释行
3. Command and control structure
Variables: Temporary variables and permanent variables (environment variables)

Set variables: use uppercase letters to name variables. Variable names can only begin with the characters in the alphabet and cannot be used as numbers.

variable Assignment: the assignment number "=" should have no spaces on either side

Assign values when defined, such as Num=1

Assigns the execution result of a command to a variable, such as: time= ' Date '

Assign a variable to another variable, such as: A= $B

Note:

Variable with multiple words: $NAME =mike Ron will error, use $NAME = "Mike Ron" or $name= ' Mike Ron '

The difference between single and double quotation marks:

The content between the single quotes is interpreted by bash and is assigned to the variable as is.

The contents of the double quotation marks can only be escaped when the "$,", double quotes,/and newline characters are decorated with five special character modifiers.

Delete variable: #unset NAME

To view variables:

List all variables: # set

Use the echo command to view variable values. For example: Echo $A

Positional variables:$n the nth parameter value of this program, n=1-9

For example:

Ls-l file1 file2 File3

$ A file name for this program ls-l

Special variables:$? Executes the return value of the previous command $* all parameters of this program
$# the number of parameters for this program
$$ the PID of this program
$! PID that executes the previous background command

Shell command: Read command: Reads data from the keyboard, assigns values to variables such as: Read USERNAMEEXPR command: Arithmetic operations on integer variables such as: $ expr 3 \*, var2 = ' expr ' expr 30/10 '/$ Var1 '

Attention:

Operators have spaces around them
When using multiplication sign, you must mask its specific meaning with a backslash. Because the shell may misunderstand the meaning of displaying asterisks.

Shell programming Syntax (i)

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.