Learning of shell scripts in Linux (i)

Source: Internet
Author: User

The shell in Linux is a special application. It is between the kernel of the system and the user. Serves as the role of the command "interpreter". Responsible for receiving the user input operation instructions, and to explain. Pass the action that needs to be performed to the kernel. and output the execution result;

Let's look at the types of shells supported by the current system:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/96/wKioL1RUM_nich3bAAD5PvaLXpY860.jpg "title=" Sell.jpg "alt=" wkiol1rum_nich3baad5pvalxpy860.jpg "/> where/bin/shell is the default shell currently used in most Linux. We mainly study bash,



Let's write the first script file to use as a test.

Vim first.sh

cd/boot/

Pwd

LS-LH *

chmod +x first.sh



650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/96/wKioL1RUNW7AMbqzAAL3l-Y8G4k812.jpg "title=" aa.jpg "alt=" Wkiol1runw7ambqzaal3l-y8g4k812.jpg "/>

Shell script Standard Hashimoto structure:

Vim first.sh


#!/bin/bash

#this is my first shell.

Cd/boot

echo "The current directory is located in"

Pwd

echo "A file that begins with a includes:"

LS-LH *


chmod +x first.sh Give it permission to execute


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/96/wKioL1RUN0ugCAaAAACzQDMsIao927.jpg "title=" QQ picture 20141101092722.jpg "alt=" Wkiol1run0ugcaaaaaczqdmsiao927.jpg "/>

It can also be done by Sh.first or. First.sh to execute the script:


Redirection and pipeline operations:

REDIRECT output uses > or >> operation symbols. > For overlay >> more for append use

For example:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4D/96/wKiom1RUOBfxUVpMAABLlYVclX0640.jpg "title=" QQ picture 20141101093216.jpg "alt=" Wkiom1ruobfxuvpmaabllyvclx0640.jpg "/>

Additional applications:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/4D/96/wKiom1RUOI2zMMFUAABrJKw-PcE851.jpg "title=" QQ picture 20141101093415.jpg "alt=" Wkiom1ruoi2zmmfuaabrjkw-pce851.jpg "/>


REDIRECT input: Redirect input using the "<" operator

Enables some interactive procedures to be able to read files to complete:

Vim Pass.txt

123.com

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/4D/96/wKiom1RUOeazNPdCAABcQoPD7dU030.jpg "title=" QQ picture 20141101094003.jpg "alt=" Wkiom1ruoeaznpdcaabcqopd7du030.jpg "/>

Direct the password directly to the Hujianli user

Error redirection :

For example, save the error message that appears when you back up the tar command to the Error.log file:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/96/wKiom1RUOtPhv-RMAAC0kqrfw-g992.jpg "title=" aa.jpg "alt=" Wkiom1ruotphv-rmaac0kqrfw-g992.jpg "/>

Files are overwritten as > when using 2>. To append content instead of overwriting the file, use the 2>> operator.


For example: auto-compile script. Define the output information into the/dev/null.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/96/wKiom1RUPPqyvg_-AAC4NyJ4tbU647.jpg "title=" aa.jpg "alt=" Wkiom1ruppqyvg_-aac4nyj4tbu647.jpg "/>



Pipeline Operation :

"|" is located in | The result of the left command output is returned as input to the right command (out of the object), and there can be more than one pipe in the same command

In the Shell | It is commonly used as the key information needed for filtering:

For example: Write a script that displays only the user name and login shell columns in passwd:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/96/wKiom1RUPqeiIIdrAADvQhWVLzw320.jpg "title=" aa.jpg "alt=" Wkiom1rupqeiiidraadvqhwvlzw320.jpg "/>

The role of awk is to put: as a separate, output $1,$7 string. -F is used as the specified delimiter: Not specified is delimited by a space mark.

List One more:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/97/wKiom1RUQF6Qh_T_AAIZwzeBtxA346.jpg "title=" aa.jpg "alt=" wkiom1ruqf6qh_t_aaizwzebtxa346.jpg "/>grep"/$ "delegate/Partition Object awk ' {print $} ' represents a space as a separator, 5th


For example:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/97/wKiom1RUQhujc9tiAADeuJ74w-4510.jpg "title=" aa.jpg "alt=" Wkiom1ruqhujc9tiaadeuj74w-4510.jpg "/>


To apply the value of a variable:

For example:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/96/wKioL1RUQ0CwblCkAAEhNmaQpq0325.jpg "title=" aa.jpg "alt=" Wkiol1ruq0cwblckaaehnmaqpq0325.jpg "/>


Double quotes:

Hujianli = "Jianli"

Echo $hujianli

Jianli


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/4D/96/wKioL1RURCHD46UKAAFTBMQxVAk699.jpg "title=" aa.jpg "alt=" Wkiol1rurchd46ukaaftbmqxvak699.jpg "/>


Single quotes, cannot interpret other variables, all the contents of the single quotation mark are output as-is.

There is no demo here.

Anti-apostrophe: The content output of the "inside" is mainly displayed.

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4D/97/wKiom1RURR3RPi47AAGCG0hsPIs298.jpg "title=" aa.jpg "alt=" Wkiom1rurr3rpi47aagcg0hspis298.jpg "/>


Read is a built-in command for bash, which assigns values to variables. For example:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/4D/97/wKiom1RUReGDrLA-AADJgKO4OH4075.jpg "title=" aa.jpg "alt=" Wkiom1ruregdrla-aadjgko4oh4075.jpg "/>



Let's review this a little bit today and continue .... Shell scripts,


This article from the "heartbroken people in the Tianya" blog, declined reproduced!

Learning of shell scripts in Linux (i)

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.