使用grep来检查隐藏目录中的文件内容(using grep to check file content in hidden directory)
我在shell脚本中具有以下功能,旨在处理〜/ .m2 / settings.xml中服务器设置的基本grep检查:
SETTIGS_FILE="~/.m2/settings.xml" SETTIGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTIGS_FILE" if grep -q "$1" "$SETTIGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED:\n\t$1 not found in ${SETTIGS_FILE}! fail "Fail :("; fi return 0 } check_settings $SETTIGS_SERVER_ID不幸的是,这导致了以下消息:
grep <id>ossrh</id> ~/.m2/settings.xml grep: ~/.m2/settings.xml: o 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:
SETTIGS_FILE="~/.m2/settings.xml" SETTIGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTIGS_FILE" if grep -q "$1" "$SETTIGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED:\n\t$1 not found in ${SETTIGS_FILE}! fail "Fail :("; fi return 0 } check_settings $SETTIGS_SERVER_IDUnfortunately, this results in the following message:
grep <id>ossrh</id> ~/.m2/settings.xml grep: ~/.m2/settings.xml: o 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, cidering this question) or whether it is an issue with the argument I'm passing grep (likely, cidering how new I am to shell scripting)
最满意答案
当变量值在引号中时, ~/前缀不会扩展到您的主目录。 将第一行更改为:
SETTIGS_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:
SETTIGS_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.
#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格
推荐阅读
留言与评论(共有 16 条评论) |
本站网友 鹿血晶 | 28分钟前 发表 |
使用grep来检查隐藏目录中的文件内容(using grep to check file content in hidden directory) 我在shell脚本中具有以下功能 | |
本站网友 怎样去痘印 | 6分钟前 发表 |
o such file or directory DEPLOY FAILED | |
本站网友 马三立的逗你玩 | 8分钟前 发表 |
o such file or directory DEPLOY FAILED | |
本站网友 跨境通电子商务平台 | 13分钟前 发表 |
\n\t$1 not found in ${SETTIGS_FILE}! fail "Fail | |
本站网友 五大国有银行 | 28分钟前 发表 |
~/.m2/settings.xml | |
本站网友 国内游 | 10分钟前 发表 |
o such file or directory DEPLOY FAILED | |
本站网友 贵阳楼市 | 5分钟前 发表 |
SETTIGS_FILE="~/.m2/settings.xml" SETTIGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTIGS_FILE" if grep -q "$1" "$SETTIGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED | |
本站网友 中国工业总产值 | 7分钟前 发表 |
o such file or directory DEPLOY FAILED | |
本站网友 玲珑公园 | 5分钟前 发表 |
( Meanwhile | |
本站网友 斜视图 | 22分钟前 发表 |
o such file or directory DEPLOY FAILED | |
本站网友 南京人流医院 | 21分钟前 发表 |
("; fi return 0 } check_settings $SETTIGS_SERVER_ID Unfortunately | |
本站网友 金昌二手房 | 21分钟前 发表 |
SETTIGS_FILE="~/.m2/settings.xml" SETTIGS_SERVER_ID="<id>ossrh</id>" check_settings () { echo grep "$1" "$SETTIGS_FILE" if grep -q "$1" "$SETTIGS_FILE"; then echo "Server is set up...\n"; else echo "DEPLOY FAILED | |
本站网友 男人的jj | 30分钟前 发表 |
考虑我是如何新的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 | |
本站网友 北京西装定制 | 26分钟前 发表 |
cidering how new I am to shell scripting) 最满意答案 当变量值在引号中时 | |
本站网友 北京广安门医院挂号 | 30分钟前 发表 |
\n\t$1 not found in ${SETTIGS_FILE}! fail "Fail |