Skip to content

Instantly share code, notes, and snippets.

@rbcmgs
Last active May 1, 2024 18:25
Show Gist options
  • Select an option

  • Save rbcmgs/8e12ccab41114d95c44a41a3ad8d77f2 to your computer and use it in GitHub Desktop.

Select an option

Save rbcmgs/8e12ccab41114d95c44a41a3ad8d77f2 to your computer and use it in GitHub Desktop.
A bash script that accepts an HTTPS URL of a GitHub GIST as a command-line argument and clones the gist into the current directory without creating nested directories. Suitable for automatic or batch gist cloning tasks when you want all your scripts to be located inside one directory.
#!/bin/bash
# This script clones a GitHub gist specified by a URL provided as an argument.
# It then makes any cloned shell scripts executable and moves all files to the current directory, avoiding a nested directory structure.
# Check for correct number of arguments
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <gist-clone-url>"
exit 1
fi
# Assign the first argument as the gist URL
GIST_URL=$1
TEMP_DIR="temp-dir" # Temporary directory name for cloning
# Clone the gist into the temporary directory
git clone $GIST_URL $TEMP_DIR
# Check if the clone was successful
if [ -d "$TEMP_DIR" ]; then
# Check and make all shell script files executable
if compgen -G "$TEMP_DIR/*.sh" > /dev/null; then
chmod +x $TEMP_DIR/*.sh
fi
# Move all the files from the temporary directory to the current directory
mv $TEMP_DIR/* .
# Remove the temporary directory
rm -rf $TEMP_DIR
echo "Gist from $GIST_URL has been successfully cloned and files moved to the current directory."
else
echo "Failed to clone gist from $GIST_URL. Check the URL format and your internet connection."
fi
# Instructions:
# - Make sure you have 'git' installed on your system.
# - Run this script with the clone URL of the gist as an argument from the directory where you want the files to be placed.
# - Example of script execution:
# chmod +x clone_gist_flat_parameterized.sh
# ./clone_gist_flat_parameterized.sh https://gist.github.com/username/gistid.git
# Note:
# This script does not handle potential directory or file conflicts in the current directory.
# Make sure the current directory is ready for new files, or adjust the script to handle such situations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment