LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-03-2003, 03:53 PM   #1
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Rep: Reputation: 15
ksh functions in .kshrc


What is the proper syntax for functions in the .kshrc file?

I'm trying to create a function called cdl which cd's into the directory listed after "cdl" then ls's everything inside the directory. In bash, I was able to get it to work using:

function cdl() {
cd $1
ls
}


but this doesn't seem to work in ksh. in ksh, i also tried:

function cdl {
cd $1
ls
}

or something like this, and what it would do is list everything in the directory that was listed after "cdl", but it wouldnt actually go into that directory.

Thanks for any help.

Last edited by brian0918; 06-05-2003 at 10:12 AM.
 
Old 06-03-2003, 04:35 PM   #2
snocked
Member
 
Registered: Dec 2002
Location: St. Louis, MO
Distribution: Slackware 9.1
Posts: 482

Rep: Reputation: 30
Why didn't you try the same thing that's in .bashrc?

function cdl() {
cd $1
ls
}
 
Old 06-03-2003, 04:44 PM   #3
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally posted by snocked
Why didn't you try the same thing that's in .bashrc?

function cdl() {
cd $1
ls
}
He did try that if you reread his post more carefully.

What is $1 ? What are you exporting or defining as $1 ? Maybe that is the reason why its not doing nothing.

Take a look at this example .kshrc file: http://publibn.boulder.ibm.com/doc_l...iles/kshrc.htm
 
Old 06-03-2003, 05:01 PM   #4
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
what i want is something similar to:

cd asdfadf

which goes into the directory...

but instead,

cdl asdfadf

will go into the directory, then display its contents. i thought i could use $1 to represent aasdfadf
 
Old 06-04-2003, 03:59 PM   #5
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Instead of using the function, its much easier to just create an alias like this:

alias cdl="cd $* | ls"

And that should work and function the same way.
 
Old 06-04-2003, 05:24 PM   #6
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
I just tried this in ksh, and it didn't work. I'm having the same problem stated in my first post... "what it would do is list everything in the directory that was listed after "cdl", but it wouldnt actually go into that directory."

in .kshrc, i added:
alias cdl='cd $* | ls'
and it did the same thing...... it lists what's in the $* directory, but doesn't go into that directory. it's as if it's ignoring everything before ls, then just executing: ls directory_name

Last edited by brian0918; 06-04-2003 at 05:28 PM.
 
Old 06-05-2003, 09:47 AM   #7
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Well....?
 
Old 06-05-2003, 10:02 AM   #8
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally posted by brian0918
Well....?
Patience, can't people sleep or work..

Double Quotes.. not Single Quotes.. notice mine:

alias cdl="cd $* | ls"

And then yours:

alias cdl='cd $* | ls'

And you can also just type this at a command and then test it out.. sometimes before it takes affect when placing in your .kshrc file, you have to logout and back in so it rereads your file.
 
Old 06-05-2003, 10:05 AM   #9
snocked
Member
 
Registered: Dec 2002
Location: St. Louis, MO
Distribution: Slackware 9.1
Posts: 482

Rep: Reputation: 30
Quote:
Originally posted by trickykid
He did try that if you reread his post more carefully.

What is $1 ? What are you exporting or defining as $1 ? Maybe that is the reason why its not doing nothing.

Take a look at this example .kshrc file: http://publibn.boulder.ibm.com/doc_l...iles/kshrc.htm
No, he tried:

function cdl {
cd $1
ls
}
 
Old 06-05-2003, 10:10 AM   #10
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by trickykid
Patience, can't people sleep or work..

Double Quotes.. not Single Quotes.. notice mine:

alias cdl="cd $* | ls"

And then yours:

alias cdl='cd $* | ls'

And you can also just type this at a command and then test it out.. sometimes before it takes affect when placing in your .kshrc file, you have to logout and back in so it rereads your file.
Didn't work. Same problem. What's the difference between ' and " ???? All my aliases use ' and they all work fine, except for this one.

Also, why do I need to log out? Can't I just type: . .kshrc
 
Old 06-05-2003, 10:29 AM   #11
snocked
Member
 
Registered: Dec 2002
Location: St. Louis, MO
Distribution: Slackware 9.1
Posts: 482

Rep: Reputation: 30
Yes you can just type . .kshrc
 
Old 06-05-2003, 10:30 AM   #12
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
Quote:
Originally posted by snocked
No, he tried:

function cdl {
cd $1
ls
}
Keyphrase is "In bash, I was able to get it to work using:
[size=large]but this doesn't seem to work in ksh[/size]. in ksh, i also tried:"

That indicates he tried both using the ksh shell to me...

But as for you problem, I'm not sure why its not working for you, as I tested it on my own machine using the korn shell and it works with no problems..
Did you try typing it at the shell prompt to test?

Code:
shell prompt> alias cdl="cd $* | ls"
shell prompt> alias
After typing the command to set the alias, just type alias at the command line and it will list all your current alias's, see if it shows up in that list for ya or not to verify.
 
Old 06-05-2003, 10:55 AM   #13
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984Reputation: 1984
drew.... why the fsck are you piping a cd into ls???

alias cdl='cd $*; ls'

or maybe better:

alias cdl='cd $* && ls'


Last edited by acid_kewpie; 06-05-2003 at 10:56 AM.
 
Old 06-05-2003, 11:04 AM   #14
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
My bad, it is single quote.. but as acid pointed out.. there are several ways to do it using alias..

Last edited by trickykid; 06-05-2003 at 11:05 AM.
 
Old 06-05-2003, 11:17 AM   #15
brian0918
Member
 
Registered: Apr 2003
Posts: 87

Original Poster
Rep: Reputation: 15
Quote:
Originally posted by acid_kewpie
drew.... why the fsck are you piping a cd into ls???

alias cdl='cd $*; ls'

or maybe better:

alias cdl='cd $* && ls'

That didn't work either. I tried both ways shown above, and what it does is go back to my home directory (as if it's reading the cd by itself). I also tried it with quotes around $*, and all it does is the same thing it's always done: ls the contents of the named directory, but never go into that directory.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Converting php5 socket functions to php3 socket functions mrobertson Programming 0 06-23-2005 09:11 AM
The ksh command Gins Linux - General 10 11-20-2004 06:56 PM
Doesnt source .kshrc when ksh is started pkan Linux - Newbie 2 03-08-2004 07:54 PM
function in .kshrc to cd then ls brian0918 Linux - Newbie 1 06-04-2003 02:42 PM
pointers to functions/member functions champ Programming 2 03-28-2003 06:22 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:45 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration