LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2008, 12:39 AM   #1
sharathkv
Member
 
Registered: Jul 2003
Distribution: HP-UX
Posts: 35

Rep: Reputation: 15
Change directory with parameter in ksh script


Hi,

How do I change to a path that is a parameter in ksh script?

The below script is not working:

PHP Code:
#!/usr/bin/ksh
. /opt/osmf/config/env/osmf-http.env

echo $ORACLE_HOME
cd $ORACLE_HOME
/opmn/logs 
If I manually type "cd $ORACLE_HOME/opmn/logs"(without quotes) in command line, it's working fine.

Thanks
 
Old 08-13-2008, 12:42 AM   #2
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
A sub-shell cannot change its parent shell's working directory. No process can change a parent process's directory.

The directory is changed in the shell script process, but once the process exits, the parent process is back in control with its original working directory.
 
Old 08-13-2008, 02:14 AM   #3
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
As Mr. C. said, you cannot write a script to change the directory because it runs in a child process, which cannot modify your interactive shell's environment (a child cannot modify the environment of the parent).

However, you can set an alias in your .profile, which executes within your interactive shell - here you can change the working directory.

e.g. in your $HOME/.profile add these lines:
Code:
. /opt/osmf/config/env/osmf-http.env 
alias oralog='cd "$ORACLE_HOME/opmn/logs"'
Of course, you don't have to call it "oralog" - use whatever you like.

One word of warning - editing your .profile is potentially risky. This file is read whenever a login shell is created. If you write something wrong in this file, it can make it so you will not be able to log in again (e.g. putting the "exit" command in the .profile - although there are many subtler problems which can occur).

To guard against this, I propose the following method to edit your .profile:
  1. In one terminal, edit the .profile. save your changes but leave the editor running.
  2. With the .profile still open in the first terminal, open another terminal and source the modified .profile with this command:
    Code:
    . ~/.profile
  3. After sourcing the .profile, check that your changes seem OK - in our case that the alias works as expected
  4. Only when you know the changes are successful, close the editor.
This method is probably a bit over-cautious, but it is a good idea if you are not a confident scripting programmer.
 
Old 08-13-2008, 07:42 AM   #4
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 63
Quote:
Originally Posted by matthewg42 View Post

To guard against this, I propose the following method to edit your .profile:
  1. In one terminal, edit the .profile. save your changes but leave the editor running.
  2. With the .profile still open in the first terminal, open another terminal and source the modified .profile with this command:
    Code:
    . ~/.profile
  3. After sourcing the .profile, check that your changes seem OK - in our case that the alias works as expected
  4. Only when you know the changes are successful, close the editor.
This method is probably a bit over-cautious, but it is a good idea if you are not a confident scripting programmer.
I think this is very good advice; there are countless times I've read about user's locking themselves out of their accounts due to incorrect changes to either the shell startup files or sshd configuration.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
It is possible to change ksh script on a SUN into bash richikiki Programming 2 06-20-2006 06:08 AM
ksh directory comparison script SeT Programming 4 08-30-2004 11:57 AM
Change parameter of /etc/sysconfig/network with a script philipina Linux - Hardware 1 03-11-2004 12:59 PM
Change parameter of /etc/sysconfig/network with a script philipina Linux - General 3 03-11-2004 05:05 AM
How to change parameter values of a function in shell script? Bassam Programming 0 01-25-2004 09:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration