It is currently Tue, 30 Nov 2021 17:59:06 GMT



 
Author Message
 question on 'read' function with Bourne Shell

I am writing a "make directory" script that creates a directory and then
it moves the person to the new directory. The trouble that I am having
with this script is that the read variable works, but when I try to 'cd
to the newly created directory I still stay at the home directory where
the command resides after I finish the script. I would like to use the
read variable and have it complete the script but leave me at the new
directory. I would appreciate help with this command.

I am on a SGI challenge l series with IRIX 5.3

Here is the comand that I am using

#!/bin/sh
TAPE=/tapeinfo
echo " enter tape number "

read number
mkdir $TAPE/$number
cd $TAPE/$number
exit

after running this script I end up in the /usr/people/oper directory
whereas, I would like to end up in the $TAPE/$number directory

Thanks in advance,

Mike



 Mon, 03 Jul 2000 03:00:00 GMT   
 question on 'read' function with Bourne Shell

You can't do it. It is impossible for the 'cd' command to change the
directory of the parent shell. The shell which executes the 'cd' (and
its children) are the only ones who can see the change. General rule on
UNIX systems: Nothing the child does can directly affect the parent.
--
Kurt J. Lanza
k...@infor.com



 Mon, 03 Jul 2000 03:00:00 GMT   
 question on 'read' function with Bourne Shell

 whelan1> I am writing a "make directory" script that creates a
 whelan1> directory and then it moves the person to the new
 whelan1> directory. The trouble that I am having with this script is
 whelan1> that the read variable works, but when I try to 'cd to the
 whelan1> newly created directory I still stay at the home directory
 whelan1> where the command resides after I finish the script. I would
 whelan1> like to use the read variable and have it complete the
 whelan1> script but leave me at the new directory. I would appreciate
 whelan1> help with this command.

Rule 1 of shell scripts: a script can't change the environment, current
directory or other attributes of the shell that invokes it.

What you need is a shell *function*.

(There are less satisfactory ways involving the '.' command, but shell
functions are the Right Way.)

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>
                           or <URL: http://www.whitefang.com/unix/>



 Mon, 03 Jul 2000 03:00:00 GMT   
 question on 'read' function with Bourne Shell

Unless you do

        . directory_script

Which will execute the script as part of the current shell.

--

     J. Wakeley Purple - wa...@iglou.com



 Mon, 03 Jul 2000 03:00:00 GMT   
 
   [ 4 post ] 

Similar Threads

1. Using '!' in bourne shell

2. shell functions don't work the same when opening sub-shells

3. Read command in bourne shell vs. K shell

4. of ~/.profile, bourne shell, and ksh93's ${.sh.version}

5. variable scope in a while loop (bourne shell) --- (l'il long)

6. `at' and Bourne shell parameter substitution

7. And yet still another, that's right another, bourne shell query

8. How to end a user's session gracefully (Solaris 2.6/Bourne shell)

9. 'read' timeouts in shell scripts

10. Light Speed Bourne Shell! (was: Bourne shell tricks)


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software