- Learning Linux Shell Scripting
- Ganesh Naik
- 222字
- 2021-06-25 22:02:56
Working with command-line arguments (special variables, set and shift, getopt)
Command-line arguments are required for the following reasons:
- They inform the utility, or they command which file or group of files to process (reading/writing of files)
- Command-line arguments tell the command/utility which option to use
Check out the following command line:
[student@localhost ~]$ my_program arg1 arg2 arg3
If my_command is a bash shell script, then we can access every command-line positional parameter inside the script, as follows:
$0 would contain "my_program" # Command $1 would contain "arg1" # First parameter $2 would contain "arg2" # Second parameter $3 would contain "arg3" # Third parameter
The following is a summary of the positional parameters:

Let's create a script parameter.sh, as follows:
#!/bin/bash echo "Total number of parameters are = $#" echo "Script name = $0" echo "First Parameter is $1" echo "Second Parameter is $2" echo "Third Parameter is $3" echo "Fourth Parameter is $4" echo "Fifth Parameter is $5" echo "All parameters are = $*"
Then, as usual, give execute permission to the script and then execute it:
./parameter.sh London Washington Delhi Dhaka Paris
The output is as follows:
Total number of parameters are = 5Command is = ./parameter.shFirst Parameter is LondonSecond Parameter is WashingtonThird Parameter is DelhiFourth Parameter is DhakaFifth Parameter is ParisAll parameters are = London Washington Delhi Dhaka Paris
推薦閱讀
- Microsoft Power BI Quick Start Guide
- Verilog HDL數(shù)字系統(tǒng)設(shè)計入門與應(yīng)用實例
- 空間機器人遙操作系統(tǒng)及控制
- 數(shù)據(jù)挖掘?qū)嵱冒咐治?/a>
- 大數(shù)據(jù)時代的數(shù)據(jù)挖掘
- 自主研拋機器人技術(shù)
- 大數(shù)據(jù)安全與隱私保護
- 信息物理系統(tǒng)(CPS)測試與評價技術(shù)
- 愛犯錯的智能體
- 我也能做CTO之程序員職業(yè)規(guī)劃
- Learning Linux Shell Scripting
- Ansible 2 Cloud Automation Cookbook
- AVR單片機工程師是怎樣煉成的
- 計算機辦公應(yīng)用培訓(xùn)教程
- Eclipse全程指南