Shell programming from beginnings to proficiency - Chapter 2. shell variables

Keywords: shell Linux Zabbix Ubuntu

Shell variables


2.1. What is a Variable

In elementary school, we learned mathematical equations, such as: what is y equal to when x = 1 and y = x + 1 are known? 
In the above topics x and y are called unknowns, but in shell programming they are variable names, and 1 on the right of the equal sign and x+1 are variable contents. "=" is called assignment here.

Through the introduction above, we can conclude that variables are more and more complex content replaced by a fixed string, which contains variables, paths, strings and other content. Variables are places and markers for temporary storage of data. The stored data is stored in memory space, and the corresponding data of variables can be extracted by calling the name of variables in the content space. The greatest advantage of using variables is convenience. Of course, in addition to convenience, it is also necessary to use variables in many cases. Otherwise, it will not be able to complete the development work.

[root@Shell ~]# blog=zhang789  # Create variables 
[root@Shell ~]# echo $blog   # Output variable zhang789

Variables are divided into two types: environmental variables (global variables) and general variables (local variables).

1. Environmental variables, also known as global variables, can be used in creating their shells and any derived child process shells. Environmental variables can be divided into user-defined variables and built-in environment variables in bash. 
2. Ordinary variables, also known as local variables, can only be used in live shell scripts that create their shell functions. Half of ordinary variables are created by developers when developing programs.


2.2. Environmental variables

Environment variables are used to define the running environment of shell, ensure the correct execution of shell commands, and determine the login user name, command path, terminal type, login directory through environment variables. All environment variables are global variables of the system and can be used in all sub-processes, including editors, shell scripts and various applications.

Environment variables can be set on the command line and lost when the user exits, so it's better to define them in the. bash_profile file in the user's home directory or in the global configuration / etc/bashrc,/etc/profile file or / etc/profile.d /. Put environment variables into the above files, and the values of these variables will be initialized every time a user logs in. 
Traditionally, all environmental variables are capitalized. Before environment variables are applied to user processes, they should be exported with the export command, such as: export AS=1

Environment variables can be used to create their shells and any sub-shells or processes generated from that shell. They are often referred to as global variables to distinguish local variables. Usually, environment variables should be capitalized. Environment variables are variables that have been exported with export built-in commands. 
Some environment variables, such as HOME, PATH, SHELL, UID, USER, etc., have been set up by the / bin/login program before the user logs in. Usually environment variables are defined and stored in the. bash_profile file in the user's home directory.

You can view environment variables with the following commands

1. env (showing only global variables) 
2. set (all variables) 
3. declare (all variables, including functions, integers, and derived)

Common Documents and Differences in Setting Environmental Variables

1. Configuration of user's environment variables

[root@Shell ~]# ll /root/.bash_profile 
-rw-r--r--. 1 root root 176 12 month 29 2013 /root/.bash_profile
[root@Shell ~]# ll /root/.bashrc 
-rw-r--r--. 1 root root 176 12 month 29 2013 /root/.bashrc

2. Configuration of Global Environment Variables

[root@Shell ~]# ll /etc/profile
-rw-r--r--. 1 root root 1750 6 month 7 2013 /etc/profile
[root@Shell ~]# ll /etc/bashrc 
-rw-r--r--. 1 root root 2835 8 month 12 2015 /etc/bashrc

The content loaded by the user after login can be operated on the springboard computer, so that the user can display the prompt information of the current login USER and IP handover or login user every time the user switches.

[root@Shell ~]# cd /etc/profile.d/ 
[root@Shell profile.d]# cat hello.sh  
echo "Hello Zhanghe" 
echo "Blog:zhang789.blog.51cto.com" 
# Log out and log in again 
root@192.168.102.161's password: 
Last login: Fri Jun  9 17:32:13 2017 from 192.168.102.1
Hello Zhanghe #The information we output 
Blog:zhang789.blog.51cto.com #Author's blog address 
[root@Shell ~]#

When the user first logs in, the prompt can only be a string.

Last login: Thu Apr 2 12:15:21 2015 from 10.0.0.122 welcome to Ubuntu linux training !
$ cat /etc/motd 
welcome to Ubuntu linux training !

2.3. Defining global variables


[root@Shell ~]# export BLOG_NAME='zhang789.blog.51cto.com'
[root@Shell ~]# echo $BLOG_NAME
zhang789.blog.51cto.com
[root@Shell ~]# ZABBIX=3.2
[root@Shell ~]# export ZABBIX
[root@Shell ~]# env | grep ZABBIX
ZABBIX=3.2
[root@Shell ~]# env | grep BLOG_NAME
BLOG_NAME=zhang789.blog.51cto.com
[root@Shell ~]# export LANG=EN       #Environment variables that change languages
[root@Shell ~]# declare -x NAME=zhanghe

2.3.1. Display and Cancel Environment Variables

Setting and Canceling Environment Variables
[root@Shell ~]# export Blog=zhang789 
[root@Shell ~]# echo $Blog zhang789
[root@Shell ~]# unset Blog 
[root@Shell ~]# echo $Blog 
[root@Shell ~]#
[root@Shell ~]# unset BLOG_NAME
[root@Shell ~]# env | grep BLOG_NAME
[root@Shell ~]#

Common System Environment Variables

2.3.2. Initialization of Environmental Variables and Effective Order of Corresponding Documents

When you log in to Linux system and start a bash shell, by default, bash will look for the settings of environment variables in several files, which can be collectively called system environment files. The situation of environment variables files checked by bash depends on the way the system runs the shell. There are generally three ways the system runs the shell. 
1. sell run by default after login through system users 
2. Non-login interactive running shell 
3. Non-interactive shel ls for script execution

Login Loading Order

1. The system will first load the / etc/profile global environment variable file after login, which is the default shell main environment variable file on Linux system. Every login user on the system will load the file. 
2. When the / etc/profile file is loaded, the script file in the / etc/profile.d directory will be executed. There are many script files in this directory, such as the character set settings of the system (/etc/sysconfig/i18n). 
3. Start running $HOME/.bash_profile (User Environment Variables File), in this file, you will find $HOME/.bashrc (User Environment Variables File), if there is, then execute, if not, then execute, and if not, in the $HOME/.bashrc file, you will find / etc/bashrc (Global Environment Variables File) if there is, then execute, if not, then execute.

If the user's shell is not logged in and started (such as typing bash or other login without entering a username password or remote SSH connection), this will only load $HOME/.bashrc (user environment variable file) and find / etc/bashrc (global environment variable file)

2.4. Define local variables

Local variables are used in the user's current shell lifetime script. For example, the value of the local variable Blog is zhang789, which is meaningful only for the current shell lifetime of the user. If another process is started or exited in the shell, the local variable Blog value is invalid.

Common string variable definition:

Variable name = value
 Variable name ='value' 
Variable name = "value" command variable definition
 Variable name=$()
Variable name=``

Requirements for variable specification in shell 
It usually consists of letters, numbers and underscores, beginning with letters. 
Example 1

[root@Shell ~]# a=192.168.1.1 
[root@Shell ~]# b='192.168.1.1' 
[root@Shell ~]# c="192.168.1.1" 
[root@Shell ~]# echo "a=$a" a=192.168.1.1 
[root@Shell ~]# echo "b=$b" b=192.168.1.1 
[root@Shell ~]# echo "c=${c}" c=192.168.1.1

Example 2

[root@Shell ~]# a=192.168.1.2-$a
[root@Shell ~]# b='192.168.1.2-$a'
[root@Shell ~]# c="192.168.1.2-$a" 
[root@Shell ~]# echo "a=$a" a=192.168.1.2-192.168.1.1 
[root@Shell ~]# echo "b=$b" b=192.168.1.2-$a
[root@Shell ~]# echo "c=${c}" c=192.168.1.2-192.168.1.2-192.168.1.1

Tips:

  1. The first way to define a variable is to specify the content of the variable directly. The content is generally simple and continuous numbers, strings, paths, etc.

  2. The second way to define b variables is to define variables by single quotation marks. The feature of this method is that when you output variables, you output whatever is in quotation marks. Even if there are variables in the content, you will output the variables as they are. This method is more suitable for defining and displaying pure strings.

  3. The third way to define c variables is to define variables by double quotation marks. The characteristic of this method is that when a variable is output, the variable in quotation marks will be parsed and output the content of the variable. Instead of calling the variable name poplar in quotation marks, it is suitable for the definition of content with variables in strings. 

  4.  Habits: Numbers without quotation marks, other defaults with double quotation marks

2.5. Define variable single quotation marks and double quotation marks without quotation marks

A brief description of single quotation marks, double quotation marks and no quotation marks is as follows:

Example

[root@Shell ~]# echo 'today is date'
today is date # Single quotation marks output whatever they see 
[root@Shell ~]# echo 'today is `date`'
today is `date` # Single quotation marks output whatever they see 
[root@Shell ~]# echo "today is date" 
today is date # When double quotation marks are used, if there are variables in them, they will be parsed into concrete contents. 
[root@Shell ~]# echo "today is `date`" 
today is Wed Apr 1 15:46:18 CST 2015 # When double quotation marks are used, if there are variables in them, they will be parsed into concrete contents. 
[root@Shell ~]# echo "today is $(date)" 
today is Wed Apr 1 15:46:24 CST 2015 # When double quotation marks are used, if there are variables in them, they will be parsed into concrete contents. 
# For continuous strings and other content, it is generally not possible to add quotation marks, but double quotation marks are generally safe, recommended.

When a variable is defined, it is tested at call time

[root@Shell ~]# Blog=zhang789      #Create a variable without quotation marks 
[root@Shell ~]# echo $Blog            #--> Display the parsed content of a variable without quotation marks 
zhang789
[root@Shell ~]# echo '$Blog'      #--> Single quotation marks, showing a variable itself 
$Blog 
[root@Shell ~]# echo "$Blog"      #--> Double quotation marks, showing the contents of a variable, which can be variables, strings, etc. 
zhang789 
[root@Shell ~]# ETT=123 
[root@Shell ~]# awk 'BEGIN {print "$ETT"}' 
$ETT 
[root@Shell ~]# awk "BEGIN {print "$ETT"}" 
123 
[root@Shell ~]# awk 'BEGIN {print '$ETT'}' 
123

Suggestions for customizing common string variables 

1. When content is purely numeric (without spaces), it can be defined without quotation marks (single or double)

ZHANG=33 
HE=yes

2. Without special circumstances, strings are generally defined by double quotation marks, especially when there are spaces between multiple strings.

NFSD_MODULE="no load" MyNAME="ZHANG is a handsome boy."

3. When the variable content needs to be output as it is, use single quotation marks ("), for example:

BLOG_NAME='ZHANG'


2.7. Naming Specifications for Variables

  1. Variable naming should be unified, all capital letters should be used, statements should be clear, and the meaning of variable content should be correctly expressed. Excessive long English words can be replaced by the first few characters. Multiple words can be linked by sign links. When quoting, it is better to use double quotation marks outside the brackets or {APACHE_ERR_NUM}.

  2. Avoid meaningless characters or numbers: for example, the following COUNT does not know its exact meaning;

Example: Incorrect definition of COUNT

CONUT=`grep keywords file` 
if [ ${CONUT} -ne 22 ]
    then  echo "Do Something" 
fi

Naming global and local variables

1. Global variable definitions in scripts, such as ZHANG_HOME or ZHANGHOME, use {} to enclose variables or {ZHANG_HOME} when using variables.
Example: Operating System Function Library Script Content Global Variable Interception Example

$ cat /etc/init.d/functions

2. Definition of local variables in scripts: Variables existing in script functions are called local variables. They should be declared locally so that they are only valid in the scope of the function, so as to prevent anomalies caused by variable naming in functions and variable renaming in external programs.

2.1. Definition of variables in functions
checkpid() { 
local i 
for i in $* ; do 
    [ -d "/proc/$i" ] && return 0 
done 
return 1
}
2.2. Variable merging

When some variables or configuration items need to be combined to make sense, such as file path and file name, it is suggested that the combined variables be combined and assigned to a new variable, which is convenient for later invocation and modification.

Example: Consolidation Definition of Script Variables for Automated Installation of httpd

VERSION="2.2.22" 
SOFTWARE_NAME="httpd" 
SOFTWARE_FULLNAME="${SOFTWARE_NAME}-${VERSION}.tar.gz"
2.3. Summary of Variable Definition: Learn how to imitate the definition of scripts in / etc/init.d/function libraries that come with operating systems.
  1. 1. Variable names can only be letters, numbers, underscores and letters.

  2. 2. Normative Definition Method of Variable Name: See Name Only Meaning 
        a) ZHANGEAge=1 capitalization of the first letter of each word
        b) ZHANGE_age=1 between words "" 
        c) ZHANGEAgeSex=1 Hump Grammar: lowercase for the first word and capitalize for the rest

  3. 3, = sign knowledge, a=1 equal sign is the meaning of assignment

  4. 4. Are the comparisons equal, with a = print variable, a variable name preceded by a $symbol, and a variable name followed by a character, enclosed in braces?

  5. 5. Pay attention to the method of variable content reference. Generally, quotation marks are used. Simple continuous strings can be output without quotation marks. We hope to use single quotation marks.

  6. 6. Variable content is a command, and at this point you need to enclose variables with inverted quotes or $().

2.8. Special variables

2.8.1. Location variables

There are some special and important variables in the shell, such as: $1, $#, which we call special location variables, passing parameters from the command line, function, or script execution.

Number of Judgment Parameters
$ cat tejing4.sh 
[ $# -ne 2 ] && {
echo "muse two" 
exit 1 
}
echo $1 $2

The second script

$ cat tejing5.sh 
#no1 
if [ $# -ne 2 ]
  then
  echo "USAGE:/bin/sh $0 arg1 arg2" 
  exit 1 
fi 
#no2 
echo $1 $2

Control the number of user parameters

$ cat a.sh 
#!/bin/bash 
[ $# -ne 2 ] &&{ 
    echo "muse two" 
    exit 1
} 
echo zhang789
$ sh a.sh sa
muse two
$ sh a.sh sa ha
zhang789

* An example of the difference between @ and @

  1. Consider all command line parameters as a single string, equivalent to 13, $*;

  2. Consider each parameter on the command line as a separate string, equivalent to 1.3. This is the best way to pass parameters to other programs because it keeps all the blanks embedded in each parameter.

The difference is only in the case of double quotation marks, i.e., and @.

$ set -- "I am" handsome ansheng. # Input three parameters $ echo $# # Now there are three parameters 3
$ for i in "$*";do echo $i;done # In the case of double quotation marks, the inner content of the parameter is output as a parameter, which really symbolizes the parameter requirements we pass in, set - "I am" 
handsome ansheng. I am handsome ansheng.
$ for i in "$@";do echo $i;done # In the case of double quotation marks, each parameter is output independently 
I am
handsome
ansheng.
$ for i ;do echo $i;done # Remove the in variable list, which is equivalent to in "$@" 
I am
handsome
ansheng.
$ for i in $*;do echo $i;done # Without double quotation marks, all parameters are output, and then the first parameter "I am" is disassembled. 
$ for i in $@;do echo $i;done # Without double quotation marks, all parameters are output, and then the first parameter "I am" is disassembled.

2.8.2. Process state variables

Controlling error return values through scripts
$ cat fanhui.sh 
exit 100 
$ sh fanhui.sh 
$ echo $? 
100
Case application case: $$When there is only one script in the system and only one process can run at the same time.
#!/bin/sh pidpath=/tmp/a.pid 
if [ -f "$pidpath" ] 
    then 
        kill -USR2 `cat $pidpath` >/dev/null 2>&1
        rm -f $pidpath 
fi 
echo $$ >$pidpath 
sleep 300
$_
$ /etc/init.d/ntpd start 
Starting ntpd:                                             [  OK  ]
$ echo $_ 
start

2.9. shell variable substring

Common operations are as follows: Manbash looks for the section data "Parameter Expansion"

${#String} Returns the length of $string
${string:position}                 stay $string From the position $position Then start extracting substrings
${string:position:length}          stay $string From the position $position After that, the extraction length is $length Substring
${string#Delete the shortest matching $substring substring substring from the beginning of the variable $string
${string##Delete the longest matching $substring substring substring from the beginning of the variable $string
${string%substring}                Dependent variable $string Start deleting the shortest match at the end $substring Substring
${string%%substring}               Dependent variable $string At the end, delete the longest match $substring Substring
${parameter/pattern/string}        Use string,To replace the first match pattern
${parameter/#pattern/string} Matches patterns in string variables from the beginning and replaces matched pettern s with strings
${parameter/%patter/string}        Match from the end string In variables pattern,Just use string To replace matches pattern
${parameter//pattern/string} Use string instead of all matching patterns to find "Parameter Expansion"

2.9.1, ${# string} Gets the length of the variable string

[root@db01 ~]# OLDBOY="I am oldboy" 
[root@db01 ~]# echo $OLDBOY I am oldboy
[root@db01 ~]# echo $OLDBOY|wc -L 11 
[root@db01 ~]# echo ${#OLDBOY} 11 
[root@db01 ~]# expr length "$OLDBOY" 11

Lines with print characters less than 6

$ cat ab.sh for n in I am ansheng linux welcome to our training. 
do 
    if [ ${#n} -lt 6 ]
    then 
        echo $n 
    fi
done
$ sh ab.sh 
I 
am 
linux 
to 
our

2.9.2. Output part of the entire string

${string:position}
$ ansheng="I am ansheng" 
$ echo ${ansheng:2}
${string:position:length} 
am ansheng
$ echo ${ansheng:2:2} 
am

2.9.3,##

$ echo ${ansheng#a*c}  
ABC123ABCabc 
$ echo $ansheng 
abcABC123ABCabc 
$ echo ${ansheng##a*c}

2.9.4,%%

$ echo $ansheng 
abcABC123ABCabc
$ echo ${ansheng%a*c} 
abcABC123ABC
$ echo ${ansheng%%a*c} 
$ echo ${ansheng%C*bc} 
abcABC123AB
$ echo ${ansheng%%C*bc} 
abcAB

2.9.5, / Replacement

$ ansheng=abcABC123ABCabc 
$ echo $ansheng 
abcABC123ABCabc 
$ echo ${ansheng/abc/ansheng}
anshengABC123ABCabc 
$ echo ${ansheng/#abc/ansheng} 
anshengABC123ABCabc 
$ echo ${ansheng/%abc/ansheng}
abcABC123ABCansheng

Case: Batch renaming

$ f=stu_102999_5_finished.jpg
$ echo $f
stu_102999_5_finished.jpg
$ echo ${f/_finished//}
stu_102999_5/.jpg
$ echo ${f/_finished/} 
stu_102999_5.jpg
$ f=stu_102999_5_finished.jpg
$ echo ${f/_finished/}       
stu_102999_5.jpg
$ mv $f `echo ${f/_finished/} `
$ ls -lrt|tail -5
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_5.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_4_finished.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_3_finished.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_2_finished.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_1_finished.jpg
$ for f in `ls *fin*.jpg`;do mv $f `echo ${f/_finished/}`;done
$ ls -lrt|tail -5
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_5.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_4.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_3.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_2.jpg
-rw-r--r-- 1 root root  0 4 month  2 16:59 stu_102999_1.jpg

Summary:

# The shortest matching is the first deletion  
## The longest matching is the first deletion  
% Is the shortest match for the end deletion 
%% Is the longest match for the end deletion


Posted by szms on Sat, 22 Jun 2019 16:10:02 -0700