官术网_书友最值得收藏!

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
主站蜘蛛池模板: 章丘市| 乃东县| 镶黄旗| 永嘉县| 普安县| 高清| 阿拉善右旗| 城口县| 大姚县| 革吉县| 阳东县| 龙南县| 姚安县| 甘洛县| 祁阳县| 镇赉县| 南郑县| 京山县| 新沂市| 临夏市| 崇左市| 东宁县| 太白县| 肇东市| 山阳县| 莒南县| 都昌县| 德化县| 武川县| 孝昌县| 灵寿县| 沂南县| 蒲城县| 文水县| 九江市| 武功县| 望都县| 天等县| 旬阳县| 吉林省| 方城县|