shell entry - variable

The definition of variables

I.e. run the process variable it is allowed to change the value of the amount of the
variable character string is fixed to designate a method for non-fixed value
variable is a convenient placeholder for the reference computer memory address the address can be changed Script runtime store program information
in the shell variables are not permanently stored in the system, it must be declared in the document

In the category of variables shell script

In the shell variables into the environment-level variables, user-level variables, system variables
environment-level variables only take effect in the current shell, shell closes the tag is missing
the user level variables are written in the user's bone file, only take effect for the current user
system level variables is written in the system configuration file / etc / profile or /etc/profile.d/, for all users to take effect

The method defined variables in shell scripts

环境级
	export A=1
用户级
	vim ~/bash_profile
 	export A=1
系统级
	vim /etc/profile
	export A=1

Environmental level only take effect in the current environment
the user level is set to take effect for the user, other users do not take effect
global variables for all users and the environment take effect

In the user's home directory
ls -a
Here Insert Picture Description

.bash.rc		是用户的shell环境变量
.bah_profile		用户变量
在.bashrc文件中写入
export a=1

Here Insert Picture Description
Here Insert Picture Description

Visualize su and su - the difference between
Here Insert Picture Description
the FIG. Analysis:
user shell environment variable added to a = 1
outputs a may output
Su student switched to the student user
may output a, because su to be non-interactive handover mode, only changes the user identity, but did not change the environment, at this time his environment or root shell environment
Su - not just switch users to switch environment, because the value is not a student of the user's environment

Specification of variable names

Variable names often contain uppercase and lowercase letters letters, numbers, underscores (not required)

Variable Name Format

	WESTOS_LINUX
	Westos_Linux
	westoS_Linux

Translation and declare a variable characters

character effect
\ Translation of a single character
" " Weak reference, batch translation of "character" that appear,
’ ’ Strong reference, batch translation '' character that appears in
' 'versus" " The difference is that, "" can not be translated, "", "` ","! "," $ "
${} Variable declaration
例如:
	A=1
	echo $Ab
	echo ${A}b

Here Insert Picture Description

Variable values ​​are passed

character effect
$0 Name of the script
$1 After the first script substrings
$2 After a second script substrings
$3 Third string string script
$# After the script with the number of strings brother
$* Script followed all strings, the pattern is "123"
&@ Script heel all strings mode is "1", "2", "3"
$$ pid process

Experiments show
to write a script, as shown below
Here Insert Picture Description
Here Insert Picture Description

As can be seen 0 Yes foot this name Call again Look 0 is the name of the script look $ role
Here Insert Picture Description
Here Insert Picture Description

Implement variable-transmission read

read 			WESTOS
read -s  		WESTOS
read -p "input: "	WESTOS

Here Insert Picture Description
Here Insert Picture Description

-S input parameter not echo
Here Insert Picture Description
Here Insert Picture Description
for more intuitive result, a blank line is added
Here Insert Picture Description
Here Insert Picture Description

linux system setting command aliases

alias xie='vim'
vim ~/.bashrc		编辑用户级别名
	alias xie='vim'
vim  /etc/bashrc		全局别名
	alias xie='vim'
unalias xie			取消别名(先在文件中删除)

Here Insert Picture Description
Here Insert Picture Description

Variable references
in two ways

   $()		适用于shell中
  ` `	适用范围更广

Here Insert Picture Description
Write a perl script (temporarily pipe this script is to demonstrate the difference between the two)
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description
Here Insert Picture Description

Set the variable with the command execution results

Hostname=$(hostname)
Hostname=`hostname`
$?
	$?是命令在执行完成之后产生的退出值
	范围是[0-255]
	当$0=0时标示命令执行没有错误输出
	这个值可以用exit 命令执行.
	例如 exit 66

Here Insert Picture Description
Here Insert Picture Description

Function in the script
when the script function to a complex block of statements defined as a method of string

Host_Message()
{
		read -p "Please input you action: " Action
		[ "Action"  ==  "exit" ] && exit 0
		[ "Action"  == "user"]&&echo You are $USER
		[ "Action" == "hostname" ]&& echo $HOST
		Host_Message
}
Host_Message

Guess you like

Origin blog.csdn.net/zhaoliang_Guo/article/details/91791861