 |
 |
 |
 |
| Using Fedora General support for current versions. Ask questions about Fedora and it's software that do not belong in any other forum. |

7th September 2011, 05:52 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
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
|

7th September 2011, 06:34 PM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,699

|
|
|
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.
|

7th September 2011, 06:38 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
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
|

7th September 2011, 06:51 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,097

|
|
|
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.
|

7th September 2011, 06:54 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
Re: Search path for 'sh'?
jpollard, but I can't set that permission because it is an NTFS file system.
|

7th September 2011, 06:56 PM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,699

|
|
|
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
|

7th September 2011, 06:59 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
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
?
|

7th September 2011, 07:02 PM
|
|
Registered User
|
|
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,097

|
|
|
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.
|

7th September 2011, 07:15 PM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,699

|
|
|
Re: Search path for 'sh'?
Quote:
Originally Posted by jpollard
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
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.
|

7th September 2011, 11:04 PM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
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.
|

8th September 2011, 12:01 AM
|
|
Official Gnome 3 Sales Rep. (and Adminstrator)
|
|
Join Date: Jul 2011
Location: Leamington Spa, UK
Age: 30
Posts: 1,699

|
|
|
Re: Search path for 'sh'?
Quote:
Originally Posted by daviddoria
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
|

8th September 2011, 12:10 AM
|
|
Registered User
|
|
Join Date: Oct 2007
Posts: 884

|
|
|
Re: Search path for 'sh'?
Gotcha. Thanks again!
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 12:31 (Monday, 20-05-2013)
|
|
 |
 |
 |
 |
|
|