Results 1 to 8 of 8
  1. #1
    Join Date
    Oct 2004
    Posts
    7

    Arrow Unanswered: Use of cd in script

    Hi Friends,

    I am tring to use cd command in the Unix script.
    When this command in executing
    cd $Archive_Name
    It is not changing the directory instead it is telling
    "cd: bad substitution"

    Then i tried using
    "cd $Archive_Name 1>&2"
    now it is telling
    "bad file unit number"

    Can any one help me how to use cd command in the script.

    - kmrs
    Last edited by kmrs; 12-06-04 at 01:34.

  2. #2
    Join Date
    Dec 2004
    Posts
    3
    Can you tell us what is in the $Archive_Name variable?

    Also, set -x is very usefull in troubleshooting scripts.


    Billy Menasco

  3. #3
    Join Date
    Oct 2004
    Posts
    7

    Question

    Hi Billy Menasco,

    Thank you for your reply.

    The variable $Archive_Name has the directory to which it has to be changed.

    What is the use of set -x?

    I am not aware of that,can you explain how to use it.

    - kmrs

  4. #4
    Join Date
    Dec 2004
    Posts
    3
    Quote Originally Posted by kmrs
    The variable $Archive_Name has the directory to which it has to be changed.

    What is the use of set -x?
    When added to the top of your script "set -x" enables tracing to allow you to monitor what is being executed inside your script.

    For more information try this Google Search for set and tracing.


    Billy Menasco

  5. #5
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    Wink

    menasco is correct, "cd: bad substitution" means your $Archive_Name variable is not set
    The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

  6. #6
    Join Date
    Oct 2004
    Posts
    7
    Hi,

    I also tried by giving the itself like

    cd /kmrs/mydir/Test

    Now also I got the error and the command was not executed.

    Is there any other way of using the cd command in the script.

    Please let me now.

    - kmrs

  7. #7
    Join Date
    Jun 2004
    Posts
    20
    Ensure that the directory "/kmrs/mydir/Test" exists.


    ####### start of script file mycd.sh ####
    Archive_Name=/kmrs/mydir/Test
    cd $Archive_Name
    ####### End of script file mycd.sh ####

    Execute as follows
    . ./mycd.sh


    HTH

  8. #8
    Join Date
    Dec 2004
    Posts
    3
    Like srsjc said, sounds like the directory does not exist. You can have the script test to make sure the directory exists. See below.

    Code:
    Archive_Name=/kmrs/mydir/Test
     
    if [ -d $Archive_Name ]
    then
    $Archive_Name
    else
    echo "$Archive_name does not exist"
    fi

Posting Permissions

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