我在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_IDUnfortunately, 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.xmlThe 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.
更多推荐
发布评论