Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Macos bash:grep仅返回;是一个目录";搜索存在的文件时_Macos_Bash - Fatal编程技术网

Macos bash:grep仅返回;是一个目录";搜索存在的文件时

Macos bash:grep仅返回;是一个目录";搜索存在的文件时,macos,bash,Macos,Bash,直到最近,我还能够让脚本在继续之前确保某个目录中存在某个文件 现在,脚本要么找不到文件,要么在grep工作时,在完成时返回“grep:/Users/user/Downloads:是一个目录” 该文件存在于目录中,但grep不想再与之交互。这是我正在处理的事情: if grep -q 'file.bin' ~/Downloads; then echo "It works!" exit 1 fi 任何建议都将不胜感激。谢谢。您要求grep在名为~/Downloads…的文件中搜索字符串(file.

直到最近,我还能够让脚本在继续之前确保某个目录中存在某个文件

现在,脚本要么找不到文件,要么在grep工作时,在完成时返回“grep:/Users/user/Downloads:是一个目录”

该文件存在于目录中,但grep不想再与之交互。这是我正在处理的事情:

if grep -q 'file.bin' ~/Downloads; then echo "It works!" exit 1 fi

任何建议都将不胜感激。谢谢。

您要求
grep
在名为
~/Downloads
…的文件中搜索字符串(
file.bin
),该文件是一个目录。因此,您看到的错误是准确的;grep只对文件而不是目录进行操作

如果要查看文件是否存在,可能只需要对文件使用标准shell测试:

if [ -f ~/Downloads/file.bin ]; then
    echo "It exists!"
fi
您可以使用
grep
查看文件中是否存在字符串:

if grep -q "a string" ~/Downloads/file.bin; then
    echo "The file contains the string"
fi

谢谢你的回答!