Cd


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cd
# 15  
Old 10-03-2001
Perhaps it would help if we looked at /usr/bin/cd, not from the context of listing files in a directory in an interactive shell, but for a command sequence:

NOTE: MAY NOT WORK.... TRY WITH EXTREME CAUTION !!!

/usr/bin/cd /tmp; rm -rf *

NOTE: MAY NOT WORK.... TRY WITH EXTREME CAUTION !!!

This would appear, if I'm not mistaken, to spawn a child process that would remove all the files in the /tmp directory. After that task completes and the child process exists; the original shell process and current directory would be the same as before the command sequence was executed.

Another example might be as simple as:

/usr/bin/cd /usr/spool/mail; ls -la > /tmp/mail_stuff

One could argue that the sequence:

ls -la /usr/spool/mail > /tmp/mail_stuff

would accomplish the same thing.... agreed.

However, it seems that there are numerous possible scenarios where /usr/bin/cd could be useful when used in a command line sequence and subshell
# 16  
Old 10-03-2001
That's a better reason than I could think of Smilie
# 17  
Old 10-04-2001
Well, I just wanted to add that on Solaris /usr/bin/cd is a two-line shell script.... It does what any other shell script does.... On HP-UX I am not sure what it is at the moment, but I will check it out tomorrow....

I can honestly say that I have never encountered a problem which required the use of this 2-line shell script...

For the record, here it is:

cmd=`basename $0`
$cmd "$@"


That's it.. Nothing more....

Quoth the raven - nevermore...

- dEvNuL
# 18  
Old 10-04-2001
From the HP-UX /usr/bin/cd:

#!/usr/bin/sh
# @(#) $Revision: 72.2 $

# This is the execable version of cd implemented using the
# posix shell built-in cd command.

cd $@
exit $?


Odd... I never even thought to check if it were binary...
# 19  
Old 10-04-2001
Quote:
Originally posted by Neo

/usr/bin/cd /tmp; rm -rf *

This would appear, if I'm not mistaken, to spawn a child process that would remove all the files in the /tmp directory.
Sorry, that won't work. I sure hope nobody tried that. Try this instead:
/usr/bin/cd /tmp; /usr/bin/pwd

Even when commands are stacked with semicolons, we still have a shell processing the command line. And it still must spawn a child, let the child run /usr/bin/cd, wait for the child to die, spawn a second child, which will run /usr/bin/pwd.

Quote:
Originally posted by devnul
Well, I just wanted to add that on Solaris /usr/bin/cd is a two-line shell script....

cmd=`basename $0`
$cmd "$@"
That's it.. Nothing more...
Actually there are some comments as well which date it Feb 6, 1995 and it has a 1995 copyright. This gives us some idea of when it appeared. 1995 does sound about right.

HP's version is also a simple script:
cd $@
exit $?

HP doesn't show a date, but it does have a comment showing that it is revision 72.2. It's hard to imagine 72 revisions on a script like this, but I guess it's not a job you assign to your best programmer.

The format of Sun's script shows that it's using its name to find the command. I got a list of all the files in /usr/bin linked to the same file. The list is alias, bg, cd, command, fc, fg, getopts, hash, jobs, kill, read, test, type, ulimit, umask, unalias, and wait. Only kill and test really make any sense. Oh well.
# 20  
Old 10-04-2001
I can't actually test on any of our systems; because none of them have /usr/bin/cd........


I agreed!!! Don't try the rm example above unless you are in a very safe directory with only temporary/test files. I don't have any way to test since /usr/bin/cd does not exist here.

This has been very interesting...... but I'm not sure what it all means, at this point. Are there any lessons learned?
# 21  
Old 10-04-2001
The AIX version seems to explain it best. It states that this
is to provide exec()-able versions of shell built-ins...
Code:
#!/usr/bin/psh
# @(#)36        1.2  src/bos/usr/bin/ksh/exec-builtin.sh, cmdksh, bos430, 9737A_
430 6/1/95 00:09:57
#
# COMPONENT_NAME: (CMDKSH) Korn Shell
#
# FUNCTIONS:
#
# ORIGINS: 27
#
#
# This shell script gets installed in /usr/bin to provide an exec()-able
# version of certain shell builtins:
#       alias           command         getopts         umask
#       bg              fc              jobs            unalias
#       cd              fg              read            wait
#       hash            type
#
# These are also builtins, but already have historical version in /usr/bin:
#       false           kill            newgrp          true
#
command=`/usr/bin/basename $0`          # Invoke the ksh builtin
if [ "$command" = "type" ]
then
        whence -v "$@"
elif [ "$command" = "hash" ]
then
        alias -t - "$@"
else
        $command "$@"
fi

...interestingly though it seems to execute in the Perl Shell
#!/usr/bin/psh but I haven't verified this.

added code tags for readability --oombera

Last edited by oombera; 02-20-2004 at 11:06 AM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question