- Outline
- parameter
- background
- Review the parameters of shell scripts get
- Scenes
- the
- centos7
- the
1. Parameters: Basic
- Outline
- Quick description parameter
1. Obtain parameters
Get the first argument
- Acquisition parameters
- Acquisition parameters using $ {num}
- $ {1} acquired first parameter
Code
#!/bin/bash var1=${1} echo ${var1}
- other
- {2} can try $, $ {3} and other subsequent parameters acquired
- By default, if enough arguments, echo does not complain, other commands, you need to verify
- Acquisition parameters
2. Get more parameters
- Question 1: obtaining multiple parameters
- A get a parameter, if the parameter more, will not be a lot of trouble
- solve
- All parameters can be obtained as
- plan 1:
$*
$*
- Once to get all the parameters
Code
#!/bin/bash var1=$* echo ${var1}
- Question 2: I went to separate parameters
- All parameters
- $ * Get to the parameters, all the parameters are ranked together
- need
- I want to get a certain parameter alone
- solve
- 1 idea: you know looking for the first of several parameters
- Use $ {num}
- 2 ideas:? What you do not know what number, but you know what kind of parameters
- Converted into an array
- Temporarily not recommended
- Conversion array, I will not
- Array after conversion, do not know if the subject of the next element, required circulating operation of the array, I will not
- Temporarily not recommended
- Converted into an array
- 3 ideas: Use $ @
- 1 idea: you know looking for the first of several parameters
- All parameters
- Scenario 2:
$@
- All parameters
- More use when cycling
Code
#!/bin/bash for word in "$@" do echo ${word} done
- All parameters
3. Get the last parameter
- Question: The last parameter
- the last one
- I wanted to get the last parameter
- the last one
solve
#!/bin/bash # 假设参数是 1 2 3 # 最后一个参数, ${3} var1=${#} echo $var1 echo ${!var1} # 倒数 第二个参数, ${2} var2=$[${#}-1] echo $var2 echo ${!var2}
- Question: hand slipped parameters did not increase
- problem
- Suddenly hand sliding, no additional parameters
- result
- The first paragraph of printing out the script name
- problem
4. Get the name of the script
- Screenplay name
- ${0}
- Print $ {0} to see
- ${0}
- problem
./<script>
- display
./<script>
- display
bash <script>
- display
source <script>
- 显示 -bash
- ????
- 显示 -bash
ps
- ref
- 学习 bash
- Shell 传递参数
- Linux获取最后一个参数(参数统计)
- 疑问
- 获取脚本名称如果这么不稳定, 那是否有 稳定的方法 呢?
- 后续尝试下 获取 选项