如何如发现不是空的显示真 [英] How to display true if find is not empty

查看:108
本文介绍了如何如发现不是空的显示真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新打坏。我刚刚从上周开始学习。我试图寻找一个文件名。
我怎么能当找到该文件显示一条消息?

这是我有什么,但它口口声声说不

 回声[Enter]键的文件名'
阅读findFile如果[[-n $(查找/ $ HOME型的F -namefindFile)]
然后
    回声是
其他
    回声'不'
科幻


解决方案

的几个问题:


  1. 使用 VAR = 读VAR 定义一个变量时,但 $ VAR 使用时。

  2. 没有理由继续找到文件后搜索,所以做类似下面,其中找到 -quit 找到一个单一的文件,并返回它的结果后, -print

 #!/斌/庆典
回声[Enter]键的文件名'
阅读findFile
如果[[-f $(发现$ HOME型的F -name$ findFile-print -quit)];然后
    回声是
其他
    回声'不'
科幻

请注意,每<一个href=\"http://stackoverflow.com/questions/22805204/how-to-display-true-if-find-is-not-empty/22805527#comment34778178_22805527\">Adaephon's评论,虽然 / $ HOME 前是不需要的,这是没有错的和文件仍然会被发现。

I am very new to bash. I have just started to learn last week. I am trying to search for a file name. How can I display a message if the file is found?

this is what i have but it keeps saying 'no'

echo ' [Enter] a file name '    
read findFile

if [[ -n $(find /$HOME -type f -name "findFile") ]]
then
    echo 'yes'
else
    echo 'no'
fi

解决方案

A few issues:

  1. Use var= or read var when defining a variable, but $var when using it.
  2. There is no reason to keep searching after finding a file, so do something like below, where find will -quit after finding a single file and return it as a result of the -print

.

#!/bin/bash
echo ' [Enter] a file name '    
read findFile
if [[ -f $(find "$HOME" -type f -name "$findFile" -print -quit) ]]; then
    echo 'yes'
else
    echo 'no'
fi

Note, per Adaephon's comment, although the / is not needed in front of $HOME, it's not wrong and the files will still be found .

这篇关于如何如发现不是空的显示真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆