Special position variable Bash Shell and Its Application

                            Special position variable Bash Shell and Its Application

      As we all know the bash shell has many unique position variable, the flexibility to use them to better perform their functions Shell script.

That position variables: $ 1, $ 2, ... are represented, for a script to call its argument passed to the command line in the script code

Special variables: $ ?, $ 0, $ *, $ @ # $, $$

---------------- ----------------------------- specific analysis

$:? For the return code of a command on the detector, 0 for success, 1-255 represents a failure

$ 0: command itself

$ *: All parameters, all the parameters passed to the script of the table into one string

$ @:  Table all the parameters passed to the script, each parameter as a separate string

$ #: The number of passing parameters to the script

$$: Get the process ID of the currently executing script Shell

For a better understanding, I wrote the following script to test, pay attention to $ 10 for brackets to be recognized.

[root@Franklin13 ~]# echo $SHELL
/bin/bash

 [root@Franklin13 ~]# vim test_arg.sh 

[root@Franklin13 ~]# bash test_arg.sh {a..z}

1st arg is a
2st arg is b
10st arg is j
All arg is a b c d e f g h i j k l m n o p q r s t u v w x y z
All arg is a b c d e f g h i j k l m n o p q r s t u v w x y z
The arg number is 26
The scriptname is test_arg.sh
[root@Franklin13 ~]#

Real 1: utilize $ 1 to write a script that automatically take ip


 

First, use the following command to intercept ip, success

[root @ Franklin13 ~] # ifconfig ens33 | grep -w "inet" | tr -s ''% | cut -d% -f3 (tr -s ''% expressed compresses all duplicate spaces to stay only a reconversion as%)
192.168.1.19

And then to make this command by calling the $ 1 can be used for a script to quickly check ip.

 

 [root@Franklin13 ~]# ./get-ip.sh lo

The ip is 127.0.0.1
Thank you for using!
[root@Franklin13 ~]# ./get-ip.sh ens33
The ip is 192.168.1.19
Thank you for using!
[root@Franklin13 ~]# ./get-ip.sh virbr0
The ip is 192.168.122.1
Thank you for using!

 -------------------------------- ---------------- The End -------------------

 

Guess you like

Origin www.cnblogs.com/Franklinhong-No1/p/11565004.html