0

So I have the multi module project and each module is in separate repository on bitbucket.

I try to write bush script that will provide cloning all repos I need, then load the folder with clonning module, run there npm install command and run cd ../ to load the previous folder.

I wrote the next script:

repositories=(
 "ssh://git@bitbucket.repo:22/db/firstRepo.git"
 "ssh://git@bitbucket.repo:22/db/secondRepo.git"
 "ssh://git@bitbucket.repo:22/db/thirdRepo.git"
)

echo "Start clonning"

clone_from_git() {
 echo "Clone from ${1}..."
 git clone ${1}
 echo "Done!"

 cd #some repo

 echo "Start npm install..."
 npm install
 echo "Done!"

 cd ../
}

for repo in "${repositories[@]}"
do
  clone_from_git $repo
done

echo "Cloning done"

But after cloning I need to load cloned folder so, if the repo was cloned from, for example, ssh://git@bitbucket.repo:22/db/firstRepo.git, than I need to run cd firstRepo. Is it possible to split each of repositories elements to get only folder name of clonning repo?

Thanks!

Roman Shmandrovskyi
  • 883
  • 2
  • 8
  • 22
  • 1
    Consider running through http://shellcheck.net/ and addressing the issues it finds. (You'll also want to check for failures -- `git clone "$1" || return`, for example, will cause the function to abort immediately if the `clone` fails -- and consider scoping your `cd` to a subshell -- that way you don't need to do a `cd ..`, and avoid various other related bugs that can come up if, f/e, the `cd` fails but the `cd ..` succeeds) – Charles Duffy Oct 24 '18 at 21:39
  • Something I haven't seen in the linked duplicates to get from `ssh://git@bitbucket.repo:22/db/firstRepo.git` to `firstRepo`: `basename -s '.git' "$repo"`, where `$repo` contains `ssh://git@bitbucket.repo:22/db/firstRepo.git`. – Benjamin W. Oct 24 '18 at 21:40
  • ...note that `${1}` does nothing useful that `$1` doesn't when not necessary to disambiguate syntax, but `"$1"` has several useful properties that `$1` and `${1}` don't -- most particularly, suppression of string-splitting and globbing behaviors. – Charles Duffy Oct 24 '18 at 21:41
  • @BenjaminW., maybe add another answer to the linked question? Though I *do* think I see some answers there that pass an extension to `basename`; https://stackoverflow.com/a/125298/14122, for example (though its quoting is a bit broken). – Charles Duffy Oct 24 '18 at 21:41
  • @CharlesDuffy Let me see if that fits properly somewhere. – Benjamin W. Oct 24 '18 at 21:42

0 Answers0