Last active
March 6, 2023 21:19
-
-
Save wheelerlaw/5ecdefc4a5917237773dfabb5540d1bf to your computer and use it in GitHub Desktop.
A collection of useful bash scripts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# List files that exist only in dir1. https://stackoverflow.com/a/24695424/1902896 | |
comm -23 <(ls dir1 | sort) <(ls dir2 | sort) | |
# Find files with a certain name located in the specified directory | |
find <directory> -name "<file_name>" 2>/dev/null | |
# Find files with a certain name located in the specified directory, then loop over each file name, printing each one. | |
# This starts to run the loop as soon as `find` produces output on stdout. | |
find <directory> -name "<file name>" 2>/dev/null | while read -r file; do | |
echo "$file" | |
done | |
# This waits unil the `find` has completed before beginning to loop. | |
while read -r file; do | |
echo "$file" | |
done <<< "$(find <directory> -name "<file name>" 2>/dev/null)" | |
# Prepend text to a file | |
echo 'task goes here' | cat - todo.txt > temp && mv temp todo.txt | |
# Clean up all non-running Docker things: | |
docker system prunt -a -f | |
# Parse JSON on the shell with `jq`: | |
<some command that outputs JSON to stdout> | jq '.key1.subkey1' | |
# Output the contents of a file with the | |
cat -n <some_file> | |
# Global .gitignore file: | |
# http://egorsmirnov.me/2015/05/04/global-gitignore-file.html | |
echo "\ | |
.idea/ | |
*.iml | |
*.iws | |
*.ipr | |
" >> ~/.gitignore_global && git config --global core.excludesfile ~/.gitignore_global | |
# Enable sources in /etc/apt/sources.list | |
sudo sed 's|# deb-src|deb-src|' -i /etc/apt/sources.list | |
# Find what package a command came from | |
type who | |
# output: who is /usr/bin/who | |
dpkg -S /usr/bin/who | |
# output: coreutils: /usr/bin/who | |
# Import a PGP public key using curl (gpg's http client is riddled with bugs): | |
curl -fsSL "https://keyserver.ubuntu.com/pks/lookup?op=get&options=mr&search=0x2EE0EA64E40A89B84B2DF73499E82A75642AC823" | gpg --import | |
# Normalizing line endings in a git repository: | |
git read-tree --empty # Clean index, force re-scan of working directory | |
git add . | |
git status # Show files that will be normalized | |
git commit -m "Introduce end-of-line normalization" | |
# Generate an Ed25519 key pair. https://medium.com/risan/upgrade-your-ssh-key-to-ed25519-c6e8d60d3c54 | |
ssh-keygen -t ed25519 | |
# Upload an arbitrary Jar file into a Maven repository | |
mvn deploy:deploy-file -DgroupId=come.wheeler -DartifactId=artifact-name -Dversion=1.0 -DgeneratePom=true -Dpackaging=jar -DrepositoryId=server-name-in-settings.xml -Durl=https://repository.host/artifactory -Dfile=jar-file.jar | |
# Determine if a OpenShift pod is ready: | |
until [ "$(oc get pods --selector strimzi.io/name=rtp-demo-cluster-entity-operator -o jsonpath='{.items[<number of pods>].status.containerStatuses[?(@.name == "<container name>")].ready}" 2>/dev/null)' = "true[ true]{<number of containers> - 1}" ]; do sleep 1; done | |
# Set everything to use Nano: | |
sudo update-alternatives --install /usr/bin/editor editor $(which nano) 1 | |
# Redirect to stderr but also save to a variable | |
tee >(jq . | cat 1>&2) | jq -r .access_token | |
# Decode cron expressions | |
# https://crontab.guru | |
# Set Gnome to use sloppy focus | |
gsettings set org.gnome.desktop.wm.preferences focus-mode sloppy | |
# Some useful functions: | |
info() { echo "INFO:" "$@"; } | |
die() { rc=$?; (( $# )) && { for s in "$@"; do >&2 printf "ERROR: %s\n" "$s"; done; }; exit "$rc"; } | |
escape() { sed -e 's/[^^]/[&]/g; s/\^/\\^/g; $!a\'$'\n''\\n' <<< "$@" | tr -d '\n'; } | |
read_literal() { IFS= read -r -d '' "$1" || :; } | |
# Add usage information to a makefile | |
#help: ## Display this help | |
# @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | |
# Do something in bash for a period of time and then quit either if it returns successfully or a timeout period expires. | |
# Timeout approach: | |
WAIT_TIME=10 | |
check_func() { | |
while :; do | |
if curl www.example.com; then | |
return 0 | |
fi | |
sleep $SLEEP_TIME | |
done | |
} | |
export -f check_func | |
trap 'kill -INT -$pid' INT | |
timeout $WAIT_TIME bash -c "check_func" & | |
pid=$! | |
wait $pid | |
# For-loop approach | |
WAIT_TIME=10 | |
SLEEP_TIME=1 | |
count="$(( WAIT_TIME / SLEEP_TIME ))" | |
for ((i=1; i<=count; i++)); do | |
if curl www.example.com; then | |
break | |
fi | |
sleep $SLEEP_TIME | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment