Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Quite often, the script I want to execute is not located in my current working directory and I don't really want to leave it.

Is it a good practice to run scripts (BASH, Perl etc.) from another directory? Will they usually find all the stuff they need to run properly?

If so, what is the best way to run a "distant" script? Is it

. /path/to/script

or

sh /path/to/script

and how to use sudo in such cases? This, for example, doesn't work:

sudo . /path/to/script
share|improve this question
    
Be aware that . /path/to/script sources the script! You don't need the period at all if you just want to run it. –  gniourf_gniourf Nov 25 '12 at 8:35

5 Answers 5

up vote 1 down vote accepted

sh /path/to/script will spawn a new shell and run she script independent of your current shell. The source (.) command will call all the commands in the script in the current shell. If the script happens to call exit for example, then you'll lose the current shell. Because of this it is usually safer to call scripts in a separate shell with sh or to execute them as binaries using either the full (starting with /) or relative path (./). If called as binaries, they will be executed with the specified interpreter (#!/bin/bash, for example).

As for knowing whether or not a script will find the files it needs, there is no good answer, other than looking at the script to see what it does. As an option, you could always go to the script's folder in a sub-process without leaving your current folder:

$(cd /wherever/ ; sh script.sh)
share|improve this answer

You can definitely do that (with the adjustments the others mentioned like sudo sh /pathto/script.sh or ./script.sh). However, I do one of a few things to run them system wide to not worry about dirs and save me useless extra typing.

1) Symlink to /usr/bin

ln -s /home/username/Scripts/name.sh /usr/bin/name

(be sure there is no overlapping name there, because you would obviously override it.) This also lets me keep them in my development folders so I can adjust as necessary.

2) Add the Scripts dir to your path (using .bash_profile - or whatever.profile you have on your shell)

PATH=/path/to/scripts/:$PATH

3) Create Alias's in the .bash_profile in ~/.bash_profile add something like:

alias l="ls -l"

As you can tell, the syntax is just alias, digits you want to act as a command, the command. So typing "l" anywhere in the terminal would result in ls -l If you want sudo, just alias sl="sudo ls -l" to note to yourself l vs sl (as a useless example).

Either way, you can just type sudo nameofscript and be on your way. No need to mess with ./ or . or sh, etc. Just mark them as executable first :D

share|improve this answer
    
I would highly recommand option 2. –  Bernhard Nov 25 '12 at 11:04

I usually do like you say

sh /path/to/script

And to run it as root/superuser

sudo sh /path/to/script

Your current directory only matters if the scripts assumes you are in the same folder as it. I would assume most scripts don't do this and you are save to run it like above.

share|improve this answer
    
would not work if secure_path is set in /etc/sudoers file –  l1zard Nov 24 '12 at 23:09

(1) I usualy put my scripts to

/usr/local/bin

or

/usr/local/sbin/ (if the script needs root privileges)

Where acording to FHS (Filesystem Hierachy Standard) they belong.

All you have to take care for is to make sure these two dirctories are added to your PATH. You can do this by editing your $HOME/.bashrc file and add this line

export PATH=$PATH:/usr/local/sbin:/usr/local/bin

If you want to execute or edit a script as root via sudo you have to add these path to the variable *secure_path* in your /etc/sudoers.

*Defaults secure_path="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"*

Editing this file is done by executing visudo to make sure you don't have any mistakes.

share|improve this answer
    
Typo: you mean .bashrc instead of .bachrc. –  gniourf_gniourf Nov 25 '12 at 14:35
    
yes of course. thanks man :) –  l1zard Nov 26 '12 at 7:02

I'm not sure it works like this in linux, assuming it doesn't if no-ones suggested it. But instead of using ././ to go back directories. Can you use quotes to give it an absolute path? Maybe it doesn't give you access to the whole drive to even be able to do that actually come to think of it.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.