Shell Getting Started Tutorial (1)-shell Basics

Source: Internet
Author: User
Tags arithmetic echo command

Shell Getting Started Tutorial (1)-shell Basics-JUSTKK's Column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/43795131

Shell Getting Started Tutorial (2)-Variables and Parameters-JUSTKK's Column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/44081993

Shell Getting Started Tutorial (3)-command editor-JUSTKK's column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/44617445

Shell Getting Started Tutorial (4)-Job Control-JUSTKK's column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/46801163

Shell Getting Started Tutorial (5)-arithmetic operations-JUSTKK's column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/47025297

Shell Getting Started Tutorial (6)-Environment-JUSTKK's Column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/47025321

Shell Getting Started Tutorial (7)-Scripting-JUSTKK's Column-Blog channel-csdn.net
http://blog.csdn.net/justkk/article/details/47025397

One, simple script file

1, use VI or other editor to write the following two lines to a file, named 1.sh.

echo "We have these files:"

Ls

2. This is a simple script file for which to add Execute permissions:

chmod +x 1.sh

3. Execute (Previous./Specify current directory, meaning explicitly specify to execute 1.sh file under current directory)

./1.sh

Observe the output of the screen.

Look, this is the case with a simple script file.

II. implementation of the process

1, single-line multi-command

You can specify multiple commands to execute in a single row, separated by semicolons, and multiple commands executed sequentially. Such as:

Ls;date;pwd

2. Continuation of the Order

If you enter a backslash at the end of a line, you can continue the input of the entire command on the next row, such as:

echo a b \

> C

The > is the command continuation symbol given by the shell. The final output of the entire command is a B C

3. Backstage execution

Add the symbol & at the end of the command, at which point the command executes in the background. You do not have to wait for the current command to finish to enter a new command. Typically used to execute long-time commands. Such as:

Sleep &

This command sleeps for 10 seconds, and you can continue to perform other commands in the foreground while the command is in the background.

4. Pipeline

Indicates that the symbol is a vertical bar |, you can thread two commands, and the output of the preceding command will be input to the following command, such as:

Ls|wc–l

The WC command counts the number of input rows, so the effect of the entire command is to count the number of files in the current directory.

5. Conditional execution

If you use && to thread two commands, the subsequent command executes only if the previous command succeeds. Such as:

LS 1.sh && echo "File 1.sh is Here"

Since 1.sh exists, the echo command behind it is executed and output information file 1.sh is here

If you delete the 1.sh file (rm 1.sh) and execute the above threaded command again, you will notice that the echo command is no longer executed.

Conversely, if you use | | After two commands are threaded, the subsequent command executes only if the previous command fails.

Thinking, Command1 && Command2 | | Command3

Command1 execution succeeds, Command2 executes, and Command3 does not execute.

Command1 execution fails, Command2 does not execute, and Command3 executes.

6. Command combination

You can use a pair of {} or () combination commands, and the combined commands are treated as a single command on the whole.

Compare the output of the following two lines of command and analyze:

Echo 123;echo 456|wc–l

{echo 123;echo 456;}| Wc–l

Note The format requirements, {There must be a white space character followed by}, and a semicolon must precede it.

{} The grouped commands are executed in the current shell, while the () grouped commands are executed in the child shell.

A child shell is a copy of the current Shell clone, and its execution results do not affect the current shell.

The concept of a child shell is detailed later.

III. IO redirection

1. REDIRECT Standard output

Command execution results are typically output to the screen and can be redirected to a file using the > symbol, such as: ls>flist

After the command executes, the screen no longer displays the output information, and the contents of the file Flist will contain a list of files in the current directory.

If the file does not exist before flist, it will be created and its contents will be overwritten if it existed before.

You can create an empty file directly using >, such as:

> A_new_file

You can use the >> to implement append redirection, the output is appended to the file, such as: LS >>flist

Noclobber Options

Disables redirection to an already existing file, which is turned off by default. You can use the following command to open:

Set-o Noclobber

When you try to redirect again, an error will be displayed: cannot overwrite existing file

ls > flist

However, you can still force redirection, ignoring the settings for this option, such as: ls>|flist

2, redirect the standard input, through the symbol < Use the contents of the file as input information of the command, such as: Cat < Flist

3. File descriptor

The program uses a file descriptor to identify the open file, and the shell automatically opens 3 descriptors for the program, 0,1,2

0 means standard input 1 means standard output 2 indicates standard error output

Description: The standard error output is also mapped to the screen by default, but unlike standard output, it is often used to output error messages.

REDIRECT error output, add descriptor 2 before symbol > (Note that there is no interval between 2 and >), assuming that the file AA does not exist:

LS AA 2>err.out

That is, you can add a descriptor before the symbol >, omitting the implied 1.

LS AA bb >myout 2>&1

Both the error output and the standard output are redirected to the file Myout, which is the output information that incorporates two descriptors.

4. Embed text

Cat <<!

Aa

Bb

!

One of the symbols! To define the end of the input information, the middle text is used as the input to the command.

Note that the ending symbol needs to be written at the beginning of a line and cannot be preceded by a <TAB> or space.

The notation is not mandatory, so you can use any character or string you like, as long as you pair it.

If in the following way, that is, before the symbol is added-the end of the symbol does not need to write the head, the front can be added <tab>, but still cannot add space.

Cat <<-!

The shell removes the <TAB> characters from the text.

Note You can also add descriptors before:<<.

5. Discard standard output

LS >/dev/null

LS AA bb >/dev/null 2>&1

The latter discards both the standard output and the standard error output.

/dev/null is a special device that can be seen as a black hole.

Iv. File name substitution

1, Characters *--match 0 or more characters

Matches all files in the current directory (with decimal points) when used alone. The name of the file that preceded it.

LS A * Displays all file names that begin with the letter A

LS *b displays all file names ending with the letter B

2, characters? --match exactly one character

Ls?? Displays a two-character file name

3, character []--match a range

LS [ab]* displays all file names that begin with the letter A or B

LS [a-z]* displays all file names that begin with lowercase letters

4, Characters! --used in conjunction with [] to indicate a reverse match

LS [!a]* shows all file names that are not preceded by the letter A

5, characters. --explicitly specify a match. The name of the file that preceded it

Ls. [A-c] Displays all file names that begin with. a/.b/.c

6. Composite Mode

* (pattern) mode repeats 0 or more times

? (pattern) mode repeats 0 or 1 times

+ (pattern) mode repeats 1 or more times

@ (pattern) mode repeats 1 times

! (pattern) Pattern exclusion

For example: LS * (a) shows all a repeats any number of file names

7. Prohibit file name substitution

Set-f or Set-onoglob

At this time special characters *?, etc. lose their special meaning, just represent the character itself.

V. Command substitution

1. Expand the output of the command in place, usually to assign a value to a variable, or to embed the output of another command in a command.

The format is: $ (command), example:

a=$ (date + "%y%m%d") defines a variable that is assigned the date of the current day

echo "Today is $ (date +"%y%m%d ")"

2, or adopt compatible format: ' Command '

Note that the symbol is the character on the left side of the number 1 on the keyboard

3, direct file input, the format is: $ (<file)

The whole is replaced with the contents of the file, such as:

x=$ (<foo) defines a variable that is assigned to the contents of the file foo

4, arithmetic operations, $ ((arithmetic-expression))

such as: Echo $ ((3*9))

Six, wave number ~ Replace

~ Expand to Current user's home directory

~user Expand the home directory for user-specific users

~-expands to the previous directory, which is $oldpwd

~+ expands to the current directory, that is, $pwd

CD-Switch to the previous directory

Test, and observe the results:

Cd/tmp

CD ~root

CD-

Once again CD-

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.