LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-31-2012, 08:27 PM   #1
greenpool
Member
 
Registered: Sep 2010
Posts: 41

Rep: Reputation: 0
Question unary operator expected error


Hi guys,

got this annoying error which i'm unable to figure out why its happening.

Code:
#!/bin/bash

EMPTY=~/Documents/empty

if [ cd "$EMPTY" ]
then
 echo  "dir exists"
else
 echo "dir doesn't exist" 1>&2 
  exit 1
fi

i keep getting the following error:

Code:
./errorChecking.sh: line 5: [: cd: unary operator expected

i have also tried:

if [[ cd "$EMPTY" ]]


thanks
 
Old 01-31-2012, 08:36 PM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,247

Rep: Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328Reputation: 2328
Try
Code:
#!/bin/bash

EMPTY=~/Documents/empty

if cd "$EMPTY"
then
 echo  "dir exists"
else
 echo "dir doesn't exist" 1>&2 
  exit 1
fi
The other way to do it (for testing cmds generally)
Code:
cmd_does_something
if [[ $? -eq 0 ]]
then
    echo "cmd success"
else
    echo "cmd fail"
fi
See http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

See the '-d' operator specifically for your prog

HTH

Last edited by chrism01; 01-31-2012 at 08:37 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] -gt: unary operator expected hd_pulse Programming 10 05-28-2011 08:57 AM
error: unary operator expected ?? Lynda_M Programming 3 11-29-2008 09:03 PM
unary operator expected error! Lynda_M Programming 3 11-29-2008 01:04 PM


All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration