Linux Programming (ii) Shell programming

Source: Internet
Author: User
Tags ming

1. Piping and redirection

File descriptor 0: standard input for a program

File descriptor 1: Standard output

File descriptor 2: standard error output

> Operator: Redirects the standard output to a file.

>> operator: Attaches the output to a file.

| operator: the connection process.

2. Shell as a programming language

2.1 Creating a Script

Comments begin with a # symbol.

#! The character that tells the system to follow it on the same line is the procedure used to execute this file.

2.2 Set the script to execute

chmod +x First

3. Shell syntax

3.1 Variables

By default, all variables are treated as strings and stored as strings. If the strings contain spaces, they must be enclosed in quotation marks.

In the shell, access its contents by adding a $ sign to the variable name. When assigning a value to a variable, only the variable name is required.

[Email protected]:~/mypro$ salutation=Hello[email protected]-virtualbox:~/mypro$ echo $salutationhello [email protected]-virtualbox:~/mypro$ salutation ='Hello World'Salutation: No command found [email protected]-virtualbox:~/mypro$ salutation='Hello World'[email protected]-virtualbox:~/mypro$ Echo $salutationhello world[email protected]-virtualbox:~/mypro$ salutation=7+5[email protected]-virtualbox:~/mypro$ Echo $salutation7+5

Use the Read command to assign the user's input to a variable.

[Email protected]:~/mypro$ read Salutation Ni hao, Xiao Ming[email protected]-virtualbox:~/mypro$ Echo $sal Utationni Hao, Xiao Ming

Similar to PHP syntax, a variable with a $ character is placed in double quotation marks, and the program executes to this line by replacing the variable with its value;

If you put it in single quotation marks, there is no substitution phenomenon.

Adding a \ Character before the $ character cancels its special meaning.

#!/bin/SHMyVar="Hi There"Echo$myvarEcho "$myvar"Echo '$myvar'Echo\ $myvarEchoEnter some textread myvarEcho '$myvar'Now equals $myvarexit0~

Execute this script:

[Email protected]:~/mypro$./myvar. SH Hi Therehi there$myvar$myvarenter some textgood bye $myvar now equals good bye

Linux Programming (ii) Shell programming

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.