Unix Shell - Re: Running a script: How to reference the directory the script is located in.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > October 2006 > Re: Running a script: How to reference the directory the script is located in.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Re: Running a script: How to reference the directory the script is located in.
Daniel Rock

2006-10-24, 7:17 pm

jhagge@gmail.com wrote:
> I would guess most users will be saving this to there desktop, but if
> they did happen to put it in a directory with a space in the name, the
> variable is set wrong - basically stopping where the space is.
>
> If the path is ~/Desktop/Saved Files/DOGFOOD
>
> the variable gets set to:
>
> ~/Desktop/Saved
>
> Any ideas how to work around this?


Best practise: Don't use whitespace in filenames.
If you cannot avoid: Quote every variable. But multi-level quoting gets
very ugly:

dir=$(dirname "$0") # remember to quote("") $0
dir=$(cd "${dir}"; pwd) # again: quote ${dir}

# or in a single line:
dir=$(cd "$(dirname "$0")"; pwd)
# nesting quotes isn't always as easy as in this example

echo "${dir}" # quota again

filename="${dir}/some_file" # dito.

if [ -f "${filename}" ]; then
: do something
fi


Summary: Remember to quote any variable which could contain whitespace.

--
Daniel
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com