Register
It is currently Thu May 23, 2013 6:35 am

BASH SCRIPT : Makes a symbolic link in PATH


All times are UTC


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Wed Jan 21, 2004 12:10 am   
Moderator
User avatar

Joined: Tue Dec 02, 2003 7:48 pm
Posts: 1718
Location: Georgia
this script makes a symbolic link in your /usr/bin directory
that way you don't have to keep moving files or adding many directories
that have different scripts to your PATH statement
Code:
#!/bin/bash
# Author: Joshua Bailey
# Script: link2path
# Summary: makes a symbolic link to a specified file in your /usr/bin dir.
# Syntax:  link2path /pathtofile/filename
# Syntax2: link2path /pathtofile/filename /path/to/place/link/

function notAForwardSlash
{
        if [[ $1 != '/' ]]
        then
                return 0
        else
                return 1
        fi
}

function getFileName
{
        STRING=$1
        LENGTH=${#STRING}
        n=0
        for ((n=0;n <= $LENGTH; n++))
        do
                CHAR=${STRING:$n:1}
                if notAForwardSlash $CHAR
                then
                        FileName=$FileName$CHAR
                else
                        FileName=""
                fi
        done
}
FILE=$1
cur_dir=$PWD
getFileName $FILE
if [[ ${FILE:0:1} != '/' ]]
then
        FILE=$cur_dir/$FILE
fi
if [[ ! $# = 2 ]]
then
      cd /usr/bin
else
       if [[ -d $2 ]]
       then
           cd $2

           # new option to add specified directory to $PATH
      # if it's not in $PATH already
      if [[ ! `echo $PATH | grep $2` ]]
      then
         echo "$2 is not in your path statement"
      read -p "Would you like it to be? (y/n) " ans
      ans=`echo $ans | tr :upper: :lower:`
      ans=${ans:0:1}
      if [[ $ans -eq "y" ]]
      then
         echo "PATH=$PATH:$2" >> ~/.bashrc
                        PATH=$PATH:$2
      else
         echo "Error: $2 not in path statement"
         exit
      fi
       fi    # end new option

        else
            echo "Error: $2 doesn't exist!"
            exit
        fi
fi
ln -s $FILE $FileName
cd $cur_dir

it's a little handy...like most scripts
it's an easy reference for some not-so-easy commands

:edit: added ability to choose which directory you wish to place it in
:edit: added ability to add directory specified to PATH statement


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


Powered by phpBB © 2012 phpBB Group
© 2003 - 2012 USA LINUX USERS GROUP