Skip to content

Instantly share code, notes, and snippets.

@SomajitDey
Last active September 26, 2021 15:50
Show Gist options
  • Select an option

  • Save SomajitDey/49020a74635914f402c829b79b746a16 to your computer and use it in GitHub Desktop.

Select an option

Save SomajitDey/49020a74635914f402c829b79b746a16 to your computer and use it in GitHub Desktop.
Install files locally. Download and install: chmod +x linstall; ./linstall linstall
#!/usr/bin/env bash
# Brief: Install locally without sudo
# Usage: install [file-path or download-link]
file="${@}"
[[ -n "${file}" ]] || read -rep'Drag-n-drop file or put download link here: ' file
file="${file/#\"/}" ; file="${file/%\"/}" # Quote (") removal, if any
if [[ "${file}" =~ ^http[s]?://.*$ ]];then
tmp=$(mktemp -d .linstallXXXXX); trap "rm -rf ${tmp}" exit
(cd "${tmp}" ; curl -sfSLNO "${file}") || exit 1
file="${tmp}/$(ls ${tmp})"
else
convert="$(wslpath "${file}" 2>/dev/null)"
file="${convert:-"${file}"}"
file="${file/#~\//${HOME}\/}" # Tilde expansion
file="${file/#~-\//${OLDPWD}\/}" # Tilde expansion
fi
[[ -f "${file}" ]] || { echo "File: ${file} doesn't exist" >&2; exit 1;}
chmod +x "${file}" || exit 1
dest="${HOME}/.bin"
cmd="export PATH=\"\${PATH}:${dest}\""
mkdir -p "${dest}"
read -rep 'Install as what? Ans: ' -i "${file##*/}" install_as
cp -i "${file}" "${dest}/${install_as}" &&\
(grep -q "^${cmd}" "${HOME}/.bashrc" || echo "${cmd}" >> "${HOME}/.bashrc") && eval "${cmd}" && \
echo "${install_as} installed locally"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment