error "test: [-d: unary operator expected" very confused.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting error "test: [-d: unary operator expected" very confused.
# 1  
Old 06-14-2009
Network error "test: [-d: unary operator expected" very confused.

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?

Cheers,
tired in glasgow
btw Im using OSX 10.5

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

# 2  
Old 06-14-2009
You have to remove the word "test", the if statment is enough..

example:-

if [ -d TEMP ]
then
echo "it is a directory"
else
mkdir TEMP
fi

and it will work..
BR
# 3  
Old 06-14-2009
Check the syntax:
Code:
[ EXPRESSION ]

You need a space after the opening and before the closing bracket.

And of course, as already mentioned, you have to choose either test or [ .

Last edited by radoulov; 06-14-2009 at 11:19 AM..
# 4  
Old 06-14-2009
Equivalence of test, /usr/bin/test, [ ], and /usr/bin/[

these are commands and you can only use one of them:-

examples:-

if test -z "$1"
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if /usr/bin/test -z "$1" # Equivalent to "test" builtin.
# ^^^^^^^^^^^^^ # Specifying full pathname.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi
echo
if [ -z "$1" ] # Functionally identical to above code blocks.
# if [ -z "$1" should work, but...
#+ Bash responds to a missing close-bracket with an error message.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi

if /usr/bin/[ -z "$1" ] # Again, functionally identical to above.
# if /usr/bin/[ -z "$1" # Works, but gives an error message.
# # Note:
# This has been fixed in Bash, version 3.x.
then
echo "No command-line arguments."
else
echo "First command-line argument is $1."
fi

Finally:-

The [[ ]] construct is the more versatile Bash version of [ ]. This is the extended test command, adopted from
ksh88.
No filename expansion or word splitting takes place between [[ and ]], but there is parameter expansion
and command substitution.
file=/etc/passwd
if [[ -e $file ]]
then
echo "Password file exists."
fi
Using the [[ ... ]] test construct, rather than [ ... ] can prevent many logic errors in scripts. For example,
the &&, ||, <, and > operators work within a [[ ]] test, despite giving an error within a [ ] construct.

# 5  
Old 06-14-2009
Thank you so much

ahmad.diab, radoulov

I'm deeply appreciative of your help
I've it working now. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Unary operator expected

In all my Googling, this usually happens when someone is comparing a variable, and that variable is unset. That doesn't appear to be the case for me... #!/bin/bash -x while read line do f=$(echo $line | tr -s ' ' | cut -d' ' -f 3) echo $f if then echo "This... (2 Replies)
Discussion started by: jnojr
2 Replies

2. Shell Programming and Scripting

unary operator expected, if condition test error.

Hi All, I'm assigning a numeric value to variable count=2, well its being assigned by code above the if condition. I want to test for 2 conditions , when $count = 0 or $count <=2 and do something when the condition matches. here is my code, but i run into the infamous : if ] then ... (2 Replies)
Discussion started by: Irishboy24
2 Replies

3. UNIX for Dummies Questions & Answers

[: -gt: unary operator expected

Hi I have problem with my script. I dont now why but i don't change anything and script stop working. this is my code: #!/bin/sh for i in `ps -A | grep pocron.sh | grep -v grep | awk '{print $2}'` do COUNT=$((COUNT+1)) done ostatnie_wykonanie=`cat porader.log`... (1 Reply)
Discussion started by: fotex
1 Replies

4. UNIX for Dummies Questions & Answers

[: =: unary operator expected error

Why am I getting this error.... #!/bin/sh # iOS-Ad-Remover # Marshall Ford @ marshallbford@gmail.com # This project is hosted @ http://ios-ad- # remover.sourceforge.net # Under the GNU GPL open source license clear echo if ; then echo "You need to be root to run this script."; exit 0; #... (24 Replies)
Discussion started by: mbf123
24 Replies

5. UNIX for Dummies Questions & Answers

Error : -ne: unary operator expected

find . -name "*.*"|xargs grep WT:DBF_WL>> $F Wfexist=`cat $F|grep $i` echo $Wfexist if ; then echo $Wfexist echo "Workflow Exist" else touch $O chmod 777 $O echo $Wfexist echo $WfExist >> $O fi I am getting the error that -ne: unary operator expected in the line with red... (2 Replies)
Discussion started by: ritu.s
2 Replies

6. Shell Programming and Scripting

if returns "unknown test operator"

Greetings, using ksh on Solaris, I am trying to identify the current version of a package installed on multiple servers using if statement in a precursor to upgrading. I have searched the forums and have found many hits, reviewed 3 pages and have tried the different variations noted there. Also... (3 Replies)
Discussion started by: 22blaze
3 Replies

7. Shell Programming and Scripting

unary operator expected

hi i am trying to compare a value with value 50. but i am getting " I am using if then echo "------------" fi please help thanks in advance Satya (2 Replies)
Discussion started by: Satyak
2 Replies

8. Shell Programming and Scripting

awk returning "[: ==: unary operator expected"

Hi All, I am new to shell scripting and right now I am just limited to using the pre-written scripts. I am in to Infrastructure management where we use different scripts to get the information passed on to the monitoring tools. I am trying to use this script to get the information about the... (2 Replies)
Discussion started by: theamrit
2 Replies

9. UNIX for Dummies Questions & Answers

unary operator expected error

Hi I am doing a script like if then echo "table name dosent exist" exit fi the problem is if $table_name is null then i am getting the error Please help me Thanks in advance (2 Replies)
Discussion started by: ssuresh1999
2 Replies

10. UNIX for Dummies Questions & Answers

"test: argument expected" error

Hi, No need to say I'm new to unix shell scripting. I have a very simple script that goes this way: for datos in `ls -rt $UNXLOG/26-Jan*` do export arch=`echo $datos |cut -d, -f1` if then export linea1=`grep Debut ${arch}` export horatot=`echo $linea1 |cut -d' ' -f5` ... (7 Replies)
Discussion started by: mvalonso
7 Replies
Login or Register to Ask a Question