Chapter 6 of linuxshell programming guide ------ environment and shell variables

Source: Internet
Author: User
To make shell programming more effective, the system provides some shell variables. Shell variables can save variable names such as pathnames, file names, or numbers. Shell regards any settings as text strings. There are two variables: local and to make s h e l programming more effective, the system provides some s h e l variables. S h e l variables can be saved, such as path names and files.
Name or a variable name such as a number. S h e l considers any settings as text strings.
There are two types of variables: local and environment. Strictly speaking, there can be four types, but the other two types are read-only and can be considered as special

Variable used to pass parameters to the s h e l script.

Use the e c h o command to display the values of a single variable and add $ Before the variable name. for example:

[Root @ localhost huangcd] # great_picture = "die hard"
[Root @ localhost huangcd] # echo $ {great_picture }}
Die hard}
[Root @ localhost huangcd] # echo $ {great_picture}
Die hard

[Root @ localhost huangcd] # last_file1_ OK .txt
[Root @ localhost huangcd] # echo $ {last_file}
OK .txt

Run the u n s e t command to clear the variable.

[Root @ localhost huangcd] # pc = enterprise
[Root @ localhost huangcd] # echo 4 {pc}
4 {pc}
[Root @ localhost huangcd] # echo $ {pc}
Enterprise
[Root @ localhost huangcd] # unset pc
[Root @ localhost huangcd] # echo 4 {pc}
4 {pc}
[Root @ localhost huangcd] # echo $ {pc}


Use the s e t command to display all locally defined s h e l variables.

[Root @ localhost huangcd] # set
BASH =/bin/bash
BASH_ARGC = ()
BASH_ARGV = ()
BASH_LINENO = ()
BASH_SOURCE = ()
BASH_VERSINFO = ([0] = "3" [1] = "2" [2] = "25" [3] = "1" [4] = "release" [5] = "i386-redhat-linux-gnu ")
BASH_VERSION = '3. 2.25 (1)-release'
COLORS =/etc/DIR_COLORS.xterm

Adding variables side by side can combine variables:

[Root @ localhost huangcd] # first = "bruce"
[Root @ localhost huangcd] # sum = willis
[Root @ localhost huangcd] # echo 4 {first }$ {sum}
4 {first} willis
[Root @ localhost huangcd] # echo $ {first }$ {sum}
Brucewillis

Sometimes you need to test whether a variable has been set or initialized. If it is not set or initialized, you can use another value. This life
Order format:
$ {V a r I a B l e:-v a l u e}
That is, if a variable value is set, use it. if no value is set, the new value is obtained. For example:

[Root @ localhost huangcd] # color = blue
[Root @ localhost huangcd] # echo "the sky is $ {color:-gray }"
The sky is blue
[Root @ localhost huangcd] # unset color
[Root @ localhost huangcd] # echo "the sky is $ {color:-gray }"
The sky is gray

You can use variables to save the replacement information of system command parameters. The following example uses a variable to save the file copy file name
Information. The variable s o u r c e stores the path of the p a s w d file, and d e s t stores the object in the c p command.

[Root @ localhost huangcd] # source = "/etc/passwd"
[Root @ localhost huangcd] # dest = "/tmp/passwd. bak"
[Root @ localhost huangcd] # cp $ {source }$ {dest}

If you do not want to change the value of a variable, you can set it to read-only. If someone includes the user himself
If you want to change it, an error message is returned. The format is as follows:
V a r I a B l e-n a m e = v a l u e
Readonly variable-name

In the following example, set the variable to the path of one of the system's tape devices and set it to read-only. any operation that changes its value will return an error message.

[Root @ localhost huangcd] # tape_dev = "/dev/rmt/0n"
[Root @ localhost huangcd] # echo $ {tape_dev}
/Dev/rmt/0n
[Root @ localhost huangcd] # readonly tape_dev
[Root @ localhost huangcd] # tape_dev = "/dev/rmt/ln"
Bash: tape_dev: readonly variable

To view all read-only variables, run the r e a d o n l y command.

[Root @ localhost huangcd] # readonly
Declare-ar BASH_VERSINFO = '([0] = "3" [1] = "2" [2] = "25" [3] = "1" [4] =" release "[5] =" i386-redhat-linux-gnu ")'
Declare-ir EUID = "0"
Declare-ir PPID = "4931"
Declare-r SHELLOPTS = "braceexpand: emacs: hashall: histexpand: history: interactive-comments: monitor"
Declare-ir UID = "0"
Declare-r tape_dev = "/dev/rmt/0n"

Environment variables are used for all user processes (often referred to as subprocesses ). A logon process is called a parent process. Execution in s h e l
User processes are called sub-processes. Unlike local variables (only for current s h e l), environment variables can be used for all sub-processes.
Includes editors, scripts, and applications.
Environment variables can be set in the command line, but these values will be lost when the user logs out, so it is best to be in the. p r o f I l e file
Definition. The system administrator may have set some environment variables in the/e t c/p r o f I l e file. Put it in the p r o f I l e file
It means that these values will be initialized each time you log on.
Traditionally, all environment variables are capitalized. Before applying environment variables to user processes, you must use the e x p o r t command to export them.
Environment variables are set in the same way as local variables.

V a r I a B L E-N A M E = v a l u e; export VARIABLE-NAME
There is a semicolon between two commands, which can also be written as follows:
V a r I a B L E-N A M E = v a l u e
Export VARIABLE-NAME

The display environment variable is the same as the display local variable. The example is as follows:

[Root @ localhost huangcd] # console = tty1
[Root @ localhost huangcd] # export console
[Root @ localhost huangcd] # echo $ console
Tty1

Run the e n v command to view all environment variables.

[Root @ localhost huangcd] # env
SSH_AGENT_PID = 4599
HOSTNAME = localhost. localdomain
Export top_startup_id =
TERM = xterm
SHELL =/bin/bash
History size = 1000

Run the u n s e t command to clear environment variables:

[Root @ localhost huangcd] # echo $ console
Tty1
[Root @ localhost huangcd] # unset console
[Root @ localhost huangcd] # echo $ console


Brourne shell has some reserved environment variable names that cannot be used for other purposes. Generally
These embedded environment variables are created in/e t c/p r o f I l e, but not completely, depending on the user.

The following are some common environment variables:

[Root @ localhost huangcd] # echo $ HOME
/Root
[Root @ localhost huangcd] # echo $ IFS


[Root @ localhost huangcd] # echo $ LOGNAME
Root
[Root @ localhost huangcd] # echo $ MAILPATH


[Root @ localhost huangcd] # echo $ SHELL
/Bin/bash

There are also some reserved environment variables. Other systems or command line applications will use them. Below are some of the most commonly used,
Note that no default settings are available for these values. the description must be displayed.

1. EDITO R
Set editor, the most common.
$ EDITOR = vi; export EDITOR
2. PWD
The path name of the current directory. use the c d command to set this option.
3. PA G E R
Save the screen flip command, such as p g and m o r e. This function is used when you view m a n text.
$ PAGER = 'PG-f-p %; d' export PAGER
4. MANPAT H

Save the m a n text directory on the system. Directories are separated by colons.
$ MANPATH =/usr/apps/man:/usr/local;/emxapnort MANPATH
5. LPDEST or P R I N T E R
Save the default printer name, which is used to specify the printer name when printing a job.
$ LPDEST = hp3si-systems

At the beginning of this chapter, four variables are mentioned: local, environment, and two other variables are considered special variables because they are
Read. These two variables are location variables and specific variable parameters. Let's take a look at the location variable.
If you want to pass information to an s h e l script, you can use the location parameter to complete this function. Number of parameters
This number can be any more, but only the first nine can be accessed. you can use the s h I f t command to change this limit. After this book
This section describes the s h I f t command. The parameter starts from the first one and ends at 9th. the $ symbol must be added before each access parameter. First
The parameter is 0, indicating that the actual script name is reserved for storage. This value can be used regardless of whether the script has parameters.
If You send Did You See Th e Full Mo o n information to the script, the following table describes how to access each parameter
Number.
$0 $1 $2 $3 $4 $5 $6 $7 $8 $9
Script name: Did Yo u S e T h e u l M o n

[Root @ localhost huangcd] # cat param
#! /Bin/bash
# Param
Echo "This is the script name: $0"
Echo "This is the script name: $1"
Echo "This is the script name: $2"
Echo "This is the script name: $3"
Echo "This is the script name: $4"
[Root @ localhost huangcd] #./param did you see the full moon
This is the script name:./param
This is the script name: did
This is the script name: you
This is the script name: see
This is the script name:

[Root @ localhost huangcd] # cat param2
#! /Bin/bash
Echo "Hello world this is 'basename $ 0' calling"
[Root @ localhost huangcd] #./param2
Hello world this is param2 calling

You can pass parameters to system commands in a script. In the following example, in the f I n d command, use the $1 parameter to specify
Find the file name.

[Root @ localhost huangcd] # cat findfile
#! /Bin/bash
# Find
Find/-name $1-print
[Root @ localhost huangcd] # sh findfile passwd
/Usr/lib/news/bin/auth/passwd
/Usr/share/doc/nss_ldap-253/pam. d/passwd
/Usr/bin/passwd
/Etc/passwd
/Etc/pam. d/passwd

Now that you know how to access and use parameters in the s h e l script, it is useful to know more about the parameters,
It is necessary to know some related control information during script running, which is the origin of specific variables. There are a total of 7 specific variables, see
Table 1 4-2.


[Root @ localhost huangcd] # cat param
#! /Bin/bash
# Param
Echo "This is the script name: $0"
Echo "This is the script name: $1"
Echo "This is the script name: $2"
Echo "This is the script name: $3"
Echo "This is the script name: $4"
Echo "the number of param is: $ #"
Echo "show all arguments: $ *"
[Root @ localhost huangcd] # sh param today is very blue
This is the script name: param
This is the script name: today
This is the script name: is
This is the script name: very
This is the script name: blue
The number of param is: 4
Show all arguments: today is very blue

Note: $? Returns 0. You can return this variable in any command or script to obtain the returned information. Based on this information, you can
For further research in the script, returning 0 means success, and 1 indicates an error.
In the following example, copy the file to/t m p and use $? Check results.

[Root @ localhost huangcd] # cp OK .txt/tmp
[Root @ localhost huangcd] # echo $?
0

Now try to export a file to a non-existent Directory:

[Root @ localhost huangcd] # cp OK .txt/usr/local/apps/dsf
Cp: The Common File "/usr/local/apps/dsf" cannot be created: the file or directory does not exist.
[Root @ localhost huangcd] # echo $?
1

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.