Shell Lesson Two learning notes

Source: Internet
Author: User

Shell Lesson Two learning notes

A. Script Specification declaration information:

#!bin/bash#date 2014-1-30#author xiaoping#mail [email protected] #Function: thsi scrits funcion is .... #version 1.1~

B. Attention points:

A. Write the paired content once

{}, [], ', ', ', ' #

B.[] space is required at both ends of the bracket

C. Process control statements write-once format

D.VI Multi-line indentation

Press V to enter visual state, select multiple lines, indent or indent with shit+> or shit+<

Variables in the C.shell:


A. Local variables and environment variables, local variables only apply to the current shell, and environment variables are global, it applies to all the current shell and any of its derived sub-processes, some variables are user-created, others are dedicated shell variables.

Environment variables can be set in the command, but they are also lost when the user exits, so it is best to define them in the. bash_profile file in the user's home directory or in the global configuration/ETC/BASHRC, or in the/etc/profile file or/etc/profile.d/. The environment variables are placed in the profile file, and the values are initialized each time the user logs on. Regulations all environment variables on the fan are capitalized. Environment variables must be exported with the Export command before they are applied to the user process.

b, local variables
The scope of local variables is scoped to the shell in which they were created. The local function can be used to create locals, but only within functions. A local variable can be set by simply assigning it a value or a variable name, set with declare built-in functions, or omitted.

Display environment variables such as:

[[email protected] jiaoben]$ echo $HOME/home/xiaoping[[email protected] jiaoben]$ echo $PS 1[\[email protected]\h \w]\$[ Email protected] Jiaobe
Histsize Number of command lines recorded in the command-line file
Tmout Number of seconds to wait for timeout before exiting
Vi/etc/profile #后面添加然后保存测试HISTSIZE =5 tmout=5[[email protected] jiaoben]#. /etc/profile[[email protected] jiaoben]# timed out waiting for input:auto-logout [[email protected] jiaoben]$[[email prot Ected] jiaoben]#. /etc/profile[[email protected] jiaoben]# history 618. /etc/profile 619 History 620 Vi/etc/profile 621. /etc/profile 622 History[[email protected] jiaoben]# timed out waiting for Input:auto-logout[[email protected] Jiaoben] $

D. self -defined environment variables standard: (usually letters, numbers, underscores, letters beginning)

A.export variable name =value

B. variable name =value;exprot variable name

C.declare-x variable name =value

Tip: The above three ways to set environment variables

E. Defining local Variables :

A general variable is a number without quotation marks, a command variable with a ' ' reference, and the other in double quotes.

Single quotes:

What you can say is what you see is what you get: the contents of a single quotation mark are output as-is, or what you see in single quotes is what you output.

Double quotes:

The contents of the double quotation marks are output, and if there are commands, variables, etc., the variables, commands are parsed out and then the final content is output.

No quotes:

strings that contain spaces are not treated as a whole output ,  If the content has commands, variables, etc., will first parse the variables, commands, and then in the output of the final content, if the string with a space and other special characters, you can not complete the output, you need to add double quotation marks, general continuous string, number, path, etc. can be used.

Example 1:

[[email protected] oldboy]# echo  ' ' Date ' # → When you see anything in single quotes, the ' date ' [[email protected] oldboy]# echo  "' Date '" #→ double quotes if there is a variable, Will first parse the variables into concrete content in the display sat oct 29 18:02:59 cst 2011[[email protected] oldboy]#  echo  ' Date ' #→ for continuous strings and other content generally without quotation marks, plus double quotation marks are generally more insurance, recommended sat oct 29 18:03:08 cst 2011 

Outreach: awk called Shell variable Problem

Double quotes:

What you see is what you get: The contents of the double quotes are output as-is, or what you see in double quotation marks will output.

Single quotes:

Output the contents of the single quotation mark, and if there are commands, variables, etc., the variables, commands are parsed out and then the final content is output.

Example 1

[Email protected] ~]$ ett=123 [[email protected] ~]$ awk ' begin {print ' $ett '} ' 123[[email protected] ~]$ awk ' begin {pri  NT "$ett"} ' $ett [[email protected] ~]$ ett= ' abc ' [[Email protected] ~]$ awk ' BEGIN {print ' $ett '} ' $ett [email protected] ~]$ awk ' begin {print ' $ett '} ' [[email protected] ~]$ awk ' begin {print ' ' $ett ' "} ' ABC

F. The role of variables in a script-defined function is only useful in functions.

Summary: Imitate the speculation system comes with the Etc/init.d/function function library script idea.

Tar zcf etc_${date +%f}_xiaoping.tar




This article is from the "Little K" blog, make sure to keep this source http://zyp88.blog.51cto.com/1481591/1607600

Shell Lesson Two learning notes

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.