使用grep来检查隐藏目录中的文件内容(using grep to check file content in hidden directory)

系统教程 行业动态 更新时间:2024-06-14 17:03:54
使用grep来检查隐藏目录中的文件内容(using grep to check file content in hidden directory)

我在shell脚本中具有以下功能,旨在处理〜/ .m2 / settings.xml中服务器设置的基本grep检查:

SETTINGS_FILE="~/.m2/settings.xml" SETTINGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTINGS_FILE" if grep -q "$1" "$SETTINGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED:\n\t$1 not found in ${SETTINGS_FILE}! fail "Fail :("; fi return 0 } check_settings $SETTINGS_SERVER_ID

不幸的是,这导致了以下消息:

grep <id>ossrh</id> ~/.m2/settings.xml grep: ~/.m2/settings.xml: No such file or directory DEPLOY FAILED: <id>ossrh</id> not found in ~/.m2/settings.xml! Fail :(

同时,在bash中使用grep "<id>ossrh</id>" ~/.m2/settings.xml返回预期的<id>ossrh</id> 。

我想知道这是否是一个可见性问题 - grep是否可以在隐藏的目录中看到事情(不太可能,考虑到这个问题 )或者它是否与我传递grep的参数有关(可能,考虑我是如何新的shell脚本)

I have the following function in a shell script meant to handle some basic grep checks for a server setting in ~/.m2/settings.xml:

SETTINGS_FILE="~/.m2/settings.xml" SETTINGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTINGS_FILE" if grep -q "$1" "$SETTINGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED:\n\t$1 not found in ${SETTINGS_FILE}! fail "Fail :("; fi return 0 } check_settings $SETTINGS_SERVER_ID

Unfortunately, this results in the following message:

grep <id>ossrh</id> ~/.m2/settings.xml grep: ~/.m2/settings.xml: No such file or directory DEPLOY FAILED: <id>ossrh</id> not found in ~/.m2/settings.xml! Fail :(

Meanwhile, just using grep "<id>ossrh</id>" ~/.m2/settings.xml in the bash returns <id>ossrh</id>, which is expected.

I'm wondering if this is an issue of visibility- whether grep can see things in hidden directories (unlikely, considering this question) or whether it is an issue with the argument I'm passing grep (likely, considering how new I am to shell scripting)

最满意答案

当变量值在引号中时, ~/前缀不会扩展到您的主目录。 将第一行更改为:

SETTINGS_FILE=~/.m2/settings.xml

对此的线索是错误消息包含~前缀。 如果前缀已扩展,则错误消息将包含目录的绝对路径名。

The ~/ prefix is not expanded to your home directory when the variable value is in quotes. Change the first line to:

SETTINGS_FILE=~/.m2/settings.xml

The clue to this is that the error message includes the ~ prefix. If the prefix had been expanded, the error message would have contained the absolute pathname to your directory.

更多推荐

本文发布于:2023-04-24 14:25:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/bd0de931dd27153b2d693cbee9e09579.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   内容   目录中   grep   check

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!