The improved addpath script

My Linux+ book has lots of simple example shell scripts. Pretty useless stuff really. For instance they have this little script for adding to the PATH environment variable;

#!/bin/bash
# add path to path variable
echo “What is the directory path you would like to add to the PATH variable?”
read MYNEWPATH
if [ -e “$MYNEWPATH” ]; then
echo “Checking validity of the “$MYNEWPATH” path.”
echo “Adding” $MYNEWPATH “to your PATH variable.”
PATH=$PATH:$MYNEWPATH
export PATH
echo “Your new PATH environment variable is now:”
echo $PATH
else
echo $MYNEWPATH ” is not a valid directory path”
fi
exit 0

However this lets total n00b users open up security holes. So I added checking for paths starting with “.” and containing hidden files. There is probably an easer way to do this but I am a total n00b shell scripter.

#!/bin/bash
# add path to path variable
echo “What is the directory path you would like to add to the PATH variable?”
read MYNEWPATH
if [ -e “$MYNEWPATH” ]; then
case $MYNEWPATH in
.* | */.* ) echo “Adding” $MYNEWPATH “would weaken the security of the sytem.”
echo “I did not add” $MYNEWPATH “to your PATH variable.”
echo “Don’t try it again you ninny. Now go and study the man pages some more”
;;
/* ) echo “Checking validity of the “$MYNEWPATH” path.”
echo “Adding” $MYNEWPATH “to your PATH variable.”
PATH=$PATH:$MYNEWPATH
export PATH
echo “Your new PATH environment variable is now:”
echo $PATH
;;
esac
else
echo $MYNEWPATH ” is not a valid directory path”
fi
exit 0

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s