LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices

Reply
 
Search this Thread
Old 08-25-2010, 11:05 PM   #1
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Rep: Reputation: 51
Adding a script to path


Hi
How would I add a script to my path so that all I would do to run it is type its name? Can I just save the script to /usr/local/ ?

Thanks.
 
Old 08-25-2010, 11:08 PM   #2
Meson
Member
 
Registered: Oct 2007
Distribution: Arch x86_64
Posts: 606

Rep: Reputation: 66
There are two good options here. 1 you can /usr/local/bin to you PATH if it isn't already there. Then save the script in /usr/local/bin.

The other option is you can add $HOME/bin (or $HOME/.bin) to your PATH and put the script there. A nice way to do this is to add a line like:
Code:
[[ -d "${HOME}/bin" ]] && export PATH="${HOME}/bin:${PATH}"
to either ~/.profile or /etc/profile...
 
1 members found this post helpful.
Old 08-25-2010, 11:09 PM   #3
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You could put the script in the $HOME/bin/ directory.
If the $HOME/bin/ directory exists, it should be added to PATH automatically by the .profile script. If not, edit your .profile (or .bash_login if used instead) and add it to your PATH variable.
 
1 members found this post helpful.
Old 08-25-2010, 11:42 PM   #4
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
Hey thanks!
So, if I had a script struscture like below, I would place that in the /usr/local/bin? In this case would it be better to go with the second option you posted?
Code:
/usr/local/src/revdns_lookup/
ip_utils.py  ip_utils.pyc  README  revdns.py  reverse_resolver.py  reverse_resolver.pyc  simple_log.py	simple_log.pyc	thread_pool.py	thread_pool.pyc  xrange.py  xrange.pyc
 
Old 08-26-2010, 12:20 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
The PATH has to contain the exact names of the directories to search. It won't automatically look in subdirectories.

Another option that's available to you is to simply place a symlink to the script in a directory that's already in your path. I personally have a directory, /usr/local/bin/myscripts, that's not in my path, and symlinks to the scripts in it in /usr/local/bin, which is.

This gives me the advantage of being able to control which scripts are available to the PATH, and under what name, since the link doesn't have to have the same name as the script itself. e.g., I link /usr/local/bin/myscripts/scriptfoo.sh --> /usr/local/bin/foo, and I can then launch it by simply typing foo.
 
1 members found this post helpful.
Old 08-27-2010, 11:40 PM   #6
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
hey thanks for the advice. I ran
Code:
ln -s /usr/local/bin /usr/local/src/my_scripts/run
I am still unable to tab to complete the command ( guess its not showing up ) . I also tried
Code:
export PATH=$PATH:/my/script/path
export
which added the path. HOwever, ( maybe because I am using gnome-term rather than xterm ) I am unable to run the script with just the name. Am I missing something?

Side note: the added path using export DOES work for that shell instance. once I close gnome-term the added path entry is gone. If I open another shell the path is not listed. I should mention I ran the export and link commands with sudo appended.

Last edited by mrmnemo; 08-27-2010 at 11:53 PM.
 
Old 08-27-2010, 11:56 PM   #7
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
also, I have some questions regarding the the bash.bashrc file in ubuntu. Which kinda reflects on PATH. I dont understand why the bash.bashrc file would place me ( as user not root ) in a chrooted environment. Am I not understanding what I am looking at correctly?
 
Old 08-28-2010, 06:54 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
Quote:
Originally Posted by mrmnemo View Post
Code:
ln -s /usr/local/bin /usr/local/src/my_scripts/run
You've got the command backwards. You want to create a link to each of your scripts inside /usr/local/bin. Run ls -l on the directory to view any symlinks that exist and the target files they point to. Read man ln for the proper syntax on creating a symlink.

And have you confirmed that /usr/local/bin is in your path? What does echo $PATH show you?

Just to be perfectly clear about it, the path is simply a list of directories that the shell will check, in the order given, for the names you enter on the command line. If it finds a filename that matches the command, it will attempt to execute that file. That's it.

The file matched can be just about anything; a program, script, or link to another file, but of course only executable files will actually work. If there are no files with that name in any of the PATH directories, it will error out. If there are multiple files of the same name in the PATH, the first one found will be the one executed, meaning that the order of the directories given can sometimes be important.

If your commands aren't being found, it simply means that there are no files with that name in any of the PATH directories. So either you have the PATH wrong, or the filenames wrong.

Quote:
Side note: the added path using export DOES work for that shell instance. once I close gnome-term the added path entry is gone.
That's as it should be. Settings always only apply to the current shell. If you want to make it permanent you have to add the command to one of your shell's start-up scripts so that it loads every time.

BTW, I'm pretty sure that gnome-term is an xterm. Most console applications are basically xterm front-ends. echo $TERM to confirm it. It shouldn't affect the behavior of your path in any case, since that's a function of the shell that's running inside the terminal.

Quote:
also, I have some questions regarding the the bash.bashrc file in ubuntu. Which kinda reflects on PATH. I dont understand why the bash.bashrc file would place me ( as user not root ) in a chrooted environment. Am I not understanding what I am looking at correctly?
The answer to that would depend on the contents of your bashrc and/or perhaps the command that launches your terminal. I've never heard of Ubuntu setting up a user in a chroot by default, however.

Last edited by David the H.; 08-28-2010 at 07:04 AM. Reason: redundant removal of redundancy
 
1 members found this post helpful.
Old 08-28-2010, 03:44 PM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
From the list of files that you posted, these look like they are from a package. If they need to be run as root (for access to low level ports), then /usr/local/sbin/ seems the most logical location for them. Make sure that they are in root's PATH. You may need to give the full path for commands which use sudo.

It might not be an issue of the PATH variable. The default env_reset behavior of sudo may remove /usr/local/bin and /usr/local/sbin from the PATH variable for security reasons. Different distros configure sudoers differently.

$HOME/bin/ would be where you would put you own (user) scripts and one liners.
/usr/bin/ and /usr/sbin/ would be where your distro's supplied programs and scripts would go when you install a package.
For scripts and binaries that you download and install from source, /usr/local/ would be the base directory for the installation instead of /usr. Files under /usr/local/ won't be touched by your distro unless you reformat the partition during a re-installation.

Just so it's clear, /usr/ and /usr/local/ contain a hierarchy of directories similar to the root directory (/). The rules on which subdirectory to use are similar, but the source of the files determine which is used. The (/) root hierarchy contains the files needed for the computer to run and be serviced. The Filesystem Hierarchy Standard explains this.
 
1 members found this post helpful.
Old 08-28-2010, 04:24 PM   #10
mrmnemo
Member
 
Registered: Aug 2009
Distribution: linux
Posts: 527

Original Poster
Rep: Reputation: 51
Hey thanks for the info on "The File System Hierarchy" . Googled it and got this, which is helping alot for understanding some of the structure. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script path issue - how to pass a path as a string to a variable PiNPOiNT Programming 5 04-17-2009 05:48 PM
script to change unix path to windows path in all files csross Programming 8 04-29-2006 01:05 PM
Adding to $PATH uv2005 Linux - Newbie 2 03-22-2006 12:32 PM
adding to $PATH poiuytrewq Linux - Newbie 3 10-27-2004 01:32 AM
adding to path hamish Linux - Software 8 02-05-2004 10:03 PM


All times are GMT -5. The time now is 07:36 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration