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

jenkins bash script - remove directory path from find command results

我需要在我的 dist 目录中搜索 jenkins 中缩小的 .js 和 .css 文件。

我有一个带有查找命令的 bash 脚本,如下所示:

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 - 脚本新手!

希望你们中的一个能帮忙,谢谢你的时间

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

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

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 '%f\n'

的用法

另请注意,find在for loop中的用法是脆弱的,建议使用while https://github.com/koalaman/shellcheck/wiki/SC2044

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

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

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

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