加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何从bash脚本中“隐藏”可执行文件?

发布时间:2020-12-16 01:28:03 所属栏目:安全 来源:网络整理
导读:我想测试bash脚本的输出,当它依赖的其中一个可执行文件丢失时,所以我想运行该脚本的依赖项“隐藏”而不是其他. PATH = ./script不是一个选项,因为脚本在到达我想要测试的语句之前需要运行其他可执行文件.有没有一种方法可以在不改变文件系统的情况下从脚本中
我想测试bash脚本的输出,当它依赖的其中一个可执行文件丢失时,所以我想运行该脚本的依赖项“隐藏”而不是其他. PATH = ./script不是一个选项,因为脚本在到达我想要测试的语句之前需要运行其他可执行文件.有没有一种方法可以在不改变文件系统的情况下从脚本中“隐藏”可执行文件?

举一个具体的例子,我想运行this script但是从它隐藏git可执行文件(这是它的主要依赖项),这样我就可以在这些条件下测试它的输出.

您可以使用builtin命令 hash:

hash [-r] [-p filename] [-dt] [name]

Each time hash is invoked,it remembers the full pathnames of the commands specified as name arguments,so they need not be searched for on subsequent invocations. … The -p option inhibits the path search,and filename is used as the location of name. … The -d option causes the shell to forget the remembered location of each name.

通过将不存在的文件传递给-p选项,就好像无法找到命令一样(尽管仍然可以通过完整路径访问它).传递-d撤消效果.

$hash -p /dev/null/git git
$git --version
bash: /dev/null/git: command not found
$/usr/bin/git --version
git version 1.9.5
$hash -d git
$git --version
git version 1.9.5

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读