One of shell Study Notes

Source: Internet
Author: User

1. First look at a common command

PS-EDF | sort | uniq | grep-V sh | more

This command combines several different shell commands, which have this feature. Uniformity and simplicity.

You can use pipeline commands to combine multiple shell commands. The preceding commands are as follows:

Displays the currently running processes. Sort sorts the processes, uniq removes duplicates, and grep-V sh removes duplicates, more indicates more.

 

Similarly, the command can also be written as follows:

PS-EDF | sort | uniq | grep-v sh> test.txt

Redirects the command execution result to a text document.

 

2. Application of interactive shell programming

For file in * <br/> DO <br/> If grep-I POSIX $ file <br/> then <br/> more $ file <br/> fi <br/> done

Those who have learned programming will not be unfamiliar with this example. The only thing that may be confused is what is file?

Here, file is a variable. $ File indicates variables referenced in shell programming. Of course, you can also use another name, but obviously the file name is easier to understand.

 

3. Variables in Shell

Shell programming is concise, so it is confusing to use syntax and variables. But there is no way, that's how it is defined!

The variable definition in shell can be defined at any time. If you want to obtain the variable value during use,

The dollar sign is added before the modifier variable. For example, $ file is replaced with the file value;

As follows:

$ File = "Hello World" <br/> $ echo $ file <br/> $ Hello World

Double quotation marks are used to include strings with spaces to avoid errors.

If you use single quotes or add a backslash/before $, the variable name will not replace the value, but the name of the output itself, as follows:

$ File = "Hello World" <br/> $ echo/$ FILE <br/> $ echo '$ file' </P> <p> // output <br/> $ file <br/> $ File

 

4. Shell variable assignment

There are two ways to assign values directly, for example

File = "my file"

The other is read from standard input, such

$ Read filename <br/> $ my file </P> <p> $ echo filename <br/> $ my file

 

5. Environment Variables in shell programming

Any programming requires environment variables, and shell is no exception. It is more dependent on the system's own variables, of course, you can also specify.

Run the command, ENV, or run the command

$ Echo $ home <br/> $ echo $ path

The value of this variable is displayed. Why do we need to add the $ symbol to display the variable value!

For the win system, the environment variable displayed in cmd is Echo % home %

 

6. parameter variables in shell programming

Let's take a look at the following example:

#! /Bin/sh <br/> para = "hello" <br/> echo $ para <br/> echo "The program $0 is now running" <br/> echo" the second para is $2 "<br/> echo" the first para is $1 "<br/> echo" The env home is $ home "</P> <p> exit 0

Run this script

$./Test. Sh A B C

The result is

Hello
The program test. Sh is now running

The second para is B

The first para is
The env home is home/usr/

Therefore, it is clear that the parameter call in shell programming is similar to the char ** argv of the main function in C!

 

PS:

Unix/Linux systems support standard input, output, and error redirection. 0, 1, and 2 indicate the standard input, output, and error respectively, and the symbol> and <indicate the output and the input redirection respectively.

If you open a file,

VI test.txt

You can also open it like this.

VI 0 <test.txt

For more information, see Google "standard input, output, and error ".

 

PS: Why sh?ProgramCannot Execute?

View the permissions of the file and add executable permissions to the file. Just run chmod + X test. Sh;

Or the program name conflicts with the system command. When executing the command, you can use./test. Sh to tell the shell the complete path of the current program script.

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.