Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

4.2.2 Receive the user's parameters


May 23, 2021 That's what Linux should learn



However, scripts like the one above can only perform some pre-defined functionality, which is too rigid. In order for the Shell scripter to better meet some of the user's real-time needs in order to get the job done flexibly, it is necessary for the scripter to receive user-entered parameters, as it did when the command was executed earlier.

In fact, the Shell scripting language in linux systems has long taken these into account, and variables for receiving parameters have been inset, with space intervals between variables. F Or Example, $ 0 Corresponds To The Name of the Current Shell Script, $ # Corresponding to a total of several parameters, $? Corresponds to the parameter Values Of All Locations, $? The parameter values for the Nth position correspond, as shown in Figures 4-15.

4.2.2 Receive the user's parameters

The parameter position variable in the Shell script in Figures 4-15

Let's practice after the theory. Try writing an example of a scripter that sees the real effect by referencing the variable parameters above:

[root@linuxprobe ~]# vim example.sh

!/bin/bash

  1. echo "当前脚本名称为$0"
  2. echo "总共有$#个参数,分别是$*。"
  3. echo "第1个参数为$1,第5个为$5。"
  4. [root@linuxprobe ~]# sh example.sh one two three four five six
  5. 当前脚本名称为example.sh
  6. 总共有6个参数,分别是one two three four five six
  7. 1个参数为one,第5个为five