shell programming tutorial

I Format of shell script file

shell script file generally consists of three parts: interpreter, command code and comments.

[root@server ~]# cat test

 Here are multiline comments
echo 'hello world'
# Here is a single line comment
echo valar morghulis
[root@server ~]# sh test
hello world
valar morghulis

The first line of the script is the interpreter. The function of this line is to specify the interpreter to be used by the script. The linux system will use this information to determine which interpreter to use to interpret the command code of the whole script. For example, the one used in this example is #/ bin/bash, whose first character is the pound sign '#', will not be executed as a command.

Next, there are two formats of comments. The format of < < beginning with any character is multi line comment, and the end of multi line comment should be consistent with the character after the beginning <. A single line comment begins with a pound sign '#'. The function of annotation is to explain the function implemented by the code, but not really execute it.

The next largest part is the command code, which enables the script to realize a variety of functions. As long as the syntax is correct, developers can give full play to it.

II Definition and use of variables

1. User defined variables

1.1 variables specified in the shell can only consist of upper and lower case letters, numbers and underscores () Composition, and the beginning cannot be a number. Below are some legal variable names: bob and team_1,Group1,_ temp.

1.2 variable is defined by variable name = variable value: for example, bob=15, team_1="Beijing"

1.3 the cancellation method of variable is unset variable name

1.4 variables are used by adding $, such as:

[root@server ~]# cat test
echo Bob this year $Bob Years old.
[root@server ~]# sh test
Bob He is 15 years old.

However, if the variable is followed by other characters, the shell will misjudge the variable name, making the function impossible, such as:

[root@server ~]# cat test
echo ${prefix}assmate
echo 1
echo $prefixassroom
echo 2
[root@server ~]# sh test


Here, the shell does not know the value of $prefixassroom, because there is no definition, so there is no output. The correct way to use it is to enclose the variables in curly braces. As shown above, classmate can output correctly.  

1.5 read only variables are defined by readonly variable name = variable value or readonly variable name

[root@server ~]# cat test
readonly var=Jon
echo ${var}
echo ${var}
[root@server ~]# sh test
test:Line 4: var: read-only variable 

2. System preset variables

Common environment variables are shown in the following table: you can view them with the set or env command

$HOMEHome directory of the current user
$USERCurrent user name
$UIDCurrent user ID
$PATHPath to search command
$PWDCurrent working directory
$SHELLshell of the current system
$RANDOMReturns a random number

There are two ways to define environment variables. Temporary definitions can be used:

export MYVAL

The way to permanently define environment variables is in / etc/profile or ~ / After adding the above configuration in bashrc, execute the source command on the configuration file.

The following is a special status variable, which can be used directly in the script:

$0Current script name
$$Current process number
$1-9The parameter passed into the script, $1 is the first parameter. If you want to return more than 10 positional parameters, use braces:
$#The number of parameters passed into the script
$*Pass in all parameters of the script as a whole.
$@Pass in all parameters of the script as independent individuals.
$?Exit status code of the previous command.
$!ID number of the last process running in the background.
[root@server ~]# sh test 1 2 3 4 5 6 7 8 9 10
$HOME yes /root
$USER yes root
$UID Is 0
$PATH yes /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
$PWD yes /root
$SHELL yes /bin/bash
$RANDOM It's 19621
$0 yes test
$$ It's 31747
$1 Yes 1
$2 It's 2
$3 It's 3
$4 It's 4
$5 It's five
$6 It's 6
$7 It's 7
$8 It's 8
$9 It's 9
${10} It's 10
$# It's 10
$* It's 1 2 3 4 5 6 7 8 9 10
$@ It's 1 2 3 4 5 6 7 8 9 10
$? Is 0
$! yes


This paper introduces two parts of knowledge. The first part is the structure of shell script, which is composed of three parts: interpreter, command code and annotation. The second part is the definition and use of variables, which introduces user-defined variables and system preset variables respectively.

Keywords: Linux Operation & Maintenance shell bash server

Added by PHPisFUN on Wed, 05 Jan 2022 09:22:41 +0200