Use Shell variables

Copyright: Susu acridine https://blog.csdn.net/weixin_44774638/article/details/91489182

Question
in this case require familiarity with the use of Shell variables, the main practice or verify the following:
1) Definition / assignment / view variables
2) application environment / Predefined / position variables
in addition to the establishment of reference variables and learn, but also understand the environment variable PWD , USER, HOME, SHELL, as well as predefined variables 0 0、 ,?Katex the parse error: the Expected 'EOF', GOT '#' AT position 1: #,*, and the position variables $ 1, $ 2, $ 10, ...... effect.
Steps
to achieve this case need to follow the steps below.

Step One: Define the variables / Assignment / View

1) New / assignment variable
New variable SCHOOL, assignment "Tarena IT GROUP", you can check the variable is set by the set command:

[root@svr5 ~]# SCHOOL="Tarena IT"
[root@svr5 ~]# set | grep SCHOOL
SCHOOL='Tarena IT'

2) Check variable
by variable names echo $ output variable values:

[root@svr5 ~]# echo $SCHOOL
Tarena IT

When viewing variables, if the variable name string to be outputted later connected together, {} should enclose the variable name to distinguish:

[root@svr5 ~]# echo $SCHOOLGroup  			//无法识别变量名SCHOOL

[root@svr5 ~]# echo ${SCHOOL}Group  		//区分后可以识别
Tarena ITGroup

3) revocation of custom variables
to revoke an existing variable, you can use the unset command:

[root@svr5 ~]# unset SCHOOL  				//撤销变量SCHOOL
[root@svr5 ~]# echo $SCHOOL  				//查看时已无结果

[root@svr5 ~]#

Step 2: Using the environment variable

1) Check the environment variables associated file
Global file is / etc / profile, valid for all users; user file ~ / .bash_profile, valid only for the specified user.
Check / etc / profile file contents:

[root@svr5 ~]# cat /etc/profile
.. ..
if [ -x /usr/bin/id ]; then
        USER="`id -un`"
        LOGNAME=$USER
        MAIL="/var/spool/mail/$USER"
fi
HOSTNAME=`/bin/hostname`
HISTSIZE=1000
.. ..
export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC
.. ..
**查看~/.bash_profile文件内容:**
[root@svr5 ~]# cat ~/.bash_profile | grep -v ^$
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin
export PATH
unset USERNAME

2) use environment variables
of the current user environment variable USER records the user name, LOGNAME record the login name, HOME record the host directory, SHELL record login Shell, HOSTNAME host name record, TERM record terminal type:

[root@svr5 ~]# echo $USER $LOGNAME $HOME $SHELL
root root /root /bin/bash
[root@svr5 ~]# echo $HOSTNAME
svr5.tarena.com
[root@svr5 ~]# echo $TERM
xterm

Path environment variable PWD record is currently located, and OLDPWD recorded before the path where the last execution of cd:

[root@svr5 ~]# cd /boot/grub/
[root@svr5 grub]# echo $PWD $OLDPWD
/boot/grub /root  							//前一次工作目录为/root
[root@svr5 grub]# cd -  						//切换到前一次的工作目录
/root 										//正确切回/root
[root@svr5 ~]# echo $PWD $OLDPWD
/root /boot/grub  							//此时OLDPWD自动设为/boot/grub
[root@svr5 ~]# OLDPWD=/usr/src  				//手动修改OLDPWD值
[root@svr5 ~]# echo $PWD $OLDPWD  				//确认修改结果
/root /usr/src
[root@svr5 ~]# cd -  							//再次切换前一次目录
/usr/src
[root@svr5 src]# pwd  				//实际进入修改后的/usr/src,而不是/boot/grub
/usr/src
56
环境变量PS1表示Shell环境的一级提示符,即命令行提示符(\u 用户名、\h 主机名、\W 工作目录、\$ 权限标识):
[root@svr5 src]# echo $PS1  				//查看默认的一级提示
[\u@\h \W]\$
[root@svr5 src]#PS1='bash-3.2\$'  		//修改一级提示
bash-3.2#  								//更改结果
bash-3.2#PS1='[\u@\h \W]\$ '  			//恢复原有设置
[root@svr5 src]# 
环境变量PS2表示二级提示符,出现在强制换行、at任务编辑等场合:
[root@svr5 ~]# echo $PS2  				//查看默认的二级提示
>
[root@svr5 src]# cd \ 					//强制换行,观察提示符效果
> /root/
[root@svr5 ~]# PS2='=> '  				//手动修改二级提示
[root@svr5 ~]# cd \  					//再次验证提示符效果
=> ~
[root@svr5 ~]# PS2='> ' 					//恢复原有设置

3) Check the system variables
using env can view all environment variables:

[root@svr5 src]# env
HOSTNAME=svr5.tarena.com
TERM=xterm
SHELL=/bin/bash
HISTSIZE=1000
SSH_CLIENT=192.168.4.110 59026 22
OLDPWD=/root
SSH_TTY=/dev/pts/0
USER=root
.. ..
HOME=/root
LOGNAME=root
CVS_RSH=ssh
SSH_CONNECTION=192.168.4.110 59026 192.168.4.4 22
LESSOPEN=|/usr/bin/lesspipe.sh %s
DISPLAY=localhost:10.0
G_BROKEN_FILENAMES=1
_=/bin/env
使用set可查看所有变量(包括env能看到的环境变量):
[root@svr5 src]# set
BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
.. ..
对比env、set列出的结果数量:
[root@svr5 src]# env | wc -l
24
[root@svr5 src]# set | wc -l

**

Step 3: Use the location variable

**
1) Create a test script to show.

[root@svr5 ~]# vim location.sh
#!/bin/bash
echo "您输入的第2、11个参数分别是:$2、${11}"
[root@svr5 ~]# chmod +x location.sh  			//添加可执行权限

2) execute the script location.sh , test results.

[root@svr5 ~]# ./location.sh one two three
您输入的第2、11个参数分别是:two、  			//$2为two,$11为空
[root@svr5 ~]# ./location.sh a b c d e f g h i j k l m n
您输入的第2、11个参数分别是:b、k  				//$2为b,$11为k

3) using the location parameter provides two integers, sum (operation Detailed knowledge of subsequent chapters)
to create a script file summation:

[root@svr5 ~]# cat sum2int.sh
#/bin/bash
echo $[$1+$2]
[root@svr5 ~]# chmod +x sum2int.sh  			//添加可执行权限
测试执行结果:
[root@svr5 ~]# ./sum2int.sh 12  				//参数不够或异常时报错
./sum2int.sh: line 2: 12+: syntax error: operand expected (error token is "+")
[root@svr5 ~]# ./sum2int.sh 12 34  				//输出12+34的结果
46
[root@svr5 ~]# ./sum2int.sh 123 456  			//输出123+456的结果
579

Step 4: Use predefined variables

1) $$ variables available currently in the process of the PID number.

[root@svr5 ~]# echo $$
9755
[root@svr5 ~]# ps -p 9755  				//检查对应PID的进程
  PID TTY          TIME CMD
 9755 pts/0    00:00:00 bash  			//当前处于bash解释器环境中

2) variable $! Will record the most recent background process PID number.

[root@svr5 ~]# sleep 300 &  				//创建一个后台测试进程(睡眠300秒)
[1] 10710
[root@svr5 ~]# echo $!
10710

3) variable $? Return value of the previous command can be used to identify whether to perform properly.

[root@svr5 ~]# ls -lh /etc/fstab  		//正常执行一条命令行
-rw-r--r-- 1 root root 733 10-09 15:34 /etc/fstab
[root@svr5 ~]# echo $?  			//检查 $? 的值,为0说明前一条命令成功
0
[root@svr5 ~]# ls -lh /etcfstab  		//故意写错一条命令
ls: /etcfstab: 没有那个文件或目录
[root@svr5 ~]# echo $?  			//检查 $? 的值,非0说明前一条命令异常/失败/
2
4)修改测试脚本location.sh,添加对“位置变量相关的预定义变量”的测试。
编辑脚本内容:
[root@svr5 ~]# cat location.sh
#!/bin/bash
echo "您输入的第2、11个参数分别是:$2、${11}"
echo "当前执行的脚本名是:$0  					//$0 记录脚本名称
您一共输入了 $# 个位置参数  						//$# 记录位置参数个数
它们是:$* "  								//$* 记录所有位置参数的内容
执行脚本测试:
[root@svr5 ~]# ./location.sh one tow 3nd 4th E F g h i j k
您输入的第2、11个参数分别是:tow、k
当前执行的脚本名是:./location.sh
您一共输入了 11 个位置参数
它们是:one tow 3nd 4th E F g h i j k

Guess you like

Origin blog.csdn.net/weixin_44774638/article/details/91489182