将参数传递给脚本(Shell基础教程3.html)
Passing Arguments to the Script
可以在执行脚本时将参数传递为脚本,方法是在脚本文件名后将其作为空格分隔的列表编写。
在脚本内部,$1变量引用命令行中的第一个参数,$2引用第二个参数,依此类推。
变量$0引用当前脚本。在以下示例中,脚本名称后跟6个参数。
./bin/my_shopping.sh apple 5 banana 8 "Fruit Basket" 15
echo $3 --> results with: banana
BIG=$5
echo "A $BIG costs just $6" --> results with: A Fruit Basket costs just 15
变量$#保留传递给脚本的参数数量
echo $# --> results with: 6
变量$@包含一个以空格分隔的字符串,其中包含传递给脚本的所有参数
Exercise
本节没有练习。您可以继续。
Tutorial Code
#!/bin/bash
# There is no exercise for this section.
# You may proceed.
Solution
#!/bin/bash
# There is no exercise for this section.
# You may proceed.
Expected Output
#!/bin/bash
# There is no exercise for this section.
# You may proceed.
- Hello, World!(Shell基础教程1.html)
- 变量(Shell基础教程2.html)
- 将参数传递给脚本(Shell基础教程3.html)
- 数组(Shell基础教程4.html)
- 数组比较(Shell基础教程5.html)
- 基本运算符(Shell基础教程6.html)
- 基本字符串操作(Shell基础教程7.html)
- 逻辑表达式(Shell基础教程8.html)
- 循环(Shell基础教程9.html)
- shell函数(Shell基础教程10.html)
- 特殊变量(Shell基础教程11.html)
- 字符串操作(Shell基础教程12.html)
- 捕捉信号命令(Shell基础教程13.html)
- 文件测试(Shell基础教程14.html)
- 输入参数解析(Shell基础教程15.html)
- 管道(Shell基础教程16.html)
- 输入输出(Shell基础教程17.html)
- 常用表达(Shell基础教程18.html)
- 特殊命令sed(Shell基础教程19.html)
版权声明:本文为原创文章,版权归 戴老师的博客 所有,转载请联系博主获得授权。
本文地址:https://tushu.info/archives/archives-jiang-can-shu-chuan-di-gei-jiao-ben.html
如果对本文有什么问题或疑问都可以在评论区留言,我看到后会尽量解答。
暂无标签