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

BASH找不到使用?/ in path存在的隐藏文件

发布时间:2020-12-15 21:18:41 所属栏目:安全 来源:网络整理
导读:我有一个BASH脚本,正在我的主目录中查找隐藏文件.当我在尝试查找文件时指定绝对路径/home/user/.foo时,脚本会找到它,但如果我使用?/ .foo,则脚本无法找到该文件.有人可以解释为什么会发生这种情况以及我能找到该文件的其他方式.我希望能够在不同用户的主目录
我有一个BASH脚本,正在我的主目录中查找隐藏文件.当我在尝试查找文件时指定绝对路径/home/user/.foo时,脚本会找到它,但如果我使用?/ .foo,则脚本无法找到该文件.有人可以解释为什么会发生这种情况以及我能找到该文件的其他方式.我希望能够在不同用户的主目录中找到该文件,而不仅仅是我自己的目录.

很抱歉没有提供代码片段,但现在是

文件= “?/ .foo” 此时

[-f $file]&&回声“foo存在!”

然而这有效

文件= “家庭/用户/ .foo” 此时

[-f $file]&&回声“foo存在!”

解决方法

这是因为当代字号是单词中第一个未引用的字符时,shell仅执行代字号扩展.在

file="~/.foo"

波浪号用双引号引用,因此保持未展开状态.最好的做法是当代字号扩展到你的主目录时总是使用$HOME(而不是像?用户那样的其他用户).

来自POSIX的直涂料:

2.6.1 Tilde Expansion

A “tilde-prefix” consists of an unquoted <tilde> character at the
beginning of a word,followed by all of the characters preceding the
first unquoted <slash> in the word,or all the characters in the word
if there is no <slash>. In an assignment (see XBD Variable
Assignment),multiple tilde-prefixes can be used: at the beginning of
the word (that is,following the <equals-sign> of the assignment),
following any unquoted <colon>,or both. A tilde-prefix in an
assignment is terminated by the first unquoted <colon> or <slash>. If
none of the characters in the tilde-prefix are quoted,the characters
in the tilde-prefix following the <tilde> are treated as a possible
login name from the user database. A portable login name cannot
contain characters outside the set given in the description of the
LOGNAME environment variable in XBD Other Environment Variables. If
the login name is null (that is,the tilde-prefix contains only the
tilde),the tilde-prefix is replaced by the value of the variable
HOME. If HOME is unset,the results are unspecified. Otherwise,the
tilde-prefix shall be replaced by a pathname of the initial working
directory associated with the login name obtained using the getpwnam()
function as defined in the System Interfaces volume of POSIX.1-2008.
If the system does not recognize the login name,the results are
undefined.

The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.

(编辑:李大同)

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

    推荐文章
      热点阅读