Re: Where am I (csh)
On Wed, 15 Jan 2003, Mark Komarinski wrote: =Without getting into the why are you using X to do the job: = =I've got a csh script in an arbitrary location. But I need to know =from within the script where it exists in the directory structure. The =reason for that is I need to source a file from within that same =directory (where the script is). If I look for cwd or pwd, I get the =directory my shell was when when I ran the script, not where the =script itself is located. = =As an additional condition, I can't use anything on the local system, as =the script will be run over NFS to various systems (hence one of the =reasons it's arbitrary). And it has to be csh, no tcshisms. = =-Mark Where to start... Never run any csh script. Go to google and read Tom Christionson's excellent paper C shell considered harmful. Having been properly chastised, you can try refering to $0. It might work. Another reason for not using csh BTW is that the equivalent construct in bash or ksh uses PATH to locate file to source in, so there's no need to specify it via any kind of pathname. -- -Time flies like the wind. Fruit flies like a banana. Stranger things have - -happened but none stranger than this. Does your driver's license say Organ -Donor?Black holes are where God divided by zero. Listen to me! We are all- -individuals! What if this weren't a hypothetical question? [EMAIL PROTECTED] ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
On Wed, Jan 15, 2003 at 02:43:42PM -0500, Mark Komarinski wrote: On Wed, Jan 15, 2003 at 02:34:28PM -0500, Kevin D. Clark wrote: Mark Komarinski [EMAIL PROTECTED] writes: Without getting into the why are you using X to do the job: I've got a csh script in an arbitrary location. But I need to know from within the script where it exists in the directory structure. The reason for that is I need to source a file from within that same directory (where the script is). If I look for cwd or pwd, I get the directory my shell was when when I ran the script, not where the script itself is located. As an additional condition, I can't use anything on the local system, as the script will be run over NFS to various systems (hence one of the reasons it's arbitrary). And it has to be csh, no tcshisms. Doing this, in the most general case, is very difficult. It doesn't matter which shell you're using either... Is `dirname $0` good enough? (even though in certain strange situations (that you will probably never experience) it might not be correct). echo $0 in the script in both irix and linux gives: -tcsh -Mark (not a shell) Ahh. This is because I'm sourcing the file. If I run the script itself, I get the expected response. Bleah. -Mark (still not a shell) msg02257/pgp0.pgp Description: PGP signature
Re: Where am I (csh)
Mark Komarinski [EMAIL PROTECTED] writes: echo $0 in the script in both irix and linux gives: -tcsh How about $_ instead of $0 ? (I just tested this; it works for me) Regards, --kevin PS It's unclear to me as to why your system is printing out tcsh instead of csh. -- Listen, this old system of yours could be on fire and I couldn't even turn on the kitchen tap without filling out a 27b/6... Bloody paperwork. Harry Tuttle, Brazil ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
On Wed, Jan 15, 2003 at 02:44:44PM -0500, Derek Martin wrote: -BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On Wed, Jan 15, 2003 at 02:20:12PM -0500, Mark Komarinski wrote: Without getting into the why are you using X to do the job: Who, us? ;-) I've got a csh script in an arbitrary location. But I need to know from within the script where it exists in the directory structure. The reason for that is I need to source a file from within that same directory (where the script is). If I look for cwd or pwd, I get the directory my shell was when when I ran the script, not where the script itself is located. I'm not that familiar with csh, but AFAIK this can not be done; at least not directly. The only way the script will know where it lives is if you tell it explicitly on the command line, either by specifying the full path (it'll be in the equivalent of argv[0] if csh makes that available), or by including the directory as a command-line option. A possible solution is to use the which command from within the script to determine the path to the script. Obviously, if the path to the script is different from within the script as compared to when the script is started (though I can't imagine why this would be), then it won't work. As an additional condition, I can't use anything on the local system, as the script will be run over NFS to various systems (hence one of the reasons it's arbitrary). And it has to be csh, no tcshisms. I'm not sure I understand why this is a problem... the script is still going to run on machine X, wherever it lives; it should be able to use any of the commands local to machine X. I could do that, but I don't want to configure 30+ machines every time I need to make a change. Perhaps I should go into more detail of this script. We have an internal project which combines a bunch of software used by people with very long sets of initials after their names (PhD, BS, MS, MD, etc.) doing things I will never understand (xray crystallography - did I even spell that right?). There is an existing collection of software available for Linux and SGIs being distributed by a different lab and most of the labs I administer are either using it, or want to. The problem with said distribution is there is a 35k cshrc file that has to be sourced when the user logs in to set up the various environmental variables and paths to let them use the applications. The problem is that as the software is distributed now, you have to create a link in the root directory to the location of where the software actually sits (which is arbitrary due to NFS and how things are configured). So on each machine that each user needs access to the collection, I have to make the symbolic link. That's annoying. There has to be a way for a script to know where it is and take advantage of that fact. But the cshrc file has to remain as it is, as the distributors of the software will change it as new versions of software packages come out. So I have have the script know where it's located when it runs so it can load up a configuration file for that instance of the collection of software, which again is at an arbitrary location. For reasons beyond my (current) control, this is all done in csh. Can't change it, but I may be able to write a bash version, but that won't help the existing users who don't want to change from csh to bash. Whew. Did I ever mention that life in academics is a lot different from life in the business world? -Mark msg02259/pgp0.pgp Description: PGP signature
Re: Where am I (csh)
On Wed, Jan 15, 2003 at 02:47:56PM -0500, Kevin D. Clark wrote: Mark Komarinski [EMAIL PROTECTED] writes: echo $0 in the script in both irix and linux gives: -tcsh How about $_ instead of $0 ? _: undefined variable. (I just tested this; it works for me) Regards, --kevin PS It's unclear to me as to why your system is printing out tcsh instead of csh. the system I'm testing on happens to have tcsh installed. But I can't guarantee that across all machines. -Mark msg02260/pgp0.pgp Description: PGP signature
Re: Where am I (csh)
Mark Komarinski [EMAIL PROTECTED] writes: The problem is that as the software is distributed now, you have to create a link in the root directory to the location of where the software actually sits (which is arbitrary due to NFS and how things are configured). Perhaps you could solve this problem by using the automounter? Regards, --kevin -- Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA) cetaceannetworks.com!kclark (GnuPG ID: B280F24E) alumni.unh.edu!kdc ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
Mark Komarinski [EMAIL PROTECTED] writes: On Wed, Jan 15, 2003 at 02:47:56PM -0500, Kevin D. Clark wrote: How about $_ instead of $0 ? _: undefined variable. (I just tested this; it works for me) I'm not trying to make an incendiary comment here. If whatever csh-flavored shell you're using doesn't fill in $_ appropriately, then I think that this is another csh incompatability that needs to be dealt with. Regards, --kevin -- Kevin D. Clark / Cetacean Networks / Portsmouth, N.H. (USA) cetaceannetworks.com!kclark (GnuPG ID: B280F24E) alumni.unh.edu!kdc ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
The short answer is: it can't be done, at least not in any manner that won't cause projectile vomiting, so just remember that you asked... A hack like this might start with the understanding that scripts are not, in themselves, executable. What's really happening when you execute a script is that the appropriate program (like bash or perl or tcl or whatever) is secretly launched and the script is fed to it for interpretation. That, in turn, usually means that (at least) one of that interpreter's file descriptors will refer to the file that the script is coming from. So, if you're truly twisted you might rummage around in /proc/pidOfInterest/fd/ and see what you can find. For example, I note that descriptor 255 seems to refer to the script in question on my 2.4.18 Debian system when I'm executing bash scripts. [ Note that pidOfInterest will be that of the interpreter (csh in your case) that's executing your script, typically available as $$ ] Of course, there's a whole lot a ways this approach can fail - one (of many) that immediately comes to mind is if the script is being piped to you as stdin from another process. For the record: trickery like this is ugly, guaranteed to be non-portable and causes cancer - you should be forced to swim 50 laps in a septic tank if you ever attempt to put a hack like this into service. ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
Another solution, albeit extremely fugly, would be to 'exec somenewscriptname' from csh that would have #! /bin/sh or whatever defined and then have that script execute in a bash environment and do whatever machinations you need. I don't envy - the constraints are obvious, and obviously painful... HTH, Bayard ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
Some followup examples, with the last one showing how it can fail: shrapnel:/tmp 165--- cat /tmp/nastyHack ; chmod a+x /tmp/nastyHack cd $* # Stand in specified directory ($HOME if none), echo PWD is $PWD# confirm our location, ls -CFl /proc/$$/fd # demo the concept. shrapnel:/tmp 166--- cd / ; /tmp/nastyHack PWD is /home/mod total 0 lrwx--1 mod mod64 Jan 15 17:39 0 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:39 1 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:39 2 - /dev/pts/3 lr-x--1 mod mod64 Jan 15 17:39 255 - /tmp/nastyHack* shrapnel:/ 167--- cd / ; /tmp/nastyHack /etc PWD is /etc total 0 lrwx--1 mod mod64 Jan 15 17:39 0 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:39 1 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:39 2 - /dev/pts/3 lr-x--1 mod mod64 Jan 15 17:39 255 - /tmp/nastyHack* shrapnel:/ 168--- cd /tmp ; ./nastyHack /usr/local PWD is /usr/local total 0 lrwx--1 mod mod64 Jan 15 17:40 0 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:40 1 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:40 2 - /dev/pts/3 lr-x--1 mod mod64 Jan 15 17:40 255 - /tmp/nastyHack* shrapnel:/tmp 169--- cd /var/log ; bash /tmp/nastyHack PWD is /home/mod total 0 lr-x--1 mod mod64 Jan 15 17:40 0 - /tmp/nastyHack* lrwx--1 mod mod64 Jan 15 17:40 1 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:40 2 - /dev/pts/3 shrapnel:/var/log 170--- cd /var/log ; cat /tmp/nastyHack | bash PWD is /home/mod total 3 lr-x--1 mod mod64 Jan 15 17:45 0 - pipe:[364424] lrwx--1 mod mod64 Jan 15 17:45 1 - /dev/pts/3 lrwx--1 mod mod64 Jan 15 17:45 2 - /dev/pts/3 ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss
Re: Where am I (csh)
On Wed, 15 Jan 2003, at 2:57pm, [EMAIL PROTECTED] wrote: Did I ever mention that life in academics is a lot different from life in the business world? Yah, in the business world, they want you to do everything you have to do in academia, and make a profit, too. -- Ben Scott [EMAIL PROTECTED] | The opinions expressed in this message are those of the author and do not | | necessarily represent the views or policy of any other person, entity or | | organization. All information is provided without warranty of any kind. | ___ gnhlug-discuss mailing list [EMAIL PROTECTED] http://mail.gnhlug.org/mailman/listinfo/gnhlug-discuss