Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple script question 3

Status
Not open for further replies.

jnothinghead

IS-IT--Management
Aug 10, 2001
18
0
0
US
I am learning to write scripts and I would like to know how I can execute my ksh script from any directory.
Thanks
 
If the script (fred.sh, for example) is not in a directory that's in your PATH, then either enter the full path - /test/scripts/fred.sh - or cd to the directory 1st & then ./fred.sh.

Hope that's what you wanted :)
TandA
 
If your script resides in /some/dir/theScript.ksh, then to make sure the script can be run from anywhere, then do the following:

PATH=$PATH:/some/dir
export PATH

The script should now run from anywhere. Note, it is best to place /some/dir after $PATH, as putting it in front is a security risk for trojan scripts (eg: someone could put a fake 'passwd' script in /some/dir, which would get called before the genuine passwd script)

Hope this helps. Cheers, NEIL :cool:
 
If you jump around to far out directories and you build your scripts in your home directory then you can enter "~/scriptname" in the other directories and it will kick off your script. Just remember when IN your home directory to enter just the scriptname without the ~ such as "scriptname" and it will run.
 
Why is some situations, when I try to run a script with ./filename.pl , it says can not execute? Why is that? What do I do wrong?

Thanks,
Cait.
 
Hi,
There are a couple of possibilities.

First you forgot to give the script execute permission

chmod 775 filename.pl

Secondly you have to tell it what program language your script is written in. UNIX doesn't have File extension association like Windows does.

There is no reason on UNIX a SH script can't end in .pl, although realistically this just causes confusion. try to avoid that.


The first line of the all EXECUTABLE UNIX SCRIPT is supposed to be....

#!<program>

where <program> is the program which your script is written for. If you forget this line or it isn't the first line of the script most UNIX systems assume /bin/sh.


For example

#!/bin/sh ( For Sh script )

#!/bin/ksh ( For ksh scripts. )

#!/usr/bin/csh ( for csh script )

#!/usr/local/bin/perl ( for perl )

Your paths maybe different than these. Typcially from the command prompt you can specify

which <program>

and it will tell you where in your search path <program> is located.

Update the first line of your script accordingly.


If you want more help concering the #! syntax and perl try

man perlrun

( assuming you have the full perl installed correctly on your unix system )



hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top