Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!husc6!cmcl2!rutgers!sunybcs!boulder!hao!noao!arizona!rupley From: rupley@arizona.edu (John Rupley) Newsgroups: comp.unix.wizards,comp.unix.questions Subject: Re: The whole prompt string thing (was: PS1 and the bourne shell...) Message-ID: <3075@megaron.arizona.edu> Date: Thu, 3-Dec-87 17:03:08 EST Article-I.D.: megaron.3075 Posted: Thu Dec 3 17:03:08 1987 Date-Received: Mon, 7-Dec-87 06:29:28 EST References: <279@caus-dp.UUCP> <1311@puff.wisc.edu> <137@anumb.UUCP> <305@caus-dp.UUCP> Organization: U of Arizona CS Dept, Tucson Lines: 116 Summary: you can do it..sort of Xref: mnetor comp.unix.wizards:5781 comp.unix.questions:5186 In article <305@caus-dp.UUCP>, marcos@caus-dp.UUCP (Marcos R. Della) writes: > Well, after mounds of responses from lots of people (I think that we have > around 30+ messages on this subject floating around) and lots of mail from > all over the place, so far we have gotten... > > ...nowhere. There are lots of interesting ideas and designs, but so far, > nothing is working on this little problem. Has anyone out there actually > done this and gotten it to work? > > We have tried everything that has come over the net, put some together, > taken others apart, mishmashed them all around and come up with the big > zilch on this problem. I never realized that this would become such a > problem. So... > > Are there any OTHER suggestions that might shed some light on the matter? > > Marcos Della > Daniel Harris You want to set PS1="current_directory_path", and you want to do this with one command, that also changes your working directory (that is, you understandably do not want to type in a sequence of three commands each time you change directory). If I remember you have a Xenix SysV look-a-like. It probably does not allow the exporting of shell function names. And you cannot redefine a builtin like cd. So you try to use a shell script, call it "cwd" or whatever, in place of the builtin, cd. However, a shell script is executed as a forked process, so a change in PS1 done in it will not affect the parent and the change is "lost" when the script terminates. Thus the correct answer, which you have been given, is that you cannot do what you want, except by modifying the sh source or worse yet, the kernel. Nevetheless, you can have theof keeping the change if you are willing to exec a new interactive shell as the last script command. You do not terminate the script until you terminate (^d or whatever) the new shell. By using "exec sh -i" you overlay the script process by the new shell. Thus you still have only the one child process associated with script execution, and this child is the new interactive shell. I threaded through the "PS1" responses, and it seems to me that several contributors implied the above, which seems a pretty obvious work-around that does what you want. Many of the other responses appear correct but relied on functionality probably not found for the Xenix Bourne shell. So if you really want the prompt to contain the working directory, and you are willing to retrain your fingers to use a new acronym to change directory, and you are willing to spawn a new interactive shell at each directory change, try the following (which I tested on a simple SysV Bourne shell (Microport SysV/AT2.2) and a Bourne shell variant under BSD4.3 (under BSD I had to sh the command)): ++++++++++++++++bourne shell script+++++++++++ #!sh #usage: cwd [directory_on_cdpath | full_directory_path] #cd to directory on command line (or $home if no argument); #set prompt PS1=dirctory_path followed by one or more ">"'s, #the number of which indicates the number of invocations of this command, #which equals the depth of the shell set; #a new shell is spawned each time cwd is invoked; #when moving back through the shells (by ^d=eof), #the login shell can be recognized by the default PS1 prompt #note: should error exit on a bad argument, doing nothing -- #if not, start up with: # sh -c "cd $1" # if [ $? -ne 0 ] exit 1 cd $1 PS1=`pwd`">"`echo $PS1|sed -e '/^[^>]*/s///'` export PS1 exec sh -i +++++++++++++++++end shell script+++++++++ A four-line script is pretty simple, but I am not sure that having a prompt with the current directory is worth the shells. If you feel it is, you might want to add frills like doing a string search on a file of the directories within your domain of the file hierarchy, which you can have cron create by executing find at 6am. The following script I do find useful: ++++++++++++++++++another shell script+++++ #!sh cwdir #usage: cwdir [-p] string__matching_directory_path #cd to directory with path matching string on command line #option: -p => interactively pick from set of matches # ("pick" from Kernighan and Pike, or equivalent) #set PS1 according to new directory path DIRLIST="/usr/local/opsys/filelists/dirlist" if [ $1 = "-p" ] then shift STRING=`pick \`egrep "$1[^\/]*\$" $DIRLIST\`` else STRING=`egrep "$1[^\/]*\$" $DIRLIST` fi if [ "x$STRING" != "x" ] then cd $STRING PS1=`pwd`">"`echo $PS1|sed -e '/^[^>]*/s///'` export PS1 exec sh -i fi +++++++++++++++++end shell script+++++++++ Hope this helps, John Rupley uucp: ..{ihnp4 | hao!noao}!arizona!rupley!local internet: rupley!local@megaron.arizona.edu telex: 9103508679(JARJAR) (H) 30 Calle Belleza, Tucson AZ 85716 - (602) 325-4533 (O) Dept. Biochemistry, Univ. Arizona, Tucson AZ 85721 - (602) 621-3929