Last active
April 7, 2022 14:35
-
-
Save dangarbri/ba337448cf54282f51bb6b5682d143d9 to your computer and use it in GitHub Desktop.
Automatically add new folders to your path without needed to manually edit a script
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
#!/usr/bin/env bash | |
# Copyright (c) 2022 Daniel Garcia-Briseno | |
# Written: 2022-04-06 | |
# Updated: 2022-04-07 - added customization with the environment variable | |
# ADDTOPATH_TARGET | |
# - Added ability to request sudo permission | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, but | |
# WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
# General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <https://www.gnu.org/licenses/>. | |
# By default this script will modify /etc/profile with the paths you | |
# provide. You can change this behaviour by specifying the target | |
# script to update in the environment variable ADDTOPATH_TARGET | |
set -e | |
# Alias inputs | |
target_folder=$1 | |
description=$2 | |
needs_privileges=0 | |
file_to_update=/etc/profile | |
# Check if the user has set the ADDTOPATH_TARGET environment | |
# variable and it is a valid file. If not, then default to | |
# /etc/profile | |
update_file_to_update() { | |
# If $ADDTOPATH_TARGET is set and the target file exists | |
if [ ! -z $ADDTOPATH_TARGET ] | |
then | |
if [ -f $ADDTOPATH_TARGET ] | |
then | |
file_to_update=$ADDTOPATH_TARGET | |
fi | |
fi | |
} | |
request_permission_if_needed() { | |
if [ ! -w $file_to_update ] | |
then | |
echo "Elevated privileges required to write to $file_to_update" | |
elevate_privileges | |
needs_privileges=1 | |
fi | |
} | |
# Relies on the assumption that after running a sudo command once, | |
# then sudo is allowed without a password | |
elevate_privileges() { | |
sudo echo "" > /dev/null | |
} | |
# Function for usage text | |
print_usage() { | |
echo "Usage:" | |
echo " " $0 "target_folder" "description" | |
} | |
update_path() { | |
# Create a new line in the user's bashrc | |
# Add the description as a comment | |
# Then update the path | |
echo "" >> $file_to_update | |
echo "#" $description >> $file_to_update | |
echo export PATH=$full_path_to_folder:\$PATH >> $file_to_update | |
} | |
sudo_update_path() { | |
# Is there a way to wrap a function with elevated privileges? | |
# if we could wrap update_path with sudo, that would be ideal. | |
echo "" | sudo tee -a $file_to_update > /dev/null | |
echo "#" $description | sudo tee -a $file_to_update > /dev/null | |
echo export PATH=$full_path_to_folder:\$PATH | sudo tee -a $file_to_update > /dev/null | |
} | |
# Confirm we have both inputs | |
if [ $# -ne 2 ] | |
then | |
print_usage | |
exit 1 | |
fi | |
# Checks if the user has specified a different file to update through | |
# the environment variable ADDTOPATH_TARGET and changes file_to_update | |
# accordingly | |
update_file_to_update | |
# We have both inputs, proceed with adding to path | |
full_path_to_folder=`realpath $target_folder` | |
# If file_to_update is write protected, then ask for sudo privilege | |
request_permission_if_needed | |
if [ $needs_privileges -eq 1 ] | |
then | |
sudo_update_path | |
else | |
update_path | |
fi | |
echo Added $full_path_to_folder to $file_to_update, source it to get your commands | |
It's kind of messy now, but now it is customizable by setting an environment variable and requests sudo if elevated permissions are needed, like for /etc/profile.
That's great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That's a pretty nifty tool, but it would be better to apply your toll to
/etc/profile
instead of~/.bashrc
since then it would apply globaly and would work beyond different shells.