Find the answer to your Linux question:
Results 1 to 9 of 9
Guyz, This cud be simpler but i aint gettin the correct lane. I need to change the current directory( directory from which am running the shell program) to a different ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2004
    Location
    Chennai, India
    Posts
    4

    need to do a cd command using shell script


    Guyz,

    This cud be simpler but i aint gettin the correct lane. I need to change the current directory( directory from which am running the shell program) to a different directory using a shell program. i tried this system() command it aint workin. ie. if iam workin in /home/karthik and i execute a script, pon execution, i want the dir to be changed to /dev.

  2. #2
    Just Joined!
    Join Date
    Oct 2004
    Posts
    6

    Re: need to do a cd command using shell script

    Quote Originally Posted by shadowman21i
    Guyz,

    This cud be simpler but i aint gettin the correct lane. I need to change the current directory( directory from which am running the shell program) to a different directory using a shell program. i tried this system() command it aint workin. ie. if iam workin in /home/karthik and i execute a script, pon execution, i want the dir to be changed to /dev.
    there's two way to do this:

    1. man bash
    2. include a "cd /dev" in your script

    hth.

  3. #3
    Linux Newbie
    Join Date
    Oct 2004
    Posts
    158
    You do understand that system() or a bash shell script that change the current working directory - when they exit - you will be where you started out. The cwd of the parent process is unchanged.

  4. #4
    Just Joined!
    Join Date
    Nov 2004
    Posts
    5
    I believed that the system() command is one of the functions in C that are used for UNIX system calls. So, if you're system() in a shell script, in won't probably work.

  5. #5
    Just Joined!
    Join Date
    Jun 2004
    Posts
    16
    Quote Originally Posted by geekster
    I believed that the system() command is one of the functions in C that are used for UNIX system calls. So, if you're system() in a shell script, in won't probably work.
    Exactly.

    This call is working only in C, so if you want to use it, you'll need to make a whole program for this small command that could be easily executed in a shell script. In that file , you only have to add a sequence of commands that you would normaly enter in your shell manualy.

    Try this instead. Open a new file and add these lines:

    #!/bin/sh
    cd /your_directory/

    To execute that command, type:
    ./your_shell_script


    Everything should be working ! I hope that I answered your question.

  6. #6
    Linux Guru lakerdonald's Avatar
    Join Date
    Jun 2004
    Location
    St. Petersburg, FL
    Posts
    5,035
    Code:
    man bash
    tells all

  7. #7
    Just Joined!
    Join Date
    Jun 2006
    Posts
    1
    Quote Originally Posted by shadowman21i
    Guyz,

    This cud be simpler but i aint gettin the correct lane. I need to change the current directory( directory from which am running the shell program) to a different directory using a shell program. i tried this system() command it aint workin. ie. if iam workin in /home/karthik and i execute a script, pon execution, i want the dir to be changed to /dev.
    Hi,

    I am facing the same problem. If your problem is resolved, could you please let me know how to do this.

    Cheers,
    Priti.

  8. #8
    Linux User
    Join Date
    Aug 2005
    Location
    Italy
    Posts
    401
    Quote Originally Posted by pritijaiswaljsr
    Hi,

    I am facing the same problem. If your problem is resolved, could you please let me know how to do this.

    Cheers,
    Priti.
    But the question is solved in this thread! (In the second post)
    When using Windows, have you ever told "Ehi... do your business?"
    Linux user #396597 (http://counter.li.org)

  9. #9
    Just Joined!
    Join Date
    Jul 2011
    Posts
    1
    The solution is to run the script with the following syntax ". ./script.sh"

    "./script.sh" does not change environment variables, the extra period and space at the beginning causes the directory to remain changed after the script is run.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •