473,657 Members | 2,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Find file path and/or user aliases....

5 New Member
OS using: Linux(core 1), Solaris (5.6 to 5.8), Unix
Perl version: v5.8.5

I need to find the path or alias that the user used to execute the program with (aka $0), just like the built-in shell commands which or where, but using Perl.

Now if I just want the path I have found many ways to do that using things like:
Expand|Select|Wrap|Line Numbers
  1. use Cwd qw(realpath);
  2. my $exe_path = realpath($0);
  3. print "\nYou are executing $exe_path.\n";
  4.  
or
Expand|Select|Wrap|Line Numbers
  1. use File::Which qw(which where);
  2. my $exe_path = which($0);
  3.  
or
Expand|Select|Wrap|Line Numbers
  1. use Shell;
  2. $exe_path = which("$0");
  3.  
etc.....

But none of those work if the user had an alias. I can doing the following examples from the shells command line prompt and it gets what I want, but haven't been able to do the same thing from my Perl script.

Example:
PROMPT> where gp
gp is aliased to egrep --color
PROMPT> alias -p| /usr/bin/which -i -a gp
gp egrep --color
/bin/egrep <--(this is what I really want)
I would prefer the second case (alias -p| /usr/bin/which -i -a) since it gives you the path to the egrep that is being used, but its not absolute that I get that. I would just be happy if I got "gp is aliased to egrep --color". I also what to avoid having to parse through the .cshrc file of the user.

So does anyone know how to do what I want. At this point I'm thinking I may have to create some perl module thats written in another language to do what I want.
Sep 19 '07 #1
9 2934
numberwhun
3,509 Recognized Expert Moderator Specialist
OS using: Linux(core 1), Solaris (5.6 to 5.8), Unix
Perl version: v5.8.5

I need to find the path or alias that the user used to execute the program with (aka $0), just like the built-in shell commands which or where, but using Perl.

Now if I just want the path I have found many ways to do that using things like:
Expand|Select|Wrap|Line Numbers
  1. use Cwd qw(realpath);
  2. my $exe_path = realpath($0);
  3. print "\nYou are executing $exe_path.\n";
  4.  
or
Expand|Select|Wrap|Line Numbers
  1. use File::Which qw(which where);
  2. my $exe_path = which($0);
  3.  
or
Expand|Select|Wrap|Line Numbers
  1. use Shell;
  2. $exe_path = which("$0");
  3.  
etc.....

But none of those work if the user had an alias. I can doing the following examples from the shells command line prompt and it gets what I want, but haven't been able to do the same thing from my Perl script.

Example:
PROMPT> where gp
gp is aliased to egrep --color
PROMPT> alias -p| /usr/bin/which -i -a gp
gp egrep --color
/bin/egrep <--(this is what I really want)
I would prefer the second case (alias -p| /usr/bin/which -i -a) since it gives you the path to the egrep that is being used, but its not absolute that I get that. I would just be happy if I got "gp is aliased to egrep --color". I also what to avoid having to parse through the .cshrc file of the user.

So does anyone know how to do what I want. At this point I'm thinking I may have to create some perl module thats written in another language to do what I want.
First, when posting, please be sure and be clear. There seem to be so many thoughts in this posting that it is hard to truly decipher what it is you want.

As far as having to cycle through the users .cshrc or whatever other type of dot file, I doubt there will be a way around that. That is where users set their environment up, define functions, set aliases, etc. Unless you are logged in as that user and can type "alias" as them, there would not be another way that I know of.

Regards,

Jeff
Sep 19 '07 #2
LuckyLeprechaun
5 New Member
First, when posting, please be sure and be clear. There seem to be so many thoughts in this posting that it is hard to truly decipher what it is you want.

Jeff
Sorry about not being clear. I just want the path to the file the user typed in to execute my script. The problem has been some people use aliases instead, so I don't get the path to the file that was being executed. The script will be executed by the user from there home account.

The reason I need this information is to find rogue/forked copies of my script and log the output to a log file that I can audit.
Sep 19 '07 #3
prn
254 Recognized Expert Contributor
I'm not sure whether it's a language problem or what, but I have no idea at all what you are looking for.

I have created the following file (called mypath):
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. print "You are executing $0 \n";
On Linux (Fedora Core 6), in bash, I run
Expand|Select|Wrap|Line Numbers
  1. $ alias shmoo='mypath'
  2. $ shmoo
  3. You are executing /home/prn/perltest/mypath
On Solaris 9, (c shell, since Bourne shell does not do alias), I run:
Expand|Select|Wrap|Line Numbers
  1. % alias shmoo '/export/home/prn/mypath'
  2. % shmoo
  3. You are executing /export/home/prn/mypath
So, what did you want?

Best Regards,
Paul
Sep 19 '07 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
I'm not sure whether it's a language problem or what, but I have no idea at all what you are looking for.

I have created the following file (called mypath):
Expand|Select|Wrap|Line Numbers
  1. #! /usr/bin/perl
  2. use strict;
  3. print "You are executing $0 \n";
On Linux (Fedora Core 6), in bash, I run
Expand|Select|Wrap|Line Numbers
  1. $ alias shmoo='mypath'
  2. $ shmoo
  3. You are executing /home/prn/perltest/mypath
On Solaris 9, (c shell, since Bourne shell does not do alias), I run:
Expand|Select|Wrap|Line Numbers
  1. % alias shmoo '/export/home/prn/mypath'
  2. % shmoo
  3. You are executing /export/home/prn/mypath
So, what did you want?

Best Regards,
Paul
I think that he is looking to resolve the aliases for commands that other users have executed and that he sees running in "ps". He would not have a way to really know what the aliases are unless he was sourced into their dot file or logged in as them, I believe.

Regards,

Jeff
Sep 19 '07 #5
LuckyLeprechaun
5 New Member
Well after many hours of google searches and banging away at the keyboard, it doesn't appear to be a good clean way. I will parse through the users .cshrc (C shell file) for aliases and do a which command and do some fancy comparison. That will be the only way to catch 98% of the cases.

I'm just really surprised that there was no way to read in a users aliases from a script in any language that I could find.
Sep 20 '07 #6
numberwhun
3,509 Recognized Expert Moderator Specialist
Well after many hours of google searches and banging away at the keyboard, it doesn't appear to be a good clean way. I will parse through the users .cshrc (C shell file) for aliases and do a which command and do some fancy comparison. That will be the only way to catch 98% of the cases.

I'm just really surprised that there was no way to read in a users aliases from a script in any language that I could find.
Actually, I am not so suprised. Even in Unix itself you would have to be logged in as that user or have sourced their .cshrc file in order to know what their aliases are. This, to me, sounds like a good project for a module.

Regards,

Jeff
Sep 20 '07 #7
LuckyLeprechaun
5 New Member
... Even in Unix itself you would have to be logged in as that user ....

Regards,

Jeff
That wouldn't be a problem, since the "user" will be the one running the script. So basically it would be logged in as that user.
Sep 20 '07 #8
numberwhun
3,509 Recognized Expert Moderator Specialist
That wouldn't be a problem, since the "user" will be the one running the script. So basically it would be logged in as that user.
Well, if it is part of the script that the user is running, then just do an:

Expand|Select|Wrap|Line Numbers
  1. alias <command_name>
  2.  
and it will return what it is triggering.

Regards,

Jeff
Sep 20 '07 #9
LuckyLeprechaun
5 New Member
When I was testing that out it didn't seem to work at all and just kept giving me errors about not finding command alias.

Since we are running older versions of Linux (Red Hat 4) & Solaris (5.6 or 5.8), I believe that we are just missing updates that allow it to work.

Thanks
Sep 20 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3719
by: Xah Lee | last post by:
suppose you want to do find & replace of string of all files in a directory. here's the code: ©# -*- coding: utf-8 -*- ©# Python © ©import os,sys © ©mydir= '/Users/t/web'
4
17004
by: John Bailo | last post by:
I have created several aliases to members using the CREATE ALIAS command, but I lost track of them. Now I want to see a catalog of all the aliases mapped to all members in a FILE. Is there a SQL commmand I can issue to see these?
0
1669
by: haylow | last post by:
Hi I am new to ASP.NET and am working on an application that runs on a webserver. The user will open up the web interface in a browser on their local machine and enter a path to a directory. I have 2 problems: First, I can't find a control that will let the user browse the file system and specify a directory - the only one I can find is the one that lets the user specify a file.
67
7681
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 4/30/06 I am looking for suggestions on how to find the date ranges where there were no transactions.
1
1966
by: amir | last post by:
Hi, When compiling a page in VS2005 this morning I received 101 messages regarding schema problems in my web.config file. When I go to view an aspx page in my IIS, IE just displays a blank page. I have the June Atlas CTP installed.
15
10842
by: Sandra-24 | last post by:
Comparing file system paths as strings is very brittle. Is there a better way to test if two paths point to the same file or directory (and that will work across platforms?) Thanks, -Sandra
0
5075
by: nimjerry | last post by:
i am using db2 udb V 9 on aix 5.3 and in db2diag.log alwas has this error occurr below is sample message 2008-03-03-09.45.34.366406+420 I306667A443 LEVEL: Warning PID : 835622 TID : 1 PROC : db2stmm (WEBEDIDB) INSTANCE: db2inst1 NODE : 000 DB : WEBEDIDB APPHDL : 0-8 APPID: *LOCAL.db2inst1.080229022428 AUTHID : DB2INST1 FUNCTION: DB2 UDB, Self tuning memory...
0
1423
by: sweavo | last post by:
Hi all, (Python 2.5 under cygwin) I'm reading a bunch of XML files and merging them in memory - each file contains a number of packages which are to be merged: for fname in gInfiles: doc=xml.dom.minidom.parse(fname) if gDoc is None:
185
7004
by: jacob navia | last post by:
Hi We are rewriting the libc for the 64 bit version of lcc-win and we have added a new field in the FILE structure: char *FileName; fopen() will save the file name and an accessor function will return the file name given a FILE *. Questions: What would be the best name for this function?
0
8399
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8312
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1959
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.