Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

UNIX Scripting FAQ

ksh Inter-process comm's

Using Signal and TWO-WAY pipe to cd login shell from another script by mi294r3
Posted: 6 Aug 03 (Edited 12 Oct 07)

  How can I change the current directory from a script.

  This question seems to pop up in the forum occasionally.

  One way is to source the script into the current shell.
          For example scriptname changedir
        Then from the prompt
        -> . changedir
   The risk is anything done by the script, is done to the login shells environment.
A way to change the current directory from another shell is the use of signals and pipes.
    This example was written and tested on AIX 5.3 using Korn Shell.  There may be some issues that might require some tweaking, however the concept is sound and can be applied to variety of problems.


  First create a pipe in your home directory
     At the prompt
       -> mknod .Can_and_String p
    Then make the following entry in your .profile

func51 ()                                                 
      while read Msg                                      
        if echo ${Msg}|grep -q "^REQ_TO_LOGIN_SHELL"; then
            eval $(echo ${Msg}|cut -d: -f2)               
       done <$HOME/.Can_and_String                        
trap "func51"  51                                         
export LOGIN_SHELL=$$

    The function definition func51 is loaded into memory when .profile is sourced.  The function is executed when a SIGNAL 51 is sent to the login shell.  
    This is where the trap command comes into play  
                trap "func51"  51
Simply put, it says trap ANY signal 51 sent to the current shell, and when you do run func51.
    I used signal 51 becasue it is undefined at my location, I did not explore using any other signals.  What you decide to use will be based on your environment.
   The function reads from the pipe .Can_and_String, looks for any string staring with REQ_TO_LOGIN_SHELL.  When it finds one, it assumes the string is a 2 field string seperated by a colon.  The second field is a command that another process is requesting the login shell to execute.
    Next create a small shell script called R2loginShell, it should have the following two lines;
kill -51 ${LOGIN_SHELL}                             
echo "REQ_TO_LOGIN_SHELL:$@" >>$HOME/.Can_and_String

  This script sends signal 51, (via the kill command) to the process ID of the login shell.  It then writes a request, for the login shell, to the pipe .Can_and_String.  The actual command is sent from another script.
     The last thing to do is to write a script that utilizes the process you just put in place.  The example I used was the following script called cdmenu
  select CHOICE in /tmp /etc /usr /var EXIT;
        if [[ ${CHOICE} = 'EXIT' ]] then   
      $HOME/R2loginShell cd ${CHOICE} &    

    This script builds a menu with 5 selections.
1) /tmp
2) /etc
3) /usr
4) /var

    Except for EXIT, when a selection is made, R2loginShell is started in the background.  The call has 2 positional parameters 1)cd & 2)the value of the option selected.
           For example if you selected 2;
1) /tmp
2) /etc
3) /usr
4) /var
#? 2    

  the call to R2loginShell cdmenu is resolved to this
    R2loginShell cd /etc

   the positional parameters are then read into the pipe .Can_and_String via $@
echo "REQ_TO_LOGIN_SHELL:$@" >>$HOME/.Can_and_String


Give it a try?

Back to UNIX Scripting FAQ Index
Back to UNIX Scripting Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close