Application of Special Variables and Special Symbols in Shell Script

Records : 428

Scenario : Commonly used special symbols in Shell scripts, =, $, ${}, $0, $n, $#, $*, $@, $?, $$, (( )), [[ ]], $[] , |, >>, >, <, <<.

Version : CentOS Linux release 7.9.2009.

1. Brief description of the functions of Shell special symbols

(1) Use = to assign variables.

(2) Use $ and ${} to get variable values.

(3) Use $0 to get the name of the current script file.

(4) Use $n to obtain the parameters passed to the script or function, and the number n indicates the number of parameters. For example: $1 means the first parameter, $2 means the second parameter.

(5) Use $# to obtain the number of parameters passed to the script or function.

(6) Use $* to get all parameters passed to the script or function.

(7) Use $@ to get all parameters passed to the script or function.

(8) Use $? to get the exit status of the previous command.

(9) Use $$ to get the current Shell process ID.

(10) Use (( )) to perform mathematical operations on integers.

(11) Use $[] to perform mathematical operations on integers.

(12) Use [[ ]] to check whether a certain condition is true.

(13) Use the | pipe symbol to connect multiple commands.

(14) Use >> to append the correct output of the command to the file.

(15) Use > to output the correct output of the command to the file in an overwriting manner.

(16) Use < to use the content in the file as the input source of the command.

(17) Use << to 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.

2. Shell special symbols use scripts

#!/bin/bash
echo '1.使用=赋值变量'
city01="Hangzhou"
city02='Suzhou'
city03=Shanghai
echo '2.使用$和${}获取变量值'
echo "城市名称: $city01"
echo "城市名称: ${city02}"
echo "城市名称: ${city03}"
echo '3.使用$0获取当前脚本文件名称'
echo "脚本名称: $0"
echo '4.使用$n获取传递给脚本或函数的参数,n数字,表示第几个参数.例如:$1表示第1个参数,$2表示第2个参数'
echo "第1个参数: $1"
echo "第2个参数: $2"
echo '5.使用$#获取传递给脚本或函数的参数个数'
echo "参数个数: $#"
echo '6.使用$*获取传递给脚本或函数的所有参数'
echo "打印所有参数: $*"
echo '7.使用$@获取传递给脚本或函数的所有参数'
echo "打印所有参数: $@"
echo '8.使用$?获取上个命令的退出状态'
echo "上一命令状态码: $?"
echo '9.使用$$获取当前Shell进程ID'
echo "当前shell进程ID号: $$"
echo '10.使用(( )),对整数进行数学运算'
result=$((100*3+99))
echo "计算结果: $result"
echo '11.使用$[],对整数进行数学运算'
result=$[100*5+99]
echo "计算结果: $result"
echo '12.使用[[ ]],检测某个条件是否成立'
if [[ $1 < $2 ]]
then
  echo "参数1小于参数2"
elif [[ $1 == $2 ]]
then
  echo "参数1等于参数2"
else
  echo "参数1大于参数2"
fi
echo '13.使用|管道符连接多个命令'
result=`ps -ef | wc -l`
echo "管道符计算结果: ${result}"
echo '14.使用>>,以追加的方式,把命令的正确输出结果输出到文件中'
echo "Hangzhou" >> m01.txt
echo "Suzhou" >> m01.txt
echo "打印m01.txt文件内容:"
echo $(cat m01.txt)
echo '15.使用>,以覆盖的方式,把命令的正确输出结果输出到文件中'
echo "Hangzhou" > m02.txt
echo "Suzhou" > m02.txt
echo "打印m02.txt文件内容:"
echo $(cat m02.txt)
echo '16.使用<,把文件中的内容作为命令的输入源'
result=`wc -l </home/tutorial/province.txt`
echo "输入重定向计算结果: ${result}"
echo '17.使用<<,从标准输入(键盘)中读取数据,直到遇见停止符EOF才停止输入.'
echo "在控制台按照如下格式试验"
echo '[root@hadoop211 tutorial]# cat >/home/tutorial/province01.txt <<EOF'
echo '> Hangzhou'
echo '> Suzhou'
echo '> EOF'

3. Execute the script

Command: bash b2023051620.sh 20 30

Parsing: Execute the command with parameters.

4. Script execution result

[root@hadoop211 tutorial]# bash b2023051620.sh 20 30
1.使用=赋值变量
2.使用$和${}获取变量值
城市名称: Hangzhou
城市名称: Suzhou
城市名称: Shanghai
3.使用$0获取当前脚本文件名称
脚本名称: b2023051620.sh
4.使用$n获取传递给脚本或函数的参数,n数字,表示第几个参数.例如:$1表示第1个参数,$2表示第2个参数
第1个参数: 20
第2个参数: 30
5.使用$#获取传递给脚本或函数的参数个数
参数个数: 2
6.使用$*获取传递给脚本或函数的所有参数
打印所有参数: 20 30
7.使用$@获取传递给脚本或函数的所有参数
打印所有参数: 20 30
8.使用$?获取上个命令的退出状态
上一命令状态码: 0
9.使用$$获取当前Shell进程ID
当前shell进程ID号: 1656
10.使用(( )),对整数进行数学运算
计算结果: 399
11.使用$[],对整数进行数学运算
计算结果: 599
12.使用[[ ]],检测某个条件是否成立
参数1小于参数2
13.使用|管道符连接多个命令
管道符计算结果: 140
14.使用>>,以追加的方式,把命令的正确输出结果输出到文件中
打印m01.txt文件内容:
Suzhou Hangzhou Suzhou Hangzhou Suzhou Hangzhou Suzhou
15.使用>,以覆盖的方式,把命令的正确输出结果输出到文件中
打印m02.txt文件内容:
Suzhou
16.使用<,把文件中的内容作为命令的输入源
输入重定向计算结果: 3
17.使用<<,从标准输入(键盘)中读取数据,直到遇见停止符EOF才停止输入.
在控制台按照如下格式试验
[root@hadoop211 tutorial]# cat >/home/tutorial/province01.txt <<EOF
> Hangzhou
> Suzhou
> EOF

Note: The province01.txt used in the example can be defined by yourself.

Above, thanks.

May 16, 2023

Guess you like

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