Find the answer to your Linux question:
Results 1 to 5 of 5
I know this does'nt work but how in my script would I get the script to confirm it's directory is in. if [ $pwd -eq $LOG_DIR ]; then echo "work" ...
  1. #1
    Just Joined!
    Join Date
    Jan 2011
    Posts
    19

    how to get my script to confirm the directory it is in

    I know this does'nt work but how in my script would I get the script to confirm it's directory is in.

    if [ $pwd -eq $LOG_DIR ];
    then
    echo "work"
    else
    echo "did'nt work"

    Thanks
    Tom

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    variables in bash are case sensitive you need $PWD

  3. #3
    Linux Enthusiast Kloschüssel's Avatar
    Join Date
    Oct 2005
    Location
    Italy
    Posts
    717
    Despite the fact that this comment is rather off topic:

    A script should not care about which working directory it is being executed at unless it is designed to do so. I.e. if a script removes files from a directory and someone else executes it in error that one could become rude, especially if the other one was me and $PWD was ~.

  4. #4
    Just Joined! cfajohnson's Avatar
    Join Date
    May 2007
    Location
    Toronto, Canada
    Posts
    52
    Quote Originally Posted by Kloschüssel View Post
    Despite the fact that this comment is rather off topic:

    A script should not care about which working directory it is being executed at unless it is designed to do so. I.e. if a script removes files from a directory and someone else executes it in error that one could become rude, especially if the other one was me and $PWD was ~.

    On the contrary; it may be very important for a script to know what directory it is being executed in, or it might do unwanted things in the wrong directory.

    On the other hand, it should never be necessary to know which directory the script itself is in.

  5. #5
    Trusted Penguin Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,230
    If the script is supposed to act on a certain directory, I think that you have two choices that are better than what you are doing here:

    1) Don't use the current directory, use the target directory. For example, if printing every file in the current directory, say "cat $TARGET_DIR/*", not "cat *". This way, the current directory doesn't matter.

    2) Make the first thing you do to cd to the target directory:
    Code:
    cd $TARGET_DIR
    cat *
    DISTRO=Arch
    Registered Linux User #388732

Posting Permissions

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