同一个可执行文件,软连接到不一样名字,执行不一样内容

原理:可经过判断 $0 来执行不一样命令
bash

以下:ide

[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,来执行不一样的命令内容。it

bash 和 sh 应该就是这种方式
class

[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
相关文章
相关标签/搜索