当前位置:网站首页>Getting started with shell variables
Getting started with shell variables
2022-06-25 13:59:00 【Resounding through heaven】
1 Custom global variables
2.1 Father and son Shell Introduction to the environment
for example : Yes 2 individual Shell Script files parent.sh and child.sh
If stay parent.sh In the script file child.sh Script files , that parent.sh Is the father Shell Environmental Science , child.sh It's the son Shell Environmental Science
2.2 Introduction to custom global variables
Is to define global variables in the current script file , This global variable can be used in the current Shell Environment and sub Shell It can be used in the environment
2.3 Custom global variable Syntax
export var_name1 var_name2
2.4 DEMO
Test the global variable in the child Shell Is it available in , In the father Shell Is it available in
1: establish 2 Script
touch parent.sh
touch child.sh
2: modify parent Script files
vi parent.sh
#!/bin/bash
name="parentName"
export name
# Call the subscript file
sh child.sh
3: modify child Script files
vi child.sh
#!/bin/bash
echo "parent As defined in name: ${name}"
4: Execute parent script
sh parent.sh
5: result 
2 Special variables $n
2.1 grammar
$n
2.2 meaning
Used to receive the parameters passed in when the script file is executed
$0 Used to get the name of the current script file
$1~$9, Means to obtain the first input parameter to the 9 Three input parameters
The first 10 Format of more than input parameters : ${
Numbers }, Otherwise, you can't get
2.3 Execute script file to pass in parameter syntax
sh Script files parm1 parm2 ...
2.4 demo
Create script file demo.sh file , And print the name of the script file in the script file , The first input parameter , The second input parameter
vim demo.sh
#!/bin/bash
# Print the current script file name
echo " Current script file name : ${0}"
# Print input parameters
echo " The first input parameter : ${1}"
echo " The second input parameter : ${2}"
perform :

3 Special symbolic variables $#
3.1 grammar
$#
3.2 meaning
Get the number of all input parameters
3.3 demo
Print demo.sh Enter the number of parameters .
vim demo.sh
#!/bin/bash
# Print the current script file name
echo " Current script file name : ${0}"
# The number of input parameters is
echo " The number of input parameters is : ${#}"
# Print input parameters
echo " The first input parameter : ${1}"
echo " The second input parameter : ${2}"

4 Special variables $* [email protected]
4.1 grammar
$*、[email protected]
4.2 meaning
1. Do not use double quotation marks , Function as
$* and [email protected] Get all input parameters , The format is : $1 $2 ... $n
2. Use double quotes
"$*" All the parameters obtained are spliced into a string , The format is : "$1 $2 ... $n"
"[email protected]" Get a set of parameter list objects , The format is : "$1" "$2" ... "$n"
You can see the difference by printing all the input parameters in a loop
Cyclic grammar :
for var in List variables
do # Loop start
command # The loop body
done # The loop ends
4.3 demo
stay demo.sh Print out all input parameters in a cycle , Experience $* And [email protected] The difference between
#!/bin/bash
# Print the current script file name
echo " Current script file name : ${0}"
# The number of input parameters is
echo " The number of input parameters is : ${#}"
# Output all input parameters
echo ' Use $* Direct output :' ${
*}
echo ' Use [email protected] Direct output :' ${
@}
# Cyclic output
echo ' Loop through the output $* All the parameters :'
for temp in "$*"
do
echo ${
temp}
done
echo ' Loop through the output [email protected] All the parameters :'
for temp in "[email protected]"
do
echo ${
temp}
done

5 Special symbolic variables $?
5.1 command
$?
5.2 meaning
Used to get previous Shell Exit status code of the command , Or the return value of the function
5.3 demo

边栏推荐
- 关于STM32使用LAN8720A插拔网线重连
- Vscode--- format setting configuration
- 专家建议|8大措施加速你的创新职业规划和成长
- Gorm-- search you don't know
- 腾讯云搭建Socks5多IP代理服务器实现游戏单窗口单IP完美搭建教程附带工具「建议收藏」
- Websocket -- reverse proxy to solve cross domain problems
- shell 运算符
- untiy强制刷新UI
- VGA display of de2-115 FPGA development board
- Error1822 and error1824 are displayed in the database
猜你喜欢

一次性讲清楚 Handler 可能导致的内存泄漏和解决办法 | 开发者说·DTalk

Windows下MySQL的安装和删除

Discuz copy today's headlines template /discuz news and information business GBK template

Application of tactile intelligent sharing-rk3568 in financial self-service terminal
![Leetcode: Sword finger offer II 091 Painting house [2D DP]](/img/d7/dc8a3522dbd58b4573cfd75497460c.png)
Leetcode: Sword finger offer II 091 Painting house [2D DP]

多台云服务器的 Kubernetes 集群搭建

英语中的九大词性与九大时态

Logistic Regression VS Linear Regression

多台云服务器的 Kubernetes 集群搭建

OpenStack学习笔记(一)
随机推荐
[proteus simulation] 51 MCU +ds1302+lcd1602 display
Prototype and prototype chain - constructor and instanceof
Websocket -- reverse proxy to solve cross domain problems
Leetcode: Sword finger offer II 091 Painting house [2D DP]
哈希表、哈希冲突
Settings the PC must be turned on
Intercept based on byte length
Error1822 and error1824 are displayed in the database
Drago Education - typescript learning
如何在 2022 年为 Web 应用程序选择技术堆栈
Knowledge of initial C language 2.0
专家建议|8大措施加速你的创新职业规划和成长
Discuz仿今日头条模板/Discuz新闻资讯商业版GBK模板
数据库中显示error1822,error1824
sql导入这样怎么解决
OpenStack-----Nova源码分析之创建虚拟机
使用调试工具调试博图TCP连接所遇到的问题
Windows下MySQL的安装和删除
Cold migration and resize of Nova component source code analysis
leetcode:456. 132 mode [monotone stack]