LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   ksh functions in .kshrc (https://www.linuxquestions.org/questions/linux-newbie-8/ksh-functions-in-kshrc-63480/)

brian0918 06-03-2003 03:53 PM

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.

snocked 06-03-2003 04:35 PM

Why didn't you try the same thing that's in .bashrc?

function cdl() {
cd $1
ls
}

trickykid 06-03-2003 04:44 PM

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

brian0918 06-03-2003 05:01 PM

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

trickykid 06-04-2003 03:59 PM

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.

brian0918 06-04-2003 05:24 PM

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

brian0918 06-05-2003 09:47 AM

Well....?

trickykid 06-05-2003 10:02 AM

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.

snocked 06-05-2003 10:05 AM

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
}

brian0918 06-05-2003 10:10 AM

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

snocked 06-05-2003 10:29 AM

Yes you can just type . .kshrc

trickykid 06-05-2003 10:30 AM

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... :rolleyes:

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.

acid_kewpie 06-05-2003 10:55 AM

drew.... why the fsck are you piping a cd into ls??? :D

alias cdl='cd $*; ls'

or maybe better:

alias cdl='cd $* && ls'


trickykid 06-05-2003 11:04 AM

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

brian0918 06-05-2003 11:17 AM

Quote:

Originally posted by acid_kewpie
drew.... why the fsck are you piping a cd into ls??? :D

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.


All times are GMT -5. The time now is 11:22 PM.