Jenkins bash脚本-从查找命令结果中删除目录路径



我需要在我的dist目录中搜索最小化的。js和。css文件。

我有一个bash脚本与查找cmd,像这样:

for path in $(/usr/bin/find dist/renew-online -maxdepth 1 -name "*.js" -or -name "*.css" -type f); do
# URL of the JavaScript file on the web server
url=$linkTarget/$path
echo "url=$linkTarget/$path"

其中linkTarget为:http://uat.xxxx.com/renew-online.

我想将dist/renew-online的缩小文件附加到linkTarget,例如:

http://uat.xxxx.com/renew-online/main-es2015.cf7da54187dc97781fff.js

但是我一直得到:http://uat.xxxx.com/renew-online/dist/renew-online/main-es2015.cf7da54187dc97781fff.js

我也尝试过-maxdepth 0,但无法获得正确的url -新手在脚本!

希望你们中有人能帮忙,谢谢你的时间

这可以通过使用'find'命令来实现:

/usr/bin/find dist/renew-online -maxdepth 1 ( -name "*.js" -o -name "*.css" ) -type f -printf "$linkTarget/%fn"

还建议将'或'语句隔离在圆括号内。

这是一个bash问题,而不是jenkins问题,您有多种方法可以做到。

如果你所有的文件都在一个路径中,而你实际上是在强制使用深度,你可以使用cut

for path in $(/usr/bin/find dist/renew-online -maxdepth 1 -name "*.js" -or -name "*.css" -type f | cut -d'/' -f2); do

另一方面,这里的https://serverfault.com/questions/354403/remove-path-from-find-command-output通过使用-printf '%fn'

也请注意,对loop使用find in a是脆弱的,建议使用whilehttps://github.com/koalaman/shellcheck/wiki/SC2044

编辑cut中使用的字段取决于查找语法中的文件夹。最准确的方法是上面的serverfault链接中的方法

最新更新