Shell Programming Specifications and variables

Source: Internet
Author: User
Tags arithmetic echo command

Concept:

The commands that will be executed are saved sequentially to a text file, which can be run by giving the file an executable permission.

The composition of the shell script:

1) Script statement: The script declaration indicates which interpreter to use to interpret

2) Note information: The comment information is a description of the script that indicates the information. You know what this script is for, this information is not executed.

3) Executable statement: Some commands for execution

How to execute a shell script

1) Absolute Path

2) Relative path

3) SH Script path

4) Source Script path
Note: In this case, the script file must have executable permissions when the absolute path and the relative path are applied to the shell script. The method of the source script path, which only takes effect in the current environment.

Pipe operator "|"

Outputs the command on the left as the processing object for the right command
Count the number of rows in passwd cat/etc/passwd | Wc–l
Filter View user cat/etc/passwd for Wang in passwd | grep "Wang"

Redirect (three types)

Standard input (/dev/stdin): The data entered by the keyboard is the standard input
Standard output (/dev/stdout): The data for the display output is the standard output
Standard error (/DEV/STDERR): Error message displayed on the monitor is standard error

REDIRECT input:

1) First create a mima.tx text and redirect it to a user as a password
Pass User name < Mima.txt
Note: Password requirements must meet the system default requirements, otherwise the error will be the password change termination.

REDIRECT output:

1) redirect the file under the specified path or the text entered directly into the specified file
echo "Wo Zui Shuai" > Test.txt
Note: There is no need to create a file in advance, because when the output is redirected, the file is not detected, and the creation format is specified, such as: Test.txt Test.log Test.bak

2) redirect Append, append the output text or content to the specified text content
echo "da Jia Dou Shuai" >> test.txt
Note: This will not affect the original content of the text, but also to add new content, but only after the original content.

Standard error Output:

1) When querying a file or directory that does not exist, you will be prompted, unable to access it, without that file or directory.

2) Use redirection to output the error message to Err.txt text (this text is not available by default)

3) Output error message needs to use 2> to output

Mixed output:

1) outputs two or more different outputs at the same time, such as simultaneous output error output and standard output

Custom variables (defined, modified, and used by the user)

1) define x equals 123, use echo command here

2) Continue with the previous step and display the newly added 456

Note: There is a space between the two values in the quotation marks above, and the other is not displayed, and will fail.

3) If you want to achieve 123456 such continuous output, the operation of the following two (brackets different)

4) Calculation subtraction Use the expr command to add the following format directly

5) Use variables to calculate the addition of the following format

6) Set the command to variable view all users of the statistics system

Defining variables and outputs

1) input format: read "-P" prompt message "" Variable name

Note: The lazy baby here is a variable, and is the input method to add the variable and first give the variable name x

2) Add two variables and use the expr command to implement

Arithmetic operations of variables

The value of the variable is used in the process control of the script, only simple integer operation, not supporting the fractional operation, the integer value operation is mainly through the internal command expr. Format: Expr variable 1 operator variable 2 operator ...

1) Use the expr command to implement the add and subtract arithmetic operations

2) Use the expr command to implement multiplication arithmetic operations (including remainder)

Note: As can be seen from the top, the multiplication needs to use the escape character \ To recognize the operation, and the division should be 3.7.0 ... Because only integers can be taken, so take 3, and 123 divided by 456 the answer should be a decimal, but equal to 0, because this command does not support decimals; the remainder of 456 divided by 123 is 87.

Pre-defined variables

Predefined variables are pre-defined special variables for bash programs that users can only use predefined variables, not create new predefined variables, or assign values directly to predefined variables.
The execution name of this program
$n the nth parameter value of this program, n=1..9
$ 9 for all parameters of the program, this option can be more than one parameter.
$# the number of parameters for this program
$$ the PID of this program (the current process ID number of the script run)
$! PID of the previous background instruction (process ID number of the last process running in the background)
$? Executes the return value of the previous instruction (displays the exit status of the last command. 0 means no error, any other value indicates an error)
$-shows the current options used by the shell, same as the SET command function
[Email protected]
is similar to $ but can be used as an array

Environment variables

An environment variable refers to a class of variables that are created in advance by the Linux system for operational needs, primarily for setting up the user's working environment. Use the ENV command to view environment variables in the current working environment. The path variable is used to set the default search path for executable programs. The global configuration file for environment variables is/etc/profile, and each user has its own standalone profile ~/.bash_profile

Shell Programming Specifications and variables

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.