同一個可執行文件,軟鏈接到不同名字,執行不同內容

原理:可通過判斷 $0 來執行不同命令

如下:

[root@test-01 ~]# cat b 
#!/bin/bash
#

[[ $0 =~ ^(.*)/c$ ]] || [[ $0 == "c" ]] && echo "c" 
[[ $0 =~ ^(.*)/d$ ]] || [[ $0 == "d" ]] && echo "d"

[root@test-01 ~]# ll c d 
lrwxrwxrwx 1 root root 1 Sep  6 16:19 c -> b
lrwxrwxrwx 1 root root 1 Sep  6 16:20 d -> b
[root@test-01 ~]# ./c
c
[root@test-01 ~]# ./d
d
[root@test-01 ~]# ./b
[root@test-01 ~]# bash c
c
[root@test-01 ~]# bash d
d
[root@test-01 ~]# bash b  
[root@test-01 ~]# /root/c
c
[root@test-01 ~]# /root/d
d
[root@test-01 ~]# /root/b
[root@test-01 ~]#

上面 c 和 d 文件都是 b 文件的軟鏈接,通過判斷鏈接名字,即 $0,來執行不同的命令內容。

bash 和 sh 應該就是這種方式

[root@test-01 ~]# ll /usr/bin/sh
lrwxrwxrwx 1 root root 4 Aug 29 14:18 /usr/bin/sh -> bash
[root@test-01 ~]# ll /usr/bin/bash
-rwxr-xr-x 1 root root 964544 Apr 11 08:53 /usr/bin/bash


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章