Fedora Linux Support Community & Resources Center
  #1  
Old 7th September 2011, 05:52 PM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxchrome
Search path for 'sh'?

I would like to run a script from any location. The script is in ~/scripts, so I added ~/scripts to my PATH. However, since I have to run it with 'sh script.sh' (due to the permissions on the directory), it doesn't seem to look in PATH for the file script.sh. Is there a way to get it to do this?

Thanks,

David
Reply With Quote
  #2  
Old 7th September 2011, 06:34 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Search path for 'sh'?

Permissions on the directory? Only the script needs execute permission, unless /home is mounted with the "noexec" option (or you're using a non-native filesystem).

Edit: Actually list (read) permission on the directory would be needed for searches via PATH to find it.

Otherwise, maybe a shell alias might be more appropriate?

Assuming you only need to run the script from a bash prompt, add this to ~/.bashrc or /etc/bashrc:

Code:
alias script_name="sh /home/daviddoria/scripts/script_name"
Gareth

Last edited by Gareth Jones; 7th September 2011 at 06:38 PM.
Reply With Quote
  #3  
Old 7th September 2011, 06:38 PM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxchrome
Re: Search path for 'sh'?

Yea, in my example I simplified /media/externalNTFS/Scripts to ~/Scripts . It is indeed a non-native filesystem that the script resides on.

The alias is a good idea. That would certainly work, but if I have 20 scripts then I have to make 20 aliases, which could get unwieldy. Is there anything else I could do?

Thanks,

David
Reply With Quote
  #4  
Old 7th September 2011, 06:51 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,865
linuxfedorafirefox
Re: Search path for 'sh'?

You need search permission on the directory, not read. If you already know the file name, you don't need to search for it. Read allows the opendir function that allows you to read the directory. search (the "x" in a directory) is needed for exec.

All you need is to set your PATH environment variable appropriately.

Something like: PATH="/usr/bin:/bin:/home/<login>/Scripts"
will do. Don't forget you must export the altered path ("export PATH")

Now all you need is the file name.

no aliases required.
Reply With Quote
  #5  
Old 7th September 2011, 06:54 PM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxchrome
Re: Search path for 'sh'?

jpollard, but I can't set that permission because it is an NTFS file system.
Reply With Quote
  #6  
Old 7th September 2011, 06:56 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Search path for 'sh'?

I'm afraid the best I can think of is to use a function instead of an alias:

Code:
function runsh
{
    script_name="$1"
    script_file="/media/externalNTFS/Scripts/$script_name"

    if [[ -f "$script_file" ]]
    then
        shift
        sh "$script_file" "$@"
    else
        echo "$0: $script_name: Script not found" >&2
        false
    fi
}
Then run the scripts:

Code:
runsh script_name args
Gareth
Reply With Quote
  #7  
Old 7th September 2011, 06:59 PM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxchrome
Re: Search path for 'sh'?

Ah cool idea, thanks. I don't suppose I could be greedy and ask for a way to tab complete script names ?

runsh MySc[tab]
->
runsh MyScript.sh

?
Reply With Quote
  #8  
Old 7th September 2011, 07:02 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,865
linuxfedorafirefox
Re: Search path for 'sh'?

You are out of luck.

NTFS is not a reasonable filesystem for linux home directories. It has neither POSIX capabilities, nor can it be secured. The only permissions that can be set can only be done by the "umask=" during mount.

These are no longer discretionary controls, as they defined globally. It is one of the reasons security is so difficult with windows. Setting the execute definition in mount means ALL files will be executable, whether they should be or not. Any file downloaded will suddenly be a possible virus.

Other problems will still persist - text files downloaded will be in their native format (newline terminated, not carrage return/new line). Ownership of all files will be by one user, there is no separation of system and user files. And the filesystem will still need to be defragged periodically - and that should only be done under Windows.
Reply With Quote
  #9  
Old 7th September 2011, 07:15 PM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Search path for 'sh'?

Quote:
Originally Posted by jpollard View Post
You need search permission on the directory, not read. If you already know the file name, you don't need to search for it. Read allows the opendir function that allows you to read the directory. search (the "x" in a directory) is needed for exec.
For the shell to find files in PATH directories, read permission is needed on those directories or it can't see the names of their contents.
Edit: Thinking about it, I may be completely wrong about that. I suspect it probably just tries to stat the file in each of the PATH dirs.

All moot due to NTFS unfortunately...

Gareth

---------- Post added at 07:07 PM ---------- Previous post was at 07:04 PM ----------

Quote:
Originally Posted by daviddoria View Post
Ah cool idea, thanks. I don't suppose I could be greedy and ask for a way to tab complete script names ?
Sorry, I've no idea how programmable completion is configured... If you've got the bash-completion package installed, you might be able to figure it out from looking at how they do it.

Gareth

---------- Post added at 07:15 PM ---------- Previous post was at 07:07 PM ----------

If all your scripts' names end ".sh", you could save yourself a little typing by changing the function:

Code:
script_file="/media/externalNTFS/Scripts/$script_name.sh"
Gareth

Last edited by Gareth Jones; 7th September 2011 at 07:31 PM.
Reply With Quote
  #10  
Old 7th September 2011, 11:04 PM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxchrome
Re: Search path for 'sh'?

Please ignore - it works great!

---------- Post added at 06:04 PM ---------- Previous post was at 05:56 PM ----------

I tried calling the runsh function from inside of a script that was called with the runsh function. I got an error that runsh was not found. Is there a reason that it would not be found?

Last edited by daviddoria; 7th September 2011 at 10:59 PM.
Reply With Quote
  #11  
Old 8th September 2011, 12:01 AM
Gareth Jones Offline
Official Gnome 3 Sales Rep. (and Adminstrator)
 
Join Date: Jul 2011
Location: Birmingham, UK
Age: 32
Posts: 2,771
linuxfirefox
Re: Search path for 'sh'?

Quote:
Originally Posted by daviddoria View Post
I tried calling the runsh function from inside of a script that was called with the runsh function. I got an error that runsh was not found. Is there a reason that it would not be found?
Yes, .bashrc etc. are only loaded for interactive shells, not scripts, hence why I mentioned the command prompt earlier. Looks like you need another script...

Put the following in ~/bin/runsh (which should already be in your PATH if you're using Fedora) and make it executable.

Code:
#!/bin/sh
# Usage: runsh SCRIPT SCRIPT_ARGS

script_name="$1"
script_file="/media/externalNTFS/Scripts/$script_name.sh"

if [[ -f "$script_file" ]]
then
    shift
    exec sh "$script_file" "$@"
fi

echo "$0: $script_name: Script not found" >&2
exit 1
Gareth
Reply With Quote
  #12  
Old 8th September 2011, 12:10 AM
daviddoria Offline
Registered User
 
Join Date: Oct 2007
Posts: 884
linuxubuntuchrome
Re: Search path for 'sh'?

Gotcha. Thanks again!
Reply With Quote
Reply

Tags
path, search

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Search Path for compiler executable rohith88 Using Fedora 3 15th January 2010 02:54 AM
problem with DNS search path abhi.rkmvm Servers & Networking 1 10th November 2009 05:22 AM
DNS search path lieilu Servers & Networking 3 8th January 2008 01:09 AM
Search path in KDE staham Using Fedora 3 15th January 2007 07:59 PM
pkg-config search path alphonsebrown Using Fedora 4 20th September 2005 08:32 AM


Current GMT-time: 08:00 (Wednesday, 22-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Pertuis - Ninghai Travel Photos on Instagram - Vepagunta