Skip to content

Instantly share code, notes, and snippets.

@BenDMyers
Last active November 26, 2019 04:06
Show Gist options
  • Save BenDMyers/e2a15dce6bd4334d52fece097b15fbf1 to your computer and use it in GitHub Desktop.
Save BenDMyers/e2a15dce6bd4334d52fece097b15fbf1 to your computer and use it in GitHub Desktop.
Bash Scripts Bootstrap
# Create new directory and jump inside it
# USAGE: md <new directory name>
function md() {
mkdir "$1"
cd "$1"
}
# Modify and re-source your .bashrc
function bashrc() {
nano ~/.bashrc;
source ~/.bashrc;
}
# Get the number of lines in a file
# USAGE: lines <filename>
function lines() {
echo $(wc -l < "$1") | xargs
}
# Kill Docker container, rebuild project, and spin up new Docker container
function rebuild() {
docker-compose down;
# Replace with some other build command
./gradlew clean build copyEar;
docker-compose up --build;
}
# Add a new alias
# USAGE: a <new alias> <command>
function a() {
if [[ $# -ne 2 ]]
then
echo "USAGE: a <alias> <expression>"
return 0
elif [[ -n "$(alias $1 2>/dev/null)" ]]
then
echo "Alias already exists!"
return 0
fi
echo -e "alias $1=\"$2\"
$(cat ~/.bashrc)" > ~/.bashrc
source ~/.bashrc
echo "Alias was added successfully!"
}
# Alias the last command run.
# DEPENDS ON a()
# USAGE: al <new alias>
function al() {
prev_cmd=$(echo $(fc -ln -1) | xargs)
if [[ $# -ne 1 ]]
then
echo "USAGE: al <alias>"
return 0
elif [[ -n "$(alias $1 2>/dev/null)" ]]
then
echo "Alias already exists!"
return 0
fi
a "$1" "$prev_cmd"
}
# Creates an alias to jump to current directory
# DEPENDS ON a()
# USAGE: ad <new alias>
function ad() {
dir=$(pwd)
a "$1" "cd $dir"
}
# Opens a specific Wikipedia article in the user's browser
# USAGE: w -- Takes you to Wikipedia homepage
# USAGE: w <query> -- Searches Wikipedia for the query
function w() {
wiki_url="https://www.wikipedia.org"
if [[ $# -ne 0 ]]
then
# Use $* to get all args, joined by first character of $IFS (a space, by default)
wiki_url="$wiki_url/wiki/?search=$*"
fi
open "$wiki_url"
}
# Checks out a branch, whether it already exists or not,
# and allows you to specify whether to set upstream (with "--up" or "^")
# USAGE: b -- List all branches
# USAGE: b <branch name> -- Checkout branch, or create a new one if it doesn't exist
function b() {
if [ $# -eq 0 ]
then
git branch
else
branch_name="$1";
set_upstream=false;
# Process "--up"/"^" flag
if [ "$1" = "--up" ] || [ "$1" = "^" ]
then
branch_name="$2";
set_upstream=true;
else
if [ "$2" = "--up" ] || [ "$2" = "^" ]
then
set_upstream=true;
fi
fi
# Create new branch if it doesn't exist, or check it out if it does
if [ -z "$(git rev-parse --verify --quiet $branch_name)" ]
then
git checkout -b $branch_name;
else
git checkout $branch_name;
fi
# Set upstream if so specified
if [ "$set_upstream" = "true" ]
then
git push --set-upstream origin $branch_name;
fi
fi
}
# Sets the upstream for the current branch
function up() {
branch_name=$(git symbolic-ref --short -q HEAD);
git push --set-upstream origin $branch_name;
}
# Delete branch(es)
# USAGE: prune <branch names separated by spaces> -- Delete specified branches
function prune() {
for branch_name in "$@"
do
if [ -z "$(git rev-parse --verify --quiet $branch_name)" ]
then
echo "Could not delete branch $branch_name. It does not exist.";
else
git branch -D $branch_name;
fi
done
}
# Opens a specific npm package in the user's browser
# USAGE: npmjs -- Opens npm homepage
# USAGE: npmjs <package name> -- Opens package on npm
function npmjs() {
npm_url="https://www.npmjs.com/package"
if [[ $# -ne 0 ]]
then
npm_url="$npm_url/$1"
fi
open "$npm_url"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment