Shell script basic application

Records : 427

Scenario : Shell script basic application. Script format, execution mode, defining and using variables, double quotes and single quotes, backquotes and $(), reading user input and files, output and input redirection, export command, alias command, exit command, viewing built-in commands .

Version : CentOS Linux release 7.9.2009.

Shell is an application program that connects users and the Linux kernel, allowing users to use the Linux kernel more efficiently, safely, and at low cost.

Shell itself is not part of the kernel, it is just an application written on the basis of the kernel.

1. CentOS command line console

Use the Xshell of Xmanager Enterprise to log in to the CentOS command line console.

1.1 root user command prompt

[root@hadoop211 ~]# 

Parsing: [], is a standard format. root means the current super user root. @hadoop211 indicates that the user is on the hadoop211 host. ~, indicates the root directory of the current user root. #, means the super user command prompt, and the operation command is entered after that.

Execute the pwd command:

[root@hadoop211 ~]# pwd
/root

1.2 Ordinary user command line prompt

[learn@hadoop211 ~]$ 

Parsing: [], is a standard format. learn indicates that it is currently an ordinary user learn. @hadoop211 indicates that the user is on the hadoop211 host. ~, indicates the root directory of the current user learn. $, indicates the command prompt of ordinary users, and the operation commands are entered after that.

Execute the pwd command:

[learn@hadoop211 ~]$ pwd
/home/learn

2. Shell script format

In general, a Shell script is an executable file.

2.1 Edit script

Screenplay name: b2023051601.sh

Edit command: vi b2023051601.sh

Script content:

#!/bin/bash
echo "Hello Shell."
echo "Shell is a scripting language."

Analysis: #! means to specify the script interpreter, specify /bin/bash to execute the b2023051601.sh script.

2.2 The script is given executable permission

Command: chmod +x b2023051601.sh

2.3 Execute the script

Command: ./b2023051601.sh

output:

Hello Shell.
Shell is a scripting language.

3. Shell script execution method

3.1 The script runs as a stand-alone program

3.1.1 Use ./ to execute

(1) Screenplay

Screenplay name: b2023051601.sh

Script content:

#!/bin/bash
echo "Hello Shell."
echo "Shell is a scripting language."

(2) Execution

Command: ./b2023051601.sh

(3) Analysis

#!/bin/bash must be specified in the script. The script needs to be given executable permission: chmod +x b2023051601.sh.

3.1.2 Use bash to execute

(1) Screenplay

Screenplay name: b2023051602.sh

Script content:

echo "Hello Shell."
echo "Shell is a scripting language."

(2) Execution

Command 1: bash b2023051602.sh

Command 2: /bin/bash b2023051602.sh

(3) Analysis

No need to specify #!/bin/bash in the script. Scripts do not need to be given executable permissions.

3.2 Run the script in the current process

(1) Screenplay

Screenplay name: b2023051603.sh

Script content:

echo "Hello Shell."
echo "Shell is a scripting language."

(2) Execution

Command 1: source b2023051603.sh

Command 2: .b2023051603.sh

(3) Analysis

No need to specify #!/bin/bash in the script. Scripts do not need to be given executable permissions.

4. Define variables and use variables

4.1 Script

Screenplay name: b2023051604.sh

Script content:

#!/bin/bash
echo "定义变量,city01,city02,city03"
city01=Hangzhou
city02='Suzhou'
city03="Shanghai"
echo "使用变量"
echo $city01
echo ${city02}
echo "长三角城市包括: ${city01},${city02},${city03}"

4.2 Execution and output

Execution: bash b2023051604.sh

output:

定义变量,city01,city02,city03
使用变量
Hangzhou
Suzhou
长三角城市包括: Hangzhou,Suzhou,Shanghai

4.3 Attention

When assigning a value, there must be no spaces on the left and right sides of the equal sign =.

4.4 Setting read-only variables and deleting variables

Screenplay:

#!/bin/bash
echo "定义变量,city01,city02,city03"
city01=Hangzhou
city02='Suzhou'
city03="Shanghai"
echo "设置为只读变量"
readonly city03
city03="Ningbo"
echo "city03=${city03}"
echo "删除变量"
unset city02
echo "city02=${city02}"

implement:

定义变量,city01,city02,city03
设置为只读变量
b20230516041.sh: line 8: city03: readonly variable
city03=Shanghai
删除变量
city02=

Analysis: city03 cannot be modified after being set as a readonly read-only variable. After city02 is deleted, the output has no value.

5. Double quotes and single quotes

When using single quotes ' ' to enclose the value of the variable, the content inside the single quotes will be output as it is. If there are variables and commands in the content, it will be output as it is. That is, using single quotes will not parse variables and commands.

When using double quotes " " to enclose the value of a variable, the variables and commands inside will be parsed first when outputting, instead of outputting the variable names and commands in double quotes as they are.

Scenarios for using single quotes: When displaying pure strings, when you do not want to parse variables, commands, etc.

Scenarios for using double quotes: Suitable for scenarios where variables and commands are attached to the string and need to be parsed before outputting.

5.1 Script

Screenplay name: b2023051605.sh

Script content:

#!/bin/bash
echo "定义变量,city01,city02,city03"
city01=Hangzhou
city02='Suzhou'
city03="Shanghai"
echo "单引号输出"
echo '长三角城市包括: ${city01},${city02},${city03}'
echo "双引号输出"
echo "长三角城市包括: ${city01},${city02},${city03}"

5.2 Execution and output

Execution: bash b2023051605.sh

output:

定义变量,city01,city02,city03
单引号输出
长三角城市包括: ${city01},${city02},${city03}
双引号输出
长三角城市包括: Hangzhou,Suzhou,Shanghai

6. Backticks and $()

Backticks `` and $() can assign the command result to a variable.

6.1 Script

Screenplay name: b2023051606.sh

Script content:

#!/bin/bash
echo '使用反引号``把命令结果赋值给变量'
cityInfo01=`cat cityInfo.txt`
echo ${cityInfo01}
echo '使用$()把命令结果赋值给变量'
cityInfo02=$(cat cityInfo.txt)
echo ${cityInfo02}

6.2 Execution and output

Execution: bash b2023051606.sh

output:

使用反引号``把命令结果赋值给变量
长三角城市包括上海、杭州、苏州等。
使用$()把命令结果赋值给变量
长三角城市包括上海、杭州、苏州等。

6.3 File content

File name: cityInfo.txt

document content:

长三角城市包括上海、杭州、苏州等。

7. Read user input

7.1 Script

Screenplay name: b2023051607.sh

Script content:

#!/bin/bash
echo "请输入城市名称:"
read cityname
echo "城市名称: ${cityname}"

7.2 Execution and output

Execution: bash b2023051607.sh

output:

请输入城市名称:
Hangzhou
城市名称: Hangzhou

8. Read the file

8.1 Script

Screenplay name: b2023051608.sh

Script content:

#!/bin/bash
echo '1.使用cat读取文件并逐行打印'
filePath=`pwd`
for line in `cat ${filePath}/province.txt`
do 
  echo "${line}"
done
echo "  "
echo '2.使用read读取文件并逐行打印'
cat ${filePath}/province.txt | while read line
do
  echo "${line}"
done

8.2 Execution and output

Execution: bash b2023051608.sh

output:

1.使用cat读取文件并逐行打印
长三角省份有浙江、江苏等。
长三角最大都市是上海。
长三角经济发达。
  
2.使用read读取文件并逐行打印
长三角省份有浙江、江苏等。
长三角最大都市是上海。
长三角经济发达。

9. Output Redirection

9.1 Script

Screenplay name: b2023051609.sh

Script content:

#!/bin/bash
rm -rf city01.txt city02.txt
echo '>表示以覆盖的方式输出到文件'
for city in "杭州" "苏州" "上海"
do 
  echo ${city}>city01.txt
done
echo "  "
echo '>>表示以追加的方式输出到文件'
for city in "杭州" "苏州" "上海"
do 
  echo ${city}>>city02.txt
done

9.2 Execution and output

Execution: bash b2023051609.sh

City01.txt and city02.txt content:

[root@hadoop211 tutorial]# cat city01.txt 
上海
[root@hadoop211 tutorial]# cat city02.txt 
杭州
苏州
上海

10. Input redirection

command <file: Use < to redirect the contents of a file to the command input.

command <<EOF: Read data from the standard input (keyboard), and stop inputting until the stop character EOF is encountered. The stop character EOF is an arbitrary string, a symbol defined by the user.

command <file1 >file2: The file file1 is used as the input source of the command, and the processing result of the command is output to the file file2.

10.1 Script

Screenplay name: b20230516010.sh

Script content:

#!/bin/bash
filePath=`pwd`
echo '1.使用<输入重定向到wc命令'
wc -l <${filePath}/province.txt
echo " "
echo '2.使用<输入重定向到read命令'
while read line
do
  echo ${line}
done <${filePath}/province.txt

10.2 Execution and output

Execution: bash b20230516010.sh

output:

1.使用<输入重定向到wc命令
3
 
2.使用<输入重定向到read命令
长三角省份有浙江、江苏等。
长三角最大都市是上海。
长三角经济发达。

11. export command

The export command is used to set or display environment variables.

When the shell executes a program, the shell provides a set of environment variables. export can add, modify or delete environment variables for use by subsequent programs. The effect of export is limited to this login operation.

11.1 Script

Screenplay name: b2023051611.sh

Script content:

#!/bin/bash
echo "1.默认系统的Path变量"
echo $PATH
export WORK_HOME=/home/tutorial
echo "2.新增系统的Path变量后"
export PATH=$PATH:$WORK_HOME/bin
echo $PATH

11.2 Execution and output

Execute: bash b2023051611.sh

output:

1.默认系统的Path变量
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/apps/module/jdk1.8.0_281/bin:/root/bin
2.新增系统的Path变量后
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/home/apps/module/jdk1.8.0_281/bin:/root/bin:/home/tutorial/bin

12.alias command

The alias cannot be written into a custom script, and it can be written in the /root/.bashrc file if it needs to be restarted to take effect.

12.1 Using aliases

Custom command alias: alias llh='ll -h'

12.2 Execution and output

Execution: llh

output:

[root@hadoop211 ~]# alias llh='ll -h'
[root@hadoop211 ~]# llh
total 4.0K
-rw-------. 1 root root 1.4K Oct 21  2022 anaconda-ks.cfg

13. exit command

Use exit to exit the program, and use $? to view the program status.

13.1 Script

Screenplay name: b2023051613.sh

Script content:

#!/bin/bash
echo '设置exit命令状态码'
exit 16

13.2 Execution and output

Execution: bash b2023051613.sh

output:

[root@hadoop211 tutorial]# bash b2023051613.sh 
设置exit命令状态码
[root@hadoop211 tutorial]# echo $?
16

14. View built-in commands

14.1 Execution and output

Use type to check whether a command is a built-in command or a non-built-in command.

[root@hadoop211 ~]# type cd
cd is a shell builtin
[root@hadoop211 ~]# type ll
ll is aliased to `ls -l --color=auto'
[root@hadoop211 ~]# type yum
yum is /usr/bin/yum

Above, thanks.

May 16, 2023

Guess you like

Origin blog.csdn.net/zhangbeizhen18/article/details/130714095