Shell script programming 2

Source: Internet
Author: User
Tags define local echo command

Shell script programming 2

Custom environment variables (global variables)

Set Environment Variables

If you want to set environment variables, you must use the export command after assigning values to the variables or setting the variables, declare with the-x Option

The built-in commands can also complete the function (Note: Do not add $ before the variable name when outputting the variable)

Format:

① Export variable name = value

② Variable name = value; export variable name

③ Declare-x variable name = value

Instance:

1. Currently effective

ExportOLDBOY = "oldboy"

Echo $ OLDBOY

2. Valid for current user

Vi. bash_profile

3. Takes effect for all users

Vi/etc/profile

Source/etc/profile or./etc/profile

Display and cancel Environment Variables

Print environment variables using the echo command

$ HOME

$ UID: UID of the current user (User ID)

$ PWD the absolute path name of the current working directory

$ SHELL Current SHELL

$ USER: current USER

[Oldboy @ mode ~] $ Echo $ HOME

/Home/oldboy

[Oldboy @ mode ~] $ Echo $ UID

501

[Oldboy @ mode ~] $ Echo $ PWD

/Home/oldboy

[Oldboy @ mode ~] $ Echo $ SHELL

/Bin/bash

[Oldboy @ mode ~] $ Echo $ USER

Oldboy

Use env {printenv} or set to display default environment variables

[Oldboy @ mode ~] $

[Oldboy @ mode ~] $ Env

HOSTNAME = mode

SHELL =/bin/bash

TERM = vt100

OLD = old

History Size = 1000

LC_ALL = C

USER = oldboy

LS_COLORS = no = 00: fi = 00: di = 01; 34: ln = 01; 36: pi = 40; 33: so = 01; 35: bd = 40; 33; 01: cd = 40; 33; 01: or =

01; 05; 37; 41: mi = 01; 05; 37; 41: ex = 01; 32 :*. cmd = 01; 32 :*. exe = 01; 32 :*. com = 01; 32 :*. btm = 01; 32 :*

. Bat = 01; 32 :*. sh = 01; 32 :*. csh = 01; 32 :*. tar = 01; 31 :*. tgz = 01; 31 :*. arj = 01; 31 :*. taz = 01; 31 :*. l

Zh = 01; 31 :*. zip = 01; 31 :*. z = 01; 31 :*. Z = 01; 31 :*. gz = 01; 31 :*. bz2 = 01; 31 :*. bz = 01; 31 :*. tz = 01; 31

:*. Rpm = 01; 31 :*. cpio = 01; 31 :*. jpg = 01; 35 :*. gif = 01; 35 :*. bmp = 01; 35 :*. xbm = 01; 35 :*. xpm = 01; 35

: *. Png = 01; 35: *. tif = 01; 35:

MAIL =/var/spool/mail/oldboy

PATH =/usr/kerberos/bin:/usr/local/sersync/bin:/usr/local

/Mysql/bin:/usr/local/mysql/scripts:/usr/local/mysql:/home/oldboy/bin:/home/oldboy/bi

N

INPUTRC =/etc/inputrc

PWD =/home/oldboy

LANG = en_US.UTF-8

OLDBOY = oldboy

SHLVL = 1

HOME =/home/oldboy

LOGNAME = oldboy

CVS_RSH = ssh

LESSOPEN = |/usr/bin/lesspipe. sh % s

G_BROKEN_FILENAMES = 1

_ =/Bin/env

Or set-

[Oldboy @ mode ~] $ Id-u

501

[Oldboy @ mode ~] $ Echo $ UID

501

Use unset to remove local and Environment Variables

Echo $ USER

Unset USER

Local variable

Define local variables

Definition of common string variables

Variable name = value

Variable name = 'value'

Variable name = "value"

Output echo "a = $"

Or echo "a =$ {}"

A brief description of single quotes, double quotes, and no quotation marks is as follows:

Single quotes:

Can be said to be what you see is what you get: the content of the single quotes is output as is, or the content is described as what you see in the single quotes.

Output whatever it is.

Double quotation marks:

Output the content in double quotation marks. If the content contains commands, variables, etc., the variable is first parsed and the result is then

Output the final content

No quotation marks:

When the content is output, the string containing spaces is considered as a whole output, if the content contains commands. Variable

First, the variable and command will parse the result, and then output the final content, if the string contains special characters such as spaces

The output cannot be complete. You need to add double quotation marks. Generally, continuous strings, Data, paths, and so on can be used, but it is best to use them.

Replace double quotation marks

Example 1: Test the 'date' command with backquotes

[Root @ mode ~] # Echo ''date''

'Date'

[Root @ mode ~] # Echo "'date '"

Sat Feb 14 19:25:29 CST2015

[Root @ mode ~] # Echo 'date'

Sat Feb 14 19:25:37 CST2015

For variable reference, one is $ and the other is''

Example 2: awk calls s [root @ mode ~] # ETT = 123

[Root @ mode ~] # Awk 'in in {print "$ ETT "}'

$ ETT

[Root @ mode ~] # Awk 'in in {print '$ et '}'

123

Suggestions for custom Variables

(1). Pure numbers (without spaces). The definition method can be single or double quotation marks (for example

A. oldboyage = 33

B. NETWORKING = yes

2.There are no special cases. strings are generally defined by double quotation marks (), especially when spaces are created for multiple characters.,

A. NFSD_NODULE = "no load"

B. MyName = "Oldboy is handsome boy ."

3.When the variable content needs to be output as is, use single quotation marks ('')

A. OLDBOY_NAME = 'oldboy'

Variable naming rules

1) The variable names should be unified, with all uppercase letters and multiple words connected with "_". When referencing, it is best to use $ {APACHE}

_ ERR_NUM} Add brackets or double quotation marks ("$ {APACHE_ERR_NUM}") to reference variables:

2) avoid meaningless characters or numbers: for example, the following COUNT, do not know its exact meaning

3) global variable definitions, such as OLDBOY_HOME or OLDBOYHOME. When using the variables, use {} to enclose them.

Or "$ {OLDBOY_HOME }"

Local variable definition in the script: a variable in the script function is called a local variable.

Declaration, so that it is valid within the scope of this function, to prevent the variable name in the function from being duplicated with the variable name in the variable external program.

Program exception,

Function TestFunc ()

{

Local I

For (I = 0; I <n; I ++ ))

Do

Echo 'Do something ';

Done

}

Variable merging: it makes sense to combine some variables or configuration items, such as the file path and file name.

The combined variables are combined and assigned to a new variable, which facilitates subsequent calls and future changes to the provider.

Then;

For example, VERSION = '2. 2.22'

SOFTWARE_NAME = "httpd"

SOFTWARE_FULLNAME = "zookeeper software_name=-zookeeper version#.tar.gz"

Use a command as a variable

CMD = 'LS' wd/

Echo $ CMD

[Root @ mode ~] # Ls

Anaconda-ks.cfg index.html install. log oldboy

Grep. log index.html. 1 install. log. syslog

[Root @ mode ~] # CMD = 'LS'

[Root @ mode ~] # Echo $ CMD

Anaconda-ks.cfg grep.logindex.html index.html. 1 install. log install. log. syslog

Oldboy

Add $ before the variable name to obtain the value of this variable. The echo command can be used to display the value of the variable. The $ A and $ {A} statements are not written.

Same, but the function is the same,

3 $ {WEEK} day if the variable and other characters form a new variable, braces must be added {}

4. the habit of using double quotation marks to enclose all character creation variables will reduce many programming errors.

Common production environment applications:

1. generate different file names for websites packaged by day.

CMD = $ (date + % F)

Echo $ CMD

Echo $ (date updated successfully f2.16.tar.gz

Echo 'date interval f'.tar.gz

Tar zcvf etc _ $ (date has reached f0000.tar.gz/etc

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.