如果检测用户的路径有一个特定的目录中它 [英] Detect if user's path has a specific directory in it
本文介绍了如果检测用户的路径有一个特定的目录中它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用 /斌/庆典
,我将如何检测,如果一个用户在其$ PATH变量有一个特定的目录?
例如
如果[-p$ HOME / bin中];然后
回声你缺少路径〜/ bin中,你可能需要添加它。
其他
回声你的路径是否正确
科幻
解决方案
很简单的东西和天真:
回声$ PATH| grep的-q无论&功放;&安培;回声发现
无论在哪里就是你要搜索的内容。取而代之的&放大器;&放大器;
你可以把 $
到一个变量或使用适当的如果
语句。
限制包括:
- 上面将匹配的较大路径子(试穿bin的匹配,它可能会发现,尽管本是不是在你的路径,/ bin中和/ usr /纸槽)
- 以上将不自动扩展喜欢〜快捷键
或使用Perl的一行代码:
的perl -e'出口(!(grep的(M {^的/ usr / bin中$},拆分(:,$ ENV {PATH})))0) '和;&安培;回声发现
这还是有它不会做任何shell扩展的限制,但如果一个子串匹配它不会失败。 (以上比赛的/ usr / bin中
,在不明确的情况下)。
With /bin/bash
, how would I detect if a user has a specific directory in their $PATH variable?
For example
if [ -p "$HOME/bin" ]; then
echo "Your path is missing ~/bin, you might want to add it."
else
echo "Your path is correctly set"
fi
解决方案
Something really simple and naive:
echo "$PATH"|grep -q whatever && echo "found it"
Where whatever is what you are searching for. Instead of &&
you can put $?
into a variable or use a proper if
statement.
Limitations include:
- The above will match substrings of larger paths (try matching on "bin" and it will probably find it, despite the fact that "bin" isn't in your path, /bin and /usr/bin are)
- The above won't automatically expand shortcuts like ~
Or using a perl one-liner:
perl -e 'exit(!(grep(m{^/usr/bin$},split(":", $ENV{PATH}))) > 0)' && echo "found it"
That still has the limitation that it won't do any shell expansions, but it doesn't fail if a substring matches. (The above matches "/usr/bin
", in case that wasn't clear).
这篇关于如果检测用户的路径有一个特定的目录中它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文