Shell script not running in git bash

61 Views Asked by At

I have created a shell script that automatically downloads local copies of my repositories in github. The script utilizes ssh to clone the repos so it gets both private and public ones. Here is the script:

#!/usr/bin/env bash

GITHUB_USERNAME="andwati"

DESTINATION_FOLDER=~/GitHub


clone_repositories() {
    local parent_dir=$1
    local url=$2

    # Extract the repository name from the URL
    local repo_name=$(basename "${url%.git}")

    # Clone the repository into the specified folder
    git clone "$url" "$parent_dir/$repo_name"

    # Move into the cloned repository folder
    cd "$parent_dir/$repo_name" || exit

    # If there are submodules, initialize and update them
    if [ -f ".gitmodules" ]; then
        git submodule init
        git submodule update
    fi

    # Recursively clone submodules if any
    if [ -f ".gitmodules" ]; then
        git submodule foreach --recursive "$0" "$parent_dir/$repo_name"
    fi

    # Move back to the parent folder
    cd "$parent_dir" || exit
}

repositories=$(curl -s "https://api.github.com/users/$GITHUB_USERNAME/repos?per_page=100" | grep -o 'git@[^"]*')

mkdir -p $DESTINATION_FOLDER


cd "$DESTINATION_FOLDER" || exit

for repository in $repositories; do
    clone_repositories "$DESTINATION_FOLDER" "$repository"
done

I usually call the script in git bash by running ./repo_sync.sh. Now the command exits after a few seconds without any feedback . I have also tried using bash ./repo_sync.sh

1

There are 1 best solutions below

0
Ian Andwati On

Running the script by calling bash -x ./repo_sync.sh makes it to work as intended as suggested by @Joe.