LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-30-2009, 09:25 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Shell script: can you tell if a script is called via an alias?


Say I have an executable shell script called "foo"...

and then I alias to it like so:

alias bar foo

so now if I run "foo" or "bar", the same shell script runs.

Is there a way, in the shell script, that I can tell if I was called as "foo" or "bar"? In tcsh, I was checking $0, but that always seems to be "foo" even if I ran it as "bar". I don't care if it's tcsh or bash or even a python script (preferably not perl or anything that can't be interpreted on the fly, like compiled C).
 
Old 09-30-2009, 09:29 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
no,
the shell converts it before it runs
 
Old 09-30-2009, 10:03 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
That's unfortunate.

Back to the drawing board.
 
Old 09-30-2009, 10:11 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
can you tell us your intentions
i understand if you cant but it would help solve the problem
 
Old 10-01-2009, 01:33 AM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,434

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
If you create a *link* 'bar' to 'foo', then when you run it, it can know it was called as 'bar' or 'foo' -at least in sh/bash. This method is used for rc.0/rc.6 shutdown items -rc.0 is a link to rc.6. The called name tells the script what to do(this is under Slackware -YMMV).
 
Old 10-01-2009, 01:36 AM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
but the original poster said alias not symlink
 
Old 10-01-2009, 01:44 AM   #7
bangaram
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Rep: Reputation: 0
not sure if it fits ur purpose....i tried this ,


<$>vi test.skh
"test.skh" [New file]
echo "running $0 "
:wq!


first run:test.skh
<$>./test.skh
running ./test.skh

second run:test.ksh
<$>ln -s test.skh test.ksh
<$>test.ksh
running ./test.ksh
<$>ln -s test.skh test1.ksh
3rd Run:
<$>test1.ksh
running ./test1.ksh
 
Old 10-01-2009, 01:49 AM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
Quote:
Originally Posted by BrianK View Post
Say I have an executable shell script called "foo"...

and then I alias to it like so:

alias bar foo

so now if I run "foo" or "bar", the same shell script runs.

Is there a way, in the shell script, that I can tell if I was called as "foo" or "bar"? In tcsh, I was checking $0, but that always seems to be "foo" even if I ran it as "bar". I don't care if it's tcsh or bash or even a python script (preferably not perl or anything that can't be interpreted on the fly, like compiled C).
got it?
 
Old 10-01-2009, 02:08 AM   #9
bangaram
LQ Newbie
 
Registered: Aug 2009
Posts: 6

Rep: Reputation: 0
Quote:
Originally Posted by smeezekitty View Post
got it?
yes,i pasted o/p for all my 3 runs...
 
Old 10-01-2009, 02:31 AM   #10
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
sorry for the outbusrt
i was trying to say the original poster is talking about alias not ln
 
Old 10-01-2009, 08:08 AM   #11
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,434

Rep: Reputation: 303Reputation: 303Reputation: 303Reputation: 303
The OP said: "Back to the drawing board", so the suggestion about using links is a way to implement what he seemed to be trying to do -differentiate between the way a single script 'identifies' itself.
 
Old 10-01-2009, 11:16 AM   #12
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
Hello all,

After I replied, I remember that I had previously done this via symlinks as you suggested. That doesn't work in this particular case, however.

For the record, I'm writing a controller for a slew of applications - groups of which need their own, unique PATHs (and a few other env vars). Rather than polluting each user's env with a ton of potentially conflicting paths, the controller is setting paths on the fly, then launching the application. When the app closes, all env vars go back to what they were before.
The purpose of the alias is to send all the potential application executables through the controller script. Being that the aliases, themselves, are created on the fly, I've simply appended some extra info to each alias, i.e. "--prog_group foo --app_name bar". Problem solved.

Thanks for all the suggestions.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux based java decompiler to be called from shell script rakeshrau Linux - Newbie 2 06-25-2009 08:40 AM
custom shell script interpreter is not called on my vserver empgodot Linux - Server 2 05-31-2009 07:40 AM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 02:34 PM
alias does not work in shell script mannahazarika Programming 9 01-20-2006 01:56 PM
Delay in shell script or alias jeopardyracing Linux - Newbie 6 09-21-2004 08:07 PM


All times are GMT -5. The time now is 03:56 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration