Last active
February 24, 2024 11:23
-
-
Save mevanlc/c50f74db32cf3ed0b610808906449282 to your computer and use it in GitHub Desktop.
Helper script to install z.sh (jump around) - https://github.com/rupa/z
This file contains hidden or 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
#!/bin/sh | |
SOURCE_URL="https://raw.githubusercontent.com/rupa/z/master/z.sh" | |
OUTPUT_LOC="$HOME/.z.sh" | |
usage_doc="Usage: $0 [-h] [-u url] [-o file] | |
-h this help | |
-u <url> download from <url> | |
default: $SOURCE_URL | |
-o <file> download to <file> | |
default: $OUTPUT_LOC" | |
main() { | |
parse_args "$@" | |
do_download | |
ask_edit_rcfile | |
} | |
do_download() { | |
[ -f "$OUTPUT_LOC" ] && err_exit "output file already exists: $OUTPUT_LOC" | |
if [ -x "$(command -v wget)" ]; then | |
wget -qO "$OUTPUT_LOC" "$SOURCE_URL" || err_exit "wget failed" | |
elif [ -x "$(command -v curl)" ]; then | |
curl -so "$OUTPUT_LOC" "$SOURCE_URL" || err_exit "curl failed" | |
else | |
err_exit "$0 requires curl or wget to be installed" | |
fi | |
[ -f "$OUTPUT_LOC" ] || err_exit "could not create file: $OUTPUT_LOC" | |
echo "Created: $OUTPUT_LOC" | |
} | |
ask_edit_rcfile() { | |
sh_name=$(basename "$SHELL") | |
[ "$sh_name" = "bash" ] && rcfile="$HOME/.bashrc" | |
[ "$sh_name" = "zsh" ] && rcfile="$HOME/.zshrc" | |
if [ -n "$rcfile" ]; then | |
printf "Add $OUTPUT_LOC to %s? [y/n] " "$rcfile" | |
read -r yn | |
case $yn in | |
[Yy]*) echo "source '$OUTPUT_LOC'" >>"$rcfile" && | |
echo "Added to $rcfile" ;; | |
*) ;; | |
esac | |
fi | |
} | |
parse_args() { | |
while getopts "hu:o:" opt; do | |
case $opt in | |
u) SOURCE_URL="$OPTARG" ;; | |
o) OUTPUT_LOC="$OPTARG" ;; | |
*) usage ;; | |
esac | |
done | |
} | |
usage() { echo "$usage_doc" >&2; } | |
err_exit() { | |
echo "Error: $1" >&2 | |
exit "${2:-1}" | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment