[ELECTRON] Shell Scripting help? "test: [-d: unary operator expected"
Simon Yuill
simon at lipparosa.org
Sun Jun 14 15:04:30 UTC 2009
Hi Ben,
This bit of Python will do the dame job for you, if you can use Python
for your scripts:
def validateFilePath(filepath, create=False, isDirectory=False):
"""
Converts C{filepath} to absolute form, expanding the
user if necessary,and tests if it exists. If the
directory, or any sub-directory in the path
does not exist, can optionally create it (off by default).
Returns validated path. If C{create} is set to false,
and the path does not exist, returns C{None}.
"""
absfilepath = os.path.abspath(os.path.expanduser(filepath))
if filepath[-1] == '/':absfilepath = absfilepath + '/'
if not isDirectory:dirpath = os.path.dirname(absfilepath)
else:dirpath = absfilepath
if os.path.exists(dirpath):return absfilepath
if create:
os.makedirs(dirpath)
return absfilepath
else:return None
best wishes
Si
Ben Rush wrote:
> Im trying to check if a series of directory exists and if not create
> them, and am having issues. All the instances of test return with the
> error "test: [-d: unary operator expected" . Searching around I see that
> if the variable is in double quotes its solves the problem - but for
> some reason, not for me. The script still makes the directories if the
> don't exists, however if they DO - it still tries to create them.
>
> What noob things am I doing wrong here?
>
> This is just one small but of my puzzle - if there is anybody
> knowledgeable in shell scripting that could give me a few hours to in
> the next day or so - I'd be happy to pay for your time.
>
> Cheers,
> Ben Rush
>
> [code]
> #!/bin/bash
> location_Parent=~/Documents/sight_of_sound
> location_IMG=~/Documents/Sight_of_sound/IMG
> location_AUD=~/Documents/Sight_of_sound/AUD
>
> if test [-d "$location_Parent"]
> then
> echo "Parent Directory OK"
> else
> echo "Error: Parent directory does not exist. Creating directory"
> mkdir $location_Parent
> fi
>
> if test [-d "$location_IMG"]
> then
> echo "IMG Directory OK"
> else
> echo "Error: IMG directory does not exist. Creating directory"
> mkdir $location_IMG
> fi
>
> if test [-d "$location_AUD"]
> then
> echo "AUD Directory OK"
> else
> echo "Error: AUD directory does not exist. Creating directory"
> mkdir $location_AUD
> fi
> [/code]
>
>
>
>
> ------------------------------------------------------------------------
>
> _______________________________________________
> members mailing list
> members at electronclub.org
> http://lists.electronclub.org/cgi-bin/mailman/listinfo/members
>
> Instructions for changing your mailing list settings:
> http://lists.electronclub.org/emailhowto.html
More information about the members
mailing list