Shell Programming Specifications and Variables

Preface

In some complex Linux maintenance tasks, a large number of repetitive inputs and interactions are time-consuming and error-prone.
Writing a proper shell script program can accomplish a series of maintenance tasks in batch and automate, greatly reducing the burden of administrators.

1. An overview of Shell scripts

1. What is a Shell?

The shell is a command interpreter, which is at the outermost level of the operating system. It is responsible for directly talking to the user, interpreting the user's input to the operating system, and processing the output of a wide variety of operating systems, and outputting to the screen for feedback to the user.This kind of dialogue can be interactive or non-interactive. The command computer that we enter is not recognized. At this time, we need a program to help us translate, become a binary program that the computer can recognize, and return the results generated by the computer to us.

2.Shell's role

Common shell_There are many interpreter programs, and when using different shells, there are some differences due to internal commands, command line prompts, and so on.The / etc/shells file lets you know what kind of shell scripts are supported by the current system

[root@c7-1 ~]# cat /etc/shells 
/bin/sh         //Is a soft link to the bash command (replaced by / bin/baah)
/bin/bash		//Shell based on E sent under GNU cabbage

/sbin/nologin   
#nologin: Strange shell that prevents users from logging on to the host.Bash (/bin/bash) is the default shell currently used by most Linux versions.

/usr/bin/sh   	//Has been replaced by bash
/usr/bin/bash	//centos and redhat systems use bash shell by default

/bin/tcsh		//An enhanced version of csh, fully compatible with csh, integrates CSH to provide more functionality.
/bin/csh		//Replaced by / bin/bash (integrated C shell, more features)
[root@c7-1 ~]# 

2.1 Why do legitimate shells on the system write to/etc/shells?

This is because some services of the system check shells that the user can use while running, and the queries for these shells are based on the file / etc/shells

2.2 When can a user get a shell to work?Which shell will you get?

When I log in, the system will give me a shell to work with, and the shell obtained by this login is recorded in the file / etc/passwd.

2.3 User's login Shell

Different shells have different functions. The shell also determines that the default shell in Linux is/bin/bash. The popular shells are ash, bash, ksh, csh, zsh, etc. Different shells have their own characteristics and uses.
Most linux systems currently use the bash shell by default, and the default login shell is / bin/bash, which you can see in the / etc/passwd file
This shell is for the user to see which shell is used for the last field in / etc/passwd and can be reassigned using chnod-s or chsh-s if you want to modify it

3. What is a shell script

  • The commands that need to be executed are saved to a file and executed sequentially. It does not need to be compiled. It is interpreted

A shell script means that when we put the original linux commands or statements in a file and execute them through this program file, we call it a shell script or shell program.We can enter a system of commands and related combinations of grammar and sentences in a script, such as variables, process control statements, etc., and combine them to form a powerful shell script

4. What Shell scripts can do

  • Automatically complete installation of software, such as installing and deploying LAMP Architecture Services
  • Automatically complete system management, such as adding users in bulk
  • Automatically complete backups, such as database backups
  • Automated analysis and processing, such as site visits

5.Shell script usage scenarios

  • Repeated operation
  • interactive tasks
  • Bulk transaction
  • Service Running Status Monitoring
  • Timed Task Execution

When a lot of complex and repetitive work needs to be done, instead of repeating commands on the command line, running shell scripts directly saves time and improves efficiency

2. Write Shell scripts

1. Composition of shell scripts

 #!/bin/bash
#First Act"#!/Bin/bash ", as indicated in the script (default interpreter); the code statements below this line are executed through the / bin/bash program.


There are other types of interpreters:
#!/ysr/bin/bash
#!/usr/bin/expect

annotation:with"#"The beginning statement is expressed as comment information, and the commented statement is not executed when the script is run

2. Write script code

  • Using vim text editor
  • One Linux command per line, written in sequence for execution
[root@c7-1 home]# vim test.sh

#!/bin/bash  
cd /boot		//Switch to boot directory
pwd				//View Current Directory
ls -lh vml*		//View all vml (kernel) files

3.Shell script execution

3.1 Method 1: Execute script under current path (absolute path vs. relative path) (with execute privileges)

[root@c7-1 home]# ./test.sh
-bash: ./test.sh: insufficient privilege
[root@c7-1 /]# /home/test.sh
-bash: /home/test.sh: insufficient privilege
[root@c7-1 home]# ls -lh
-rw-r--r--.  1 root root  38 9 February 18:56 test.sh
#Cannot execute without permission to test.sh file

[root@c7-1 /]# Chmod +x/home/test.sh //grant test.sh executable permissions
[root@c7-1 home]# ls -lh
-rwxr-xr-x.  1 root root  38 9 February 18:56 test.sh

[root@c7-1 home]# ./test.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64
[root@c7-1 /]# /home/test.sh
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64
#Permission is granted before it can be executed

3.2 Method 2: sh, bash script file path (this way you can not add execute permissions to the script file)

[root@c7-1 home]# ls -lh
-rw-r--r--.  1 root root  38 9 February 18:56 test.s

[root@c7-1 home]# bash test.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

[root@c7-1 home]# sh test.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

3.3 Method 3: Sorce script file path (may not have permission to execute)

[root@c7-1 home]# source test.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

3.4 Method 4: Other Methods

[root@c7-1 /]# sh < /home/test.sh 
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

[root@c7-1 /]# cat /home/test.sh | sh
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

[root@c7-1 /]# cat /home/test.sh | bash
/boot
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

4. Better script composition

[root@c7-1 /]# vim /home/test.sh 

#!/bin/bash

#hellow
cd /boot

echo "The current directory is located in:"
pwd

echo "Where vml The starting files include:"
ls -lh vml*


[root@c7-1 /]# bash /home/test.sh 
The current directory is located in:
/boot
 Where vml The starting files include:
-rwxr-xr-x. 1 root root 5.7M 7 February 2201:24 vmlinuz-0-rescue-88592b0b2e2b49fab4e71eedc23b1998
-rwxr-xr-x. 1 root root 5.7M 8 February 23, 2017 vmlinuz-3.10.0-693.el7.x86_64

3. Redirection and Pipeline Operation

1. Interactive hardware devices

  • Standard Input: Receive user input data from the device
  • Standard Output: Output data to the user through the device
  • Standard error: Error information is reported by the device
typeDevice FilesFile Description NumberDefault device
Standard Input/dev/stdin0keyboard
standard output/dev/stdout1Monitor
Standard error output/dev/stderr2Monitor

2. Redirection operation

  • Redirect input:
[root@c7-1 home]# vim passwd.txt 
12345
[root@c7-1 home]# Passwd --stdin zhangsan < passwd.txt //Redirect the data 12345 of passwd.txt to the passwd zhangsan user, i.e. change the zhangsan password to 12345
 Change User zhangsan Password.
passwd: All authentication tokens have been successfully updated

  • Redirect output:
[root@c7-1 home]# Echo "123456" > passwd.txt //redirect output, enter 123456 into passwd.txt
[root@c7-1 home]# cat passwd.txt 
123456

[root@c7-1 home]# cat passwd.txt 
12345
[root@c7-1 home]# Echo "123456789" > passwd.txt //append 123456789 to the tail of passwd.txt
[root@c7-1 home]# cat passwd.txt 
12345
123456789
  • Standard error output
[root@c7-1 home]# ls /tmp/ xxx 2>1.txt
[root@c7-1 home]# cat 1.txt 
ls: cannot access xxx: No file or directory

[root@c7-1 home]# ls /tmp/ www 2>>1.txt
[root@c7-1 home]# cat 1.txt 
ls: cannot access xxx: No file or directory
ls: cannot access www: No file or directory
#Error message found in two lines
  • Mixed Output
[root@c7-1 home]# ls /etc/passwd xxx &>> 1.txt
[root@c7-1 home]# cat 1.txt 
ls: cannot access xxx: No file or directory
/etc/passwd
  • &>and>&Symbols
&To express the same meaning
1>&2 Redirect Standard Output to Standard Error
2>&1 Redirect standard errors to standard output

[root@c7-1 home]# ls /etc/passwd xxx > 1.txt 2>&1
[root@c7-1 home]# cat 1.txt 
ls: cannot access xxx: No file or directory
/etc/passwd
#Redirect standard errors from 1.txt to 1.txt

[root@c7-1 home]# ls /etc/passwd xxx 2> 1.txt 1>&2
[root@c7-1 home]# cat 1.txt 
ls: cannot access xxx: No file or directory
/etc/passwd
#Output standard error to 1.txt and redirect 1.txt standard output to standard error
  • /dev/null (black hole)

Think of it as a "black hole" and everything written to it will be lost forever. Try to read from it and nothing will be read.However/dev/null is very useful on both command line and script

[root@c7-1 home]# echo "123456" > /dev/null 
[root@c7-1 home]# cat /dev/null 
[root@c7-1 home]# 
#Nothing to See

3. Pipeline operation symbol'|'

  • Output the left command as the processing object for the right command
[root@c7-1 home]# grep "/bin/bash$" /etc/passwd | awk -F: '{print $1,$7}'
root /bin/bash
lz /bin/bash
zhangsan /bin/bash
#Retrieve first and seventh line users of call/bin/bash

[root@c7-1 home]# df -Th | grep "/$" | awk '{print $6}'
19%

Redirection and pipeline operations are very common functions in shell environments. Skilled and flexible use of them will help you write powerful shell scripts with concise groups of code

IV. Variables of Shell

1. The role of variables

  • Used to store specific parameters (values) that the system and users need to use

    Variable name: Use a fixed name, defined by the system or user
    Variable value: Ability to change according to user settings, system environment changes

2. Type of variable

  • Custom variables: defined, modified, and used by users themselves
  • Special variables: environment variables, read-only variables, location variables, predefined variables

5. Custom Variables

1. Definition of variables

Variable operations in Bash are relatively simple and less complex than other advanced programming languages such as C/C++, Java, and so on.When defining a new variable, you generally do not need to declare it in advance, but rather specify the name of the variable and assign it to the initial value (content).

2. Define a new variable

  • Variable names start with a letter or an underscore, case sensitive and recommended in all capitals

Format:
Variable name = variable value
There are no spaces on either side of the equal sign.Variable names must begin with a letter or an underscore, and do not include special characters in the name

2.1 Cancel variables

unset Variable Name
[root@c7-1 home]# unset Pr

3. View the value of a variable

  • Viewing and referencing variable values with echo

By prefixing the variable name with the leading symbol'$', you can reference the value of a variable, use the echo command to view the variable, and use the echo command to view multiple variable values at the same time.

Format:
echo $Variable Name

[root@c7-1 home]# a=lz //Define Variables
[root@c7-1 home]# echo $a	//View the value of a variable
lz
[root@c7-1 home]# b=666
[root@c7-1 home]# echo $a $b
lz 666


#When variable names are easily confused with other characters that follow them,
#You need to brace'{}'or you won't be able to determine the correct variable name.
#For undefined variables, null values are displayed
[root@c7-1 home]# echo $Pr
Python
[root@c7-1 home]# echo ${Pr}2.7.20
Python2.7.20

[root@c7-1 home]# echo ${test}RMB
RMB

4.echo options

[root@c7-1 home]# Echo-n Hello //-n means no line break output
hello[root@c7-1 home]#


[root@c7-1 home]# Echo-e "hello\n" //-e outputs the escape character, which is then output to the screen;N Line Break
hello								

[root@c7-1 home]# Echo-e "hello\t" // \t After escape insert tab, that is, tab
hello

#Note: \Escape character, special symbols following \ will lose their special meaning and become ordinary characters.
#For example, $will output the'$'symbol instead of being a variable reference
	

5. Special assignment operations

1. Double quotation mark ""

Double quotation marks allow you to refer to other variable values through the $symbol. Double quotation marks mainly define strings, especially when spaces are included in the assignment.In other cases double quotation marks can usually be omitted

#When there are spaces in the content
[root@c7-1 home]# echo "hello world"
hello world

#When assigning values in variables
[root@c7-1 home]# version=2
[root@c7-1 home]# pyver="python $version"
[root@c7-1 home]# echo $pyver 
python 2

2. Single quotation marks''

No reference to other variable values, $treated as normal character
However, if the assignment contains a single quotation mark'', it needs to be escaped with the'symbol to avoid conflict.

[root@c7-1 ~]# pyver='python $version'
[root@c7-1 ~]# echo $pyver 
python $version

3. Inverse Apostrophe`

Inverse apostrophes are used primarily for command substitution, allowing you to assign a variable to the screen output from executing a command.
The scope enclosed in apostrophes must be a command line that can be executed or an error will occur

[root@c7-1 ~]# ls -lh `which useradd`
-rwxr-x---. 1 root root 116K 11 June 6, 2016 /usr/sbin/useradd
#Find the program location of the useradd command by which useradd command first, then list the file properties based on the search results

[root@c7-1 ~]# time=`date +%T`
[root@c7-1 ~]# echo $time
13:56:41

#It is difficult to implement nested command substitution in a single line of commands with an apostrophe.
#Instead of the apostrophe, you can use'$()'to solve nesting problems
[root@c7-1 ~]# rpm -qc $(rpm -qf $(which useradd))
/etc/default/useradd
/etc/login.defs

4.read command

In addition to the above assignments, you can use Bash's built-in command read to assign values to variables.
Used to prompt the user to enter information for simple interaction.Read in from standard input device (keyboard) when executed
A line of content, separated by spaces, assigns each read field to the specified variable in turn (the extra content is assigned to the
Last variable).If only one variable is specified, the entire line is assigned to the variable.

Interactive Definition Variable
-p Prompt user for information
-n Define Number of Characters
-s Do not display user input, often used to enter a password
-t Defines the time-out period beyond which no output will exit automatically



[root@c7-1 ~]# Read-p "Please enter your name:" name
 Please enter your name: dashiji   
[root@c7-1 ~]# echo $name
dashiji

[root@c7-1 ~]# Read-s-p "Please enter your password:" pass
 Please enter your password:[root@c7-1 ~]# 
[root@c7-1 ~]# echo $pass
123

5. Read from a file and assign it to a variable

[root@c7-1 ~]# echo 192.168.3.3 > ip.txt
[root@c7-1 ~]# cat ip.txt 
192.168.3.3

[root@c7-1 ~]# Read-p "Please enter your IP:" IP < ip.txt
[root@c7-1 ~]# echo $IP
192.168.3.3

6. Set the scope of the variable

By default, newly defined variables are only valid in the current Shell environment and are therefore called local variables, which are no longer available when entering a subroutine or a new Shell environment

#Format 1:
export Variable Name

#Format 2:
export Variable Name=Variable Value

#The two formats can be mixed

To enable user-defined variables to continue to work in all child Shell environments and to reduce duplicate settings, you can export through the internal command
Exports the specified variable as a global variable.Users can specify multiple variable names as parameters at the same time (without using the "s" symbol), with variable names separated by spaces

[root@c7-1 ~]# name=zhangsan
[root@c7-1 ~]# test=18
[root@c7-1 ~]# echo $name $test
zhangsan 18
[root@c7-1 ~]# bash //Enter child shell environment
[root@c7-1 ~]# echo $name $test 	
						//Discovery is empty
[root@c7-1 ~]# exit 
exit
[root@c7-1 ~]# export name test		//Export as Global Variable
[root@c7-1 ~]# bash
[root@c7-1 ~]# echo $name $test		//Subprogram references global variables
zhangsan 18

Export allows you to export global variables and assign them at the same time, so you don't need to assign them in advance when you define a new global variable. env looks at the user's current environment variables

[root@c7-1 ~]# export abc=123
[root@c7-1 ~]# env
abc=123

7. Variable operations

Operations of 7.1 Variables

#Format:
expr Variable 1 Operator Variable 2 [Operator variable 3]...
  • Common operators:
Addition operation:+
Subtraction operation:-
Multiplication:\*
Division operation:/
Modulo (Remainder) operations:%

Example:

[root@c7-1 ~]# expr 1+1
1+1
[root@c7-1 ~]# expr 1 + 1
2
[root@c7-1 ~]# expr 12 \* 2
24
[root@c7-1 ~]# expr 12 /  2
6

[root@c7-1 ~]# x=42
[root@c7-1 ~]# y=36
[root@c7-1 ~]# expr $x + 5
47
[root@c7-1 ~]# sum=`expr $y \* $y \* 2`
[root@c7-1 ~]# echo $sum
2592



[root@c7-1 ~]# vim shuzi.sh
#!/bin/bash
read -p "Please enter the first number:" num1
read -p "Please enter the second number:" num2

sum=`expr $num1 + $num2`

echo "Sum: $sum"

[root@c7-1 ~]# bash shuzi.sh 
Please enter the first number: 22
 Please enter the second number: 22
 Sum: 44

[root@c7-1 ~]# echo $[10+10]
20

[root@c7-1 ~]# echo $((1+1))
2

6. Special Shell Variables

1. Environment variables

Pre-created by the system to set the user's working environment
Configuration file: /etc/profile, ~/.bash_profile

  • Common environment variables
    -PWD,PATH
    -USER,SHELL,HOME

The env command allows you to see the environment variables in your current working environment, and for some common environment variables, you should understand their respective uses.For example, the variable USER denotes the user name, HOME denotes the user's host directory, LANG denotes the language and character set, and PWD denotes the current working directory.
PATH denotes command search path, RANDOM denotes random number, returns integer 0-32767, USER denotes account name of current account, etc. Generally defined in full capitals, be careful to distinguish from custom variables

[root@c7-1 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin

[root@c7-1 ~]# echo $HOME
/root

[root@c7-1 ~]# echo $USER
root

[root@c7-1 ~]# echo $LANG
zh_CN.UTF-8

1.1 PATH variable

The PATH variable sets the default search path for executable programs. When only the file name is specified to execute the command program, the Linux system will look for the corresponding executable in the directory range specified by the PATIH variable and prompt "command not found" if it cannot be found.

[root@c7-1 ~]# fs.sh
bash: fs.sh: Command not found...

[root@c7-1 ~]# PATH="$PATH:/root"
[root@c7-1 ~]# fs.sh
123456

2. Read-only variables

  • Used when variable values are not allowed to be modified
[root@c7-1 ~]# name=11111
[root@c7-1 ~]# readonly name
[root@c7-1 ~]# echo $name 
11111
[root@c7-1 ~]# name=2222
-bash: name: read-only variable
[root@c7-1 ~]# unset name
-bash: unset: name: Unable to reverse settings: read-only variable
[root@c7-1 ~]# 

#Restart invalidates and can be modified

3. Location variables

  • Number expressed as $n, n between 1 and 9
[root@c7-1 ~]# vim user.sh
#!/bin/bash
useradd $1
echo $2 | passwd --stdin $1

[root@c7-1 ~]# Bash user.sh Lisi 123 //lisi is the first positional parameter, $1;123 is the second position parameter $2
 Change User lisi Password.
passwd: All authentication tokens have been successfully updated

4. Predefined variables

  • $#: Number of location variables on the command line
  • $*: Content of all location variables
  • $?:The state returned after the last command execution, when the return value is 0, indicating normal execution, and a non-zero value indicating an exception or error in execution
  • $0: Currently executing process/program name
  • $@: Indicates that all location parameters are listed, but in a single form
  • $$: Process number indicating the return of the current process
  • $!:Return the process number of the last background process
[root@c7-1 ~]# vim fuhao.sh
#!/bin.bash

echo $1
echo "$0 Represents the name of the script or program currently executing"
echo "$# Number of parameters representing positions on the command line
echo "$* The content of all the location parameters as a whole"
echo "$@ Indicates that all unknown parameters are listed, but in a single form

[root@c7-1 ~]# bash fuhao.sh 1 2 3 4
1
fuhao.sh Represents the name of the script or program currently executing
4 Number of parameters representing positions on the command line
1 2 3 4 The content of all the location parameters as a whole
1 2 3 4 Indicates that all unknown parameters are listed, but in a single form


#Understand the difference between $* and $@
$*  : Consider all parameters as a space-delimited string as a whole(Single String)Back on behalf of"$1 $2 $3 $4". 
$@	: Separate parameters with double quotes n Copy of the parameter list, each parameter returned as a string, representing"$1" "$2" "$3" "$94". 

$*  : Consider all parameters as a whole
$@	: Treat each parameter as a separate individual

set:View all variables in the system, including environment variables and custom variables (without the command to view custom variables separately, you can set Pipe Filtration)

summary

  • The role of Shell and scenarios
  • Writing Specification and Execution Method of Shell Script
  • Role and usage of redirection and piping
  • How to use single quotation marks, double quotation marks and apostrophes when assigning custom variables
  • Common operators for numeric variables: +, -, *, /,%
  • Use of environment variables, read-only variables, location variables, predefined variables

Keywords: Linux CentOS shell

Added by ammupon on Tue, 07 Sep 2021 07:29:21 +0300