Created
September 14, 2018 09:14
-
-
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
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
| 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