Use of Linux Shell Variables

Source: Internet
Author: User
Tags echo command

I. Use of local variables

Local variables act only in this shell, just like local variables. It does not affect variables in other shells.

1. Show variable values
You can use the echo command. Note that you must add the $ symbol before the variable name when using the variable.

2. Clear variable
Use the unset command to clear the command.

3. Show all local shell Variables
Some environment variables are pre-defined in shell. To display these commands, you can use the set command.

 

In addition, locally defined variables are also displayed.

4. Test whether the variable has been assigned a value
$ {Variable:-value}
If the variable has been assigned a value, the variable value is used. If the variable is null, value is used. Note that value is not assigned to variable.
$ {Variable: = value}
The difference above this expression is that if variable is null, value is assigned to variable.

If you want to check whether the variable is null, and if it is null, the system error message is displayed, the following expression is used:
$ {Variable :?}
If you need to display your own defined information, the information is as follows:
$ {Variable :? "Customed message "}

5. Set Read-Only variables
You can use readonly to define a variable as read-only.
Readonly variable
Execute readonly to display all read-only variables.

 

 

 

II. Environment Variables

 

Environment variables are used for all user processes. In Linux, the login process is called the parent process, and the user programs executed in shell are called Child processes. Environment variables can be set in the command line, but these values will be lost when the user logs out. Therefore, it is best to define it in. profile under the $ home/directory. Traditionally, all environment variables are in uppercase. Before applying environment variables to user processes, you must use the Export command to export them. The setting method is the same as that of the local variable.

1. Show Environment Variables
Use the Env command to view all environment variables

 

2. Dive into shell Variables
Exinit saves the initialization options in the VI editor. For example, to display the row number, it is as follows:
Exinit = 'set nu '; export exinit
Home: used to record the user's home directory;
Ifs: Used as the default domain separator specified by shell.
Path: stores the directory order for command or script search. You can set it in. profile.

3. Export variables to sub-Processes
Use the Export command in the parent script to export the variable so that the script can know the value of the variable.

 

 

 

Iii. location variable parameters

When passing a parameter to a script, you can use this location variable to obtain the parameter.
They are:
$0: Script Name. This variable contains the address. You can use basename $0 to obtain the Script Name.
$1: The first parameter
$2, $3, $4, $5,... and so on.

 

 

 

Iv. Specific variable parameters

Control Information about script running
$ # Number of parameters passed to the script
$ * Display all parameters passed to the script with a single string
$ ID of the script run
$! ID of the last process running in the background
$ @ Is the same as $ #, but it is enclosed by quotation marks and each parameter is returned in quotation marks.
$-Display the current options used by shell.
$? Displays the launch status of the last command. 0 indicates no error.

 

After starting the Java Process with shell, obtain the PID of the process. Save the PID to a file. When it is disabled, read the file and obtain the PID and kill PID.

 

#! /Bin/sh </P> <p> Server =/home/Java/server <br/> Cd $ server </P> <p> case "$1" in </P> <p> start) <br/> nohup Java-xmx128m-jar server. jar> $ server/server. log 2> & 1 & <br/> echo $! > $ Server/server. PID <br/>; </P> <p> stop) <br/> kill 'cat $ server/server. PID '<br/> RM-RF $ server/server. PID <br/>; </P> <p> restart) <br/> $0 stop <br/> sleep 1 <br/> $0 start <br/>; </P> <p> *) <br/> echo "Usage: Run. sh {START | stop | restart} "<br/>; </P> <p> esac </P> <p> exit 0 

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.