Shell script variables and parameters

Source: Internet
Author: User
Tags echo display

The variables are divided into three types:
1, ordinary variable
is the local variable. Variable is the variable name

Variable=value//= the left and right sides can not have spaces, otherwise it will be treated as commands, output a command not found

Both $variable and ${variable} represent their values.
You can use the Echo display to use unset cleanup.

Test whether the variable has been assigned
${variable:-value} If the variable is already assigned, the value of the variable is used. If the variable is empty, value is used. It should be noted that value is not assigned to variable.
${variable:=value} The difference above this expression is that if variable is empty, value is assigned to variable.

If you need to verify that the variable is empty, and if it is empty, display a system error message, use the following expression: ${variable:?}
If you need to display your own defined information, the following: ${variable:? Customed Message "}

Setting read-only variables
You can use ReadOnly to define a variable as read-only. namely ReadOnly variable
Perform readonly individually to display all read-only variables.



2, Environment variables
(1) Environment variable setting
environ-variable = value # environment variable assignment
EXPORT environ-variable # environment variable declaration
ENV # View Environment variables
unset environ-variable # Clear Environment variables


(2) Important environment variables
PATH, Home, SHELL, Pwd/oldpwd, User/uid, PPID, PS1/PS2, IFS, etc.


(3) Several environment variable configuration files

System level, system Global environment variable setting:
/etc/profile
/etc/bashrc

Normal user level, user home directory of the Private environment variable settings:
~/.bash_profile
~/.bashrc

To understand the difference between BASHRC and profile, first figure out what is an interactive shell and a non-interactive shell, and what is the login shell and non-login shell. The interactive pattern is that the shell waits for your input and executes the commands you submit. This pattern is called interactive because the shell interacts with the user.
This pattern is also familiar to most users: Log in, execute commands, and sign out. When you sign back, the shell is terminated. The shell can also run in another mode: non-interactive mode. In this mode, the shell does not interact with you, but instead reads the commands stored in the file and executes them. When it reads the end of the file, the shell terminates.

Profile and BASHRC are both used to save the user's environment information, profiles for the login shell, and BASHRC for Non-loginshell.

The specific use of the situation is as follows:
/etc/profile: This file sets the environment information for each user of the system, and the file is executed when the first user logs on. and collects the shell's settings from the/ETC/PROFILE.D directory's configuration file.

/ETC/BASHRC: This file is executed for each user running the bash shell. When the bash shell is opened, the file is read. Some Linux versions of the/ETC directory already have no BASHRC files.

~/.bash_profile Each user can use this file to enter shell information that is specific to their own use, and the file executes only once when the user logs on. By default, it sets some environment variables and then executes the user's. bashrc file.

~/.BASHRC: This file contains bash information dedicated to a user's bash shell, which is read when the user logs on and each time a new shell is opened.

In addition, the variables set in the/etc/profile (global) can be applied to any user, while the variables set in ~/.BASHRC, such as local, can only inherit the variables in/etc/profile, they are "parent-child" relationships.



3, Position variable
That is, you can use this variable to get parameters when you pass parameters to the script.
(1) General position variable
The $ $ represents the script itself
$--> $ $*/$@ is all parameters

(2) Special position variable
$? What execution status. 0 means correct, and non 0 indicates error.
$*/$@ Displays all parameter position parameters passed to the script in a single string
Number of $# position parameters
$$ Script Run Process number

parameter Read

Use a specific variable of $1. . $9 passing parameters to the script. $ #用于统计传递参数的个数.

#!/bin/sh

back_days= ' while

[-N ' $] do case ' ' in  
	-bakday '  
			echo ' Found the-bakday option "  
			echo" the parameter follow-b is $
			back_days=$2
			shift  
			;;  
		#-c)  
		#	echo "Found the-c option"  
		#	echo "The parameter follow-c is $"  
		#	shift  
		#< c19/>;;  
		*)  
			echo "is not a option"  
			;;
	ESAC  
	shift  
Done  

#用shell命令自建的选项解析, you can do according to their own ideas  
#优点: Customized, not to do, only unexpected  
#缺点: Trouble  


Run results

[XXXXXXXX ~]$./test.sh-bakday 7
Found the-bakday option
The parameter follow-b is 7
Back 7

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.