UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 2nd, 2003, 10:22 AM
vslewis vslewis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Phoenix
Posts: 5 vslewis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 40 m 44 sec
Reputation Power: 0
Angry ksh alias arguments

is there a way to defien in an 'alias' where the command-line arguments are used??
as an example I will use a csh alias:
alias dir 'ls -alrtF \!* | more'

Reply With Quote
  #2  
Old December 2nd, 2003, 12:20 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
No. In ksh, an alias only renames a command. Use a function instead.

dir() { ls -alrtF $* ; }

Reply With Quote
  #3  
Old December 6th, 2003, 10:52 PM
jayakhanna jayakhanna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: India
Posts: 61 jayakhanna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I am able to do this in Solaris 5.8. If I understood the problem rightly then

alias dir='ls -l'

then doing *dir* does the same function as *ls -l*

Regards
__________________
Regards
JK

Reply With Quote
  #4  
Old December 6th, 2003, 10:56 PM
jayakhanna jayakhanna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: India
Posts: 61 jayakhanna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
I missed out to add these also

# alias dir='ls -l $*'
# dir demo.c
-rw-r--r-- 1 root other 1279 Jan 17 2003 demo.c
# dir demo.c fchmod.c
-rw-r--r-- 1 root other 1279 Jan 17 2003 demo.c
-rw-r--r-- 1 root other 3702 Jan 28 2003 fchmod.c
#

See we can give the command line arugments also using alias

Reply With Quote
  #5  
Old December 9th, 2003, 07:26 AM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
Quote:
Originally posted by jayakhanna
I missed out to add these also

# alias dir='ls -l $*'
# dir demo.c
-rw-r--r-- 1 root other 1279 Jan 17 2003 demo.c
# dir demo.c fchmod.c
-rw-r--r-- 1 root other 1279 Jan 17 2003 demo.c
-rw-r--r-- 1 root other 3702 Jan 28 2003 fchmod.c
#

See we can give the command line arugments also using alias


But only if the rename is enough, which is rarely the case. Notice that the OP really wanted the output of ls piped though more. So a more complete function would have been:

dir() { ls -alrtF $* | more ; }

Try that with an alias...

Reply With Quote
  #6  
Old December 9th, 2003, 07:53 AM
jayakhanna jayakhanna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: India
Posts: 61 jayakhanna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Dear,
It also work with more try this

alias dir='ls -l $* | more'

$ dir

It has the same behaviour. I tried this in Solaris 5.8



Reply With Quote
  #7  
Old December 9th, 2003, 09:35 PM
Perderabo Perderabo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 121 Perderabo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 54 sec
Reputation Power: 5
I just tried on solaris 5.8 using ksh and it failed. What shell are you using?

Try this:

set one two three
alias xyz='echo $* ; echo next line ; echo '
xyz four five
xyz six seven


That $* in the alias will expand to "one two three". The arguments that you pass to the alias get tacked on to the end of the command line.

If you issue your alias:
dir="ls -l $* | more'
and then do
dir /etc/passwd
you will indeed pass an argument to the alias and the result will basicly boil down to:
more /etc/passwd
the "ls -l one two three" may succeed if you have those files, but more will ignore the output of ls in this case. Or you may see errors about one two and three not existing.

While you may chuckle with delight over the functionality of this wonderful alias, try to understand that it's not what the OP was looking for. He wanted the $* to be replaced with the arguments that he passed to the alias. In ksh, that will require a function.

Reply With Quote
  #8  
Old December 12th, 2003, 06:46 AM
jayakhanna jayakhanna is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: India
Posts: 61 jayakhanna User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
Yeah you are right

Quote:
True knowledge is in knowing that you don' t know anything


Cheers
JK

Reply With Quote
  #9  
Old May 11th, 2006, 03:08 AM
baobao baobao is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 baobao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 18 sec
Reputation Power: 0
alias

hi ther,

i just tried this syntax u hav posted:

dir() { ls -l ; }

-and it works. but how come after launching another session, the defined alias is gone/missing. so it cannot use by other sessions. how can i define this so other terminals can use also.

appreciate for any feedback.

Thanks.

Reply With Quote
  #10  
Old May 11th, 2006, 04:44 AM
SimonJM SimonJM is offline
Contributing User
Dev Shed Novice (500 - 999 posts) Click here for more information
 
Join Date: Mar 2006
Posts: 762 SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level)SimonJM User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 22 h 8 m 4 sec
Reputation Power: 336
If you want it to be for every seesion you need to put the function definition (or alias if you wish to use that instead) in the .profile of the user.

Reply With Quote
  #11  
Old May 13th, 2006, 09:44 PM
baobao baobao is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2006
Posts: 2 baobao User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 m 18 sec
Reputation Power: 0
Smile to SimonJM

thanks for the advice, however, i just did, but stil not working. after adding to .profile, do i need to restart any service? i just insert the command:

#
dir() { ls -l ; }

-- thats it. anything i missed out.

pls help. Thanks a lot for your time.

Reply With Quote
  #12  
Old May 13th, 2006, 10:29 PM
Ehlanna's Avatar
Ehlanna Ehlanna is offline
Not a clue what to put ...
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2006
Location: in front of this keyboard
Posts: 815 Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level)Ehlanna User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Weeks 2 Days 5 h 43 m 48 sec
Reputation Power: 243
That should work - you WILL need to source the .profile (either by logging off and on again or by . .profile). This is, of course, assuming your shell is one that uses .profile (ksh, etc.). If not, then you'd need to put the function defintion in the correct file: .cshrc (I think, for the C shell).

In any event, doing that with an alias would be better!

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > ksh alias arguments


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT