Operating System - HP-UX
1759677 Members
3514 Online
108886 Solutions
New Discussion юеВ

Why my newcd got problem?

 
SOLVED
Go to solution
zhaogui
Super Advisor

Why my newcd got problem?

I found that I have to provide a parameter to "cd" command in order for my newcd to work properly. Can I do without this parameter?

Here is my .kshrc
set -o vi
set -o monitor
alias ll='ls -la'
alias setprmt='PS1=$HOST"["$USER"]:"${PWD##*/}" "'
setprmt
alias cd=newcd
function newcd
{
\cd "$@" >&- || return 1
setprmt
}

Can anybody tell me if the script for newcd is correct or not, why need \cd "$@" >&- and how to interpret it? Does it support null parameter?
Thanks,
8 REPLIES 8
Ceesjan van Hattum
Esteemed Contributor

Re: Why my newcd got problem?

As far as i can see, your cd is indeed just a bit different than normal:

$@=$1$2$3...
($*=$1,$2,$3... where ,=IFS)
<&- = close standard output

In other words, \cd is just the cd with all arguments concatenated. If it failes (||=OR) it returns 1, else it will continue with setting a nice-looking promt (setprmt=PS1-setting).

if you do a 'cd a /b'
i suppose it will do a
cd a/b
, 'cause it should delete the spaces.

If it was upto me, i would not set this newcd.

Regards,
Ceesjan
zhaogui
Super Advisor

Re: Why my newcd got problem?

Here is the error message I got when I simply type 'cd' without any parameter,
myhost[myuser]:opc_op cd
newcd[2]: @: parameter not set
Normal cd will move to my home directory as what 'cd ~' does. How come my newcd cannot?
Other doubts:
1. why there is \ before cd? If I remove it, I will get this,
myhost[myuser]:opc_op cd /usr
ksh: newcd: recursion too deep
2. what is close standard output for &-? Is it the same as normal standard output 1> ?
zhaogui
Super Advisor

Re: Why my newcd got problem?

Another interesting doubt is, why the .kshrc always run after all commands in .profile finished?

.kshrc is the same as mentioned above.
Here is .profile,
# Set up the shell variables:
EDITOR=vi
export EDITOR

ENV='${START[(_$-=1)+(_=0)-(_$-!=_${-%%*i*})]}'
START=$HOME/.kshrc
export ENV START
LANG=C ; export LANG
PWD=$HOME ; export PWD
SHELL=/bin/ksh ; export SHELL
HOST=`uname -n` ; export HOST
USER=`logname` ; export USER
echo $$
Peter Kloetgen
Esteemed Contributor

Re: Why my newcd got problem?

Hi Zhaogui,

your .kshrc is run after the .profile because you have the entry:

ENV=.........

This variable tells your system, to source the .kshrc- file for each new kornshell which is started.

and your newcd should work better, if you first run a test- command, to check if there are any parameters given to the function:

if test $# -ne 0
then
your_function_command
else
cd
fi

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
Peter Kloetgen
Esteemed Contributor
Solution

Re: Why my newcd got problem?

Hi again,

of course the whole construction has to look like this:

function newcd
{
if test $# -ne 0
then
\cd "$@" >&- || return 1
setprmt
else
cd
fi
}

Allways stay on the bright side of life!

Peter

I'm learning here as well as helping
zhaogui
Super Advisor

Re: Why my newcd got problem?

Good progress! it can accept cd without any parameter, but still got another error message. here it is,
myhost[myuser]:opc_op cd /usr
myhost[myuser]:usr cd
ksh: newcd: recursion too deep

I suspect this could be due to the endless loop of recursion, because cd calls newcd first and then call cd again, and call newcd again and again...

Peter Kloetgen
Esteemed Contributor

Re: Why my newcd got problem?

Hi Zhaogui,

what happens if you delete the ">&-"?

And try to do the same script without "|| return 1".

Allways stay on the bright side of life!

Peter
I'm learning here as well as helping
zhaogui
Super Advisor

Re: Why my newcd got problem?

I got it, here is my new .kshrc,
set -o vi
set -o monitor
alias ll='ls -la'
alias setprmt='PS1=$HOST"["$USER"]:"${PWD##*/}" "'
setprmt
alias cd=newcd
function newcd
{
if test $# -ne 0
then
\cd "$@" >&- || return 1
setprmt
else
\cd
setprmt
fi
}
echo "End of kshrc:"
You see, now I understand why I must put \ before every cd and why we need to create such a newcd in ksh environment.
Thank you very much for your help. I will assign points to you.