- Learning Linux Shell Scripting
- Ganesh Naik
- 153字
- 2021-06-25 22:02:57
Understanding default parameters
Many times, we may pass certain parameters from the command line, but, sometimes, we may not pass any parameters at all. We may need to initialize certain default values to certain variables.
We will review this concept through the following script.
Create script default_argument_1.sh, as follows:
#!/bin/bash MY_PARAM=${1:-default} echo $MY_PARAM
Execute the script and check the output:
$ chmod +x default_argument_1.sh One$ ./default_argument_1.sh OneOne$ ./default_argument_1.shdefault
Create another default_argument_2.sh script:
#!/bin/bash variable1=$1 variable2=${2:-$variable1} echo $variable1 echo $variable2
The output is as follows:
We executed the script two times:
- When we passed two arguments, then variable1 was $1 and variable2 was $2.
- In the second case, when we passed only one argument, then $1 was taken as the default argument for $2. Therefore, variable1 was used as the default for variable2. If we do not give a second parameter, then the first parameter is taken as the default for the second parameter.
推薦閱讀
- Deep Learning Quick Reference
- 精通MATLAB圖像處理
- PostgreSQL 11 Server Side Programming Quick Start Guide
- 輕松學Java Web開發
- 走入IBM小型機世界
- 完全掌握AutoCAD 2008中文版:機械篇
- Grome Terrain Modeling with Ogre3D,UDK,and Unity3D
- 網絡布線與小型局域網搭建
- PVCBOT機器人控制技術入門
- Mastering Geospatial Analysis with Python
- 單片機原理實用教程
- 從零開始學JavaScript
- Java Deep Learning Projects
- 7天精通Photoshop CS5平面視覺設計
- 當產品經理遇到人工智能