Why does /bin contain binaries for builtins?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Why does /bin contain binaries for builtins?
# 8  
Old 01-20-2011
Quote:
Originally Posted by methyl
On HP-UX "/usr/bin/cd" is actually a Posix Shell script.
I never understood that, how on earth would a cd scriptwork? cd needs to change the current context's dir, running a script generally creates a new context..
# 9  
Old 01-20-2011
Are you ready for this? In my mind this can only work if the Shell executes the script within the current Shell.

Code:
#!/usr/bin/sh
# @(#)B.11.11_LR 
# This is the executable version of cd implemented using the
# posix shell built-in cd command.
cd "$@"
exit $?

Just tried it. From the command line doesn't work. From a Shell script it doesn't work.
Good catch Corona688.

Last edited by methyl; 01-20-2011 at 05:47 PM..
# 10  
Old 01-20-2011
It's a long story. Some vendor added echo as a builtin command to sh and csh and then dropped the echo binary. This broke lots of stuff. It was easily fixed by installing an echo binary but it generated a lot of interest. Then Posix convened. They said that builtins are great but you must have executable binaries for all builtin commands. They did not really mean "cd" but a strict reading of the spec implied that they did. And an executable version of cd is not tough to write. Solaris 10 has a ksh script. I would post it, but it's copyrighted!

The external cd command is largely useless although HP did concoct a convoluted example of where it could be used it conjuction with "find -exec" to test if it's possible to cd to a directory or not.

BTW I often run /bin/pwd to be sure that I get my physical location.

Code:
$
$ cd /usr/local
$ pwd
/usr/local
$ /bin/pwd
/opt/sfw
$ ls -l /usr/local
lrwxrwxrwx   1 root     root           8 Nov 25  2008 /usr/local -> /opt/sfw
$

These 5 Users Gave Thanks to Perderabo For This Post:
# 11  
Old 01-20-2011
Methyl: That must be the fruit of crazed and draconic source control procedures. Or someone in management deciding builtins are evil and bad.
Per: Ah... not too far off, then.
# 12  
Old 01-20-2011
If we can roll the clock back I prefer the binaries which behave exactly as defined in the "man" pages whether or not you specify the path. The various POSIX and BSD extensions are useful in a single platform environment but a real problem when writing portable Scripts. To be fair, there is a similar issue on M$ platforms.
Never had this issue with Novell Netware.


Apologies @gelv2005 for hijacking your thread. Interesting subject with real impact in the commerical world.

Last edited by methyl; 01-20-2011 at 06:24 PM..
# 13  
Old 01-20-2011
Quote:
Originally Posted by methyl
Are you ready for this? In my mind this can only work if the Shell executes the script within the current Shell.
Code:
#!/usr/bin/sh
# @(#)B.11.11_LR 
# This is the executable version of cd implemented using the
# posix shell built-in cd command.
cd "$@"
exit $?

Just tried it. From the command line doesn't work. From a Shell script it doesn't work.
It does actually, at least as designed. There is a similar cd executable in Solaris and probably most Unixes, at least those POSIX compliant as the standard mandate that all utilities can be exec-ed, regardless of the usefulness: http://pubs.opengroup.org/onlinepubs...l#tag_23_01_07

/usr/bin/cd works as documented (quoted from Solaris cd manual page Man Page for cd (OpenSolaris Section 1) - The UNIX and Linux Forums ):
DESCRIPTION
/usr/bin/cd
The /usr/bin/cd utility changes the current directory in the
context of the cd utility only. This is in contrast to the
version built into the shell. /usr/bin/cd has no effect on
the invoking process but can be used to determine whether or
not a given directory can be set as the current directory.

Edit: Sorry, I overlook Perderabo had already answered to methyl's posting.
---------- Post updated 21-01-11 at 00:14 ---------- Previous update was 20-01-11 at 23:38 ----------

Quote:
Originally Posted by Perderabo
Solaris 10 has a ksh script. I would post it, but it's copyrighted!
OpenSolaris has an interesting code that implements all of these utilities:
Cross Reference: /onnv/onnv-gate/usr/src/cmd/ksh/builtins/alias.c

Last edited by jlliagre; 01-20-2011 at 06:45 PM..
# 14  
Old 01-20-2011
No problem. The fact remains that when invoked as /usr/bin/cd it does not behave the same as a plain "cd" because it starts a new Shell. I am well aware that the many and various POSIX "standards" over the years contain anomalies.


The HP-UX implementation of the binary "pwd" is more interesting because I have used the fact that it behaves correctly (as distinct from the POSIX Shell version) to produce consistent portable scripts.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Usage of #!/bin/sh vs #!/bin/bash shell scripts?

Some question about the usage of shell scripts: 1.) Are the commands of the base shell scripts a subset of bash commands? 2.) Assume I got a long, long script WITHOUT the first line. How can I find out if the script was originally designed für "sh" or "bash"? 3.) How can I check a given... (3 Replies)
Discussion started by: pstein
3 Replies

2. AIX

Redistribution bin required for AIX. j7r164redist.7.1.0.25.bin

Hi, I am planning to install a version of Informatica on my AIX box. It requires a specific java build in pap6470_27sr2-20141101_01(SR2). The current link for IBM 64-bit SDK for AIX®, JavaTM Technology Edition, Version 7 Release 1 has a more recent version in j7r164redist.7.1.0.75.bin. Is... (4 Replies)
Discussion started by: meetpraveens
4 Replies

3. UNIX for Dummies Questions & Answers

Shell and bash builtins...

Not sure if this is the right forum but I have collated a listing of shell and bash builtins. Builtins is a loose word and may include the '/bin' drawer/folder/directory but NOT any others in the path list. In the case of my Macbook Pro, OSX 10.7.5 the enabled internals is also listed... ... (1 Reply)
Discussion started by: wisecracker
1 Replies

4. Solaris

What is the difference between xpg4/bin and usr/bin?

Hi Experts, I found that the same commands(sort, du, df, find, grep etc.) exists in both dir. What is the difference to use them? i.e: to use xpg4/bin/grep and usr/bin/grep My OS version is SunOS 5.10 Regards, Saps (7 Replies)
Discussion started by: saps19
7 Replies

5. OS X (Apple)

When to use /Users/m/bin instead of /usr/local/bin (& whats the diff?)?

Q1. I understand that /usr/local/bin means I can install/uninstall stuff in here and have any chance of messing up my original system files or effecting any other users. I created this directory myself. But what about the directory I didn't create, namely /Users/m/bin? How is that directory... (1 Reply)
Discussion started by: michellepace
1 Replies

6. UNIX for Advanced & Expert Users

Writing Custom Builtins for KSH93

I am looking to create some ksh93 extensions using the custom builtin feature. I can successfully create a builtin function, load it using the builtin -f command and get an output. However, I want to get/set values of KSH variables from within my built-in. For example, lets say I am creating... (2 Replies)
Discussion started by: a_programmer
2 Replies

7. Programming

pthread_mutex_lock in ANSI C vs using Atomic builtins of GCC

I have a program which has 7-8 threads, and lots of shared variables; these variables (and also they may not the primitive type, they may be enum or struct ), then they may read/write by different threads at the same time. Now, my design is like this, typedef unsigned short int UINT16;... (14 Replies)
Discussion started by: sehang
14 Replies

8. Shell Programming and Scripting

program name and function name builtins

Hi Is there a way to get the program/script name or function name usng built ins. Like in many languages arg holds the program name regards (2 Replies)
Discussion started by: xiamin
2 Replies

9. UNIX for Dummies Questions & Answers

fuser: difference with bin/sh and bin/ksh shell script

Hi, I have a problem I don't understand with fuser. I launch a simple shell script mysleep.sh: I launch the command fuser -fu mysleep.sh but fuser doesn't return anything excepted: mysleep: Then I modify my script switching from #!/bin/sh to #!/bin/ksh I launch the command fuser -fu... (4 Replies)
Discussion started by: Peuj
4 Replies

10. UNIX for Dummies Questions & Answers

/bin/sh: /usr/bin/vi: No such file or directory when doing crontab

I just set up an ftp server with Red Hat 5.2. I am doing the work, I'm baby stepping, but it seems like every step I get stuck. Currently, I'm trying to set up a crontab job, but I'm getting the following message: /bin/sh: /usr/bin/vi: No such file or directory. I see that vi exists in /bin/vi,... (3 Replies)
Discussion started by: kwalter
3 Replies
Login or Register to Ask a Question