Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I need to search in my dist directory for minified .js and .css files within jenkins.

I have a bash script with a find cmd, like this:

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"

Where linkTarget is: http://uat.xxxx.com/renew-online.

I want to attach the minified files form dist/renew-online to the linkTarget, for example:

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

BUT I keeping getting: http://uat.xxxx.com/renew-online/dist/renew-online/main-es2015.cf7da54187dc97781fff.js

I've tried with -maxdepth 0 also but can't get the correct url - newbie at scripts!

Hopefully one of you guys can help, thanks your time

question from:https://stackoverflow.com/questions/65939232/jenkins-bash-script-remove-directory-path-from-find-command-results

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.2k views
Welcome To Ask or Share your Answers For Others

1 Answer

This is more a bash question than a jenkins one and you have multiple ways to do it.

If all your files are in a single path, and actually you are forcing with the depth, you can use a cut

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

In the other hand the here https://serverfault.com/questions/354403/remove-path-from-find-command-output by the usage of -printf '%f '

Please note as well that the usage of find in a for loop is fragile and it is recommended to use a while https://github.com/koalaman/shellcheck/wiki/SC2044

EDIT the field used in cut depends on the folders you have in your find syntax. The most accurate way is the one in the serverfault link above


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...