Skip to content

Instantly share code, notes, and snippets.

@kapb14
Created September 14, 2018 09:14
Show Gist options
  • Select an option

  • Save kapb14/287ce6cf2bc373349f6a71c2a3c144c7 to your computer and use it in GitHub Desktop.

Select an option

Save kapb14/287ce6cf2bc373349f6a71c2a3c144c7 to your computer and use it in GitHub Desktop.
Bash function to select, edit and then source one of .bashrc* files in user's home directory
function editrc () {
if [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]] || [[ "$1" == "help" ]]; then
cat <<EOF
Usage: editrc [--help|-h|help] [ID]
[ID] - first column in output (index number)
It may be executed without arguments and select the file with interactive prompt.
EOF
return
fi
rc_scripts=$(find ~ -maxdepth 1 -name ".bash*" -type f | egrep -v "_history|*.save")
n=0
function listrc () {
for f in ${rc_scripts}; do
((n++))
printf '%s\t%s\n' "${n}" "${f}"
done
}
if [[ -z $1 ]]; then
rc_script=$(listrc | percol | awk $'{print $2}')
else
if [[ $1 =~ ^-?[0-9]+$ ]]; then
rc_script=$(listrc | grep $1 | awk $'{print $2}')
else
echo "ERROR: wrong argument passed. It must be an integer. You provided: $1"
return
fi
fi
nano -Y bash ${rc_script}
source ${rc_script} && echo ":: rc script: ${rc_script} was edited and sourced. ::"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment