庆典 - 为您在文件路径字符串 [英] Bash - check for a string in file path
问题描述
我如何检查在bash中的文件路径的字符串?我想:
How can I check for a string in a file path in bash? I am trying:
if [[$(echo "${filePathVar}" | sed 's#//#:#g#') == *"File.java"* ]]
更换所有向前冒号(:)路径斜杠。它不工作。 Bash是看到文件路径字符串作为文件的路径和引发错误没有这样的文件或目录。我们的目的是为它看作为一个字符串的文件路径。
to replace all forward slashes with a colon (:) in the path. It's not working. Bash is seeing the file path string as a file path and throws the error "No such file or directory". The intention is for it to see the file path as a string.
例如:filePathVar可能是
Example: filePathVar could be
** / myloc / src目录/ File.java
**/myloc/src/File.java
在这种情况下,检查应该返回真
in which case the check should return true.
的请注意,我写这个剧本一詹金斯里面工作作为构建的一步。的
更新为15年12月15日
下面的返回未找到
,这是不对的。
The following returns Not found
, which is wrong.
#!/bin/bash
sources="**/src/TESTS/A.java **/src/TESTS/B.java"
if [[ "${sources}" = ~B.java[^/]*$ ]];
then
echo "Found!!"
else
echo "Not Found!!"
fi
下面的返回找到
这也也是错误的(除去周围比较 =
的空间)。
The following returns Found
which also also wrong (removed the space around the comparator =
).
#!/bin/bash
sources="**/src/TESTS/A.java **/src/TESTS/C.java"
if [[ "${sources}"=~B.java[^/]*$ ]];
then
echo "Found!!"
else
echo "Not Found!!"
fi
比较操作显然是行不通的。
The comparison operation is clearly not working.
推荐答案
据曾在下面一个简单的方法:
It worked in a simpler way as below:
#!/bin/bash
sources="**/src/TESTS/A.java **/src/TESTS/B.java"
if [[ $sources == *"A.java"* ]]
then
echo "Found!!"
else
echo "Not Found!!"
fi
这篇关于庆典 - 为您在文件路径字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!