[英]Linux Find Binary File
I am attempting to find a binary file in a Linux system using something like this: 我正在尝试使用类似以下内容的Linux系统中的二进制文件:
if [ -f `which $1` ] then
echo "File Found"
else
echo "File not Found"
fi
while the code works fine the problem is "which" will return a null operator which BASH interprets as something existing so a file always comes back found. 虽然代码可以正常工作,但问题是“哪个”将返回空运算符,BASH会将其解释为存在的运算符,因此总会找到文件。 Any suggestions would be great.
任何建议都很好。
Thanks 谢谢
After a bit more thought, there is no reason to use [[ ]]
(or [ ]
for that matter). 再多考虑一下,就没有理由使用
[[ ]]
(或使用[ ]
了)。 There is even no reason to use command substitution either $()
$()
甚至都没有理由使用命令替换
if which "$1" > /dev/null 2>&1; then
echo "found"
else
echo "not found"
fi
If you're using bash
then please use the [[ ]]
construct. 如果您使用的是
bash
请使用[[ ]]
构造。 One of the benefits (among many) is that it doesn't have this problem 好处(其中很多)之一是它没有此问题
[[ -f $(which $1) ]] && echo found
Also, `` is deprecated, use $()
instead 此外,``已弃用,请改用
$()
if [ `which "$1"` != "" ]; then
which
将不会返回""
当它找到二进制。
I like 'hash' for this (if you're a bash user..) (and it's actually more portable behavior than which) 我为此喜欢“哈希”(如果您是bash用户..)(实际上比它更可移植)
hash blahblah
bash: hash: lklkj: not found bash:哈希:lklkj:找不到
hash /bin/ls <-- silently successful
This method works on Linux and OSX similarly, where-as 'which' has different behavior. 此方法在Linux和OSX上类似地工作,其中“哪”具有不同的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.