Surely there must be a way to execute a script from any location and get that script to output it's full path to stdout??
did you try what i have mentioned.
if I understood you correctly then that will work.
Code:
bash->cat a.sh
#!/usr/bin/ksh
echo "this is where you executed the script from -- $(dirname $0)"
bash->pwd
/home-dev
bash->/home-dev/a.sh
this is where you executed the script from -- /home-dev
bash->mkdir test
bash->cp a.sh test
bash->/home-dev/test/a.sh
this is where you executed the script from -- /home-dev/test
Slight change.
Within "ksh" this should catch virtually every way of invoking the script (including ./scriptname and through $PATH ) and return an absolute path to the script.
Code:
whence -p "${0}"
However if the script is invoked with a path containing "." or ".." the result of the whence still contains those characters.
Finding a clean path to the script itself involves cleaning up the result of "whence".
One tortuous way is to:
1) Find the inode number of the script (extract from "ls -id").
2) User "dirname" to extract the path to the script,
"cd" to the directory containing the script,
and then store the result of "pwd -P".
The "-P" switch to "pwd" eliminates the effect of links.
The "pwd" also eliminates "." and ".." from the middle of the directory name.
3) Finally use "find" from the stored "pwd -P" with -xdev and -inum switches
to find the raw path to the script.
Do you have PERL installed on your system? There are some functions that I have come across that can give you what you are looking for.
Code:
$ cat /tmp/getpath.pl
#!/usr/bin/perl
use strict;
use warnings;
print GetAbsoluteFileName($0) . "\n";
exit 0;
sub GetAbsoluteFileName {
my $passed_filename = shift;
my $absolute_filename;
use Cwd 'abs_path';
#expand tildes
$passed_filename =~ s{ ^ ~ ( [^/]* ) }
{ $1
? (getpwnam($1))[7]
: ( $ENV{HOME} || (getpwuid($<))[7]
)
}ex;
$absolute_filename = abs_path(ExpandEnvVars( $passed_filename ) );
return $absolute_filename;
}
#Takes a string and substitutes the value of the environment variable and returns the string back
sub ExpandEnvVars {
my $env_var = shift;
# expand variables in $env_var, if environment var does not exist, leave it alone
$env_var =~ s{
\$ # find a literal dollar sign
(\w+) # find a "word" and store it in $1
}{
no strict 'refs'; # for $$1 below
if (exists $ENV{$1}) {
$ENV{$1}; # expand environment variables only
} else {
"\$$1"; # environment var does not exist, so leave what they gave us
}
}egx;
return $env_var;
}
$ /tmp/getpath.pl
/tmp/getpath.pl
$ ls -1 /tmp/test1/getpath.pl
/tmp/test1/getpath.pl
$ /tmp/test1/getpath.pl
/tmp/test1/getpath.pl
$ export MYTEST=/tmp/test2
$ ls -1 $MYTEST/getpath.pl
/tmp/test2/getpath.pl
$ $MYTEST/getpath.pl
/tmp/test2/getpath.pl
I'm finally about to make the jump from PHP's mysql driver to the newer mysqli one using the mysqli converter tool.
The tool is called by this command line:
php /usr/share/nginx/html/rather/converter/cli.php -d -p ".php,.shtml,.inc" -u -b -v -w >> /tmp/convert.log
e.g. at the web root:... (2 Replies)
Hi i wrote a script which dosent exists after executing any help
#!/bin/bash
netstat -ptlen | grep 10000
if ; then
echo "Hive Thrift server is running"
exit 0
else
echo "Hive Thrift server is down Trying to Bring up the service" | mail -s "ALERT" team@domain.com
`nohup hive... (7 Replies)
Hi,
I have a script get_DB_var.ksh which do a data base call and get some variables as below:
sqlplus -silent $user/$pass@dbname <<END
select col1,
col2,
col3
from table_name where col4=$1;
exit;
END
Now I want to access all these variables i.e.... (9 Replies)
Hi,
This is a quick one.
I have got a review comment in one of the scripts that i wrote:
"In UNIX script it not advisable to use cd to a directory and then run command."
Is this true?
I was trying to cd to log directory and use some cat or head or ls command.
Many Thanks,
Sam (2 Replies)
I need to execute a shell script kept in unix machine from windows. User id, password area available.
For eg.
There's a shell script wich moves all the logs kept in my home directory to a directory named LOGS.
Now i need to get this done through windows; either using a batch file, or java... (4 Replies)
Hi all,
I am getting : No such file or directory while executing a shell script. But i have that corresponding file in the corresponding path. It also have executable rights. Please help me out in this
Thanks in advance.
Ananthi.U (7 Replies)
I have a shell script /home/user1/bin/sh1, how can I print out the directory of this file inside the script.
Say I am in /home/user1/, then I run
/home/user1/bin/sh1
I need it to print out /home/user1/bin.
Thanks. (2 Replies)
Hi Guys.
I am a complete shell scripting newbie with some syntax and commands understanding. I'm more of a win admin. With that said: I need to write a shell script to monitor a directory '/Mon_Dir' for new occurrences of files with .xx extension. Once a new file is detected in the directory, a... (4 Replies)