LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-16-2007, 05:49 AM   #1
hinetvenkat
Member
 
Registered: Nov 2004
Location: Mumbai
Posts: 80

Rep: Reputation: 15
Unary operator error


hi...

when i am executing this below script i got unary operator error which is shown below script msg.

what is the basic issue behind this. Script look like correct only

Please help me

Thanks




Script
~~~~~~~~~~~~~

#!/bin/bash
# fileinfo.sh
FILES="/usr/sbin/accept
/usr/sbin/pwck
/usr/sbin/chroot
/usr/bin/fakefile
/sbin/badblocks
/sbin/ypbind" # List of files you are curious about.
# Threw in a dummy file, /usr/bin/fakefile.
echo
for file in $FILES
do
if [ ! −e "$file" ] # Check if file exists.
then
echo "$file does not exist."; echo
continue # On to next.
fi
ls −l $file | awk '{ print $9 " file size: " $5 }' # Print 2 fields.
whatis `basename $file` # File info.
# Note that the whatis database needs to have been set up for this to work.
# To do this, as root run /usr/bin/makewhatis.
echo
done
exit 0


Error
~~~~~~~~~

#./fileinfo.sh

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
file size:
accept: nothing appropriate

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
file size:
pwck: nothing appropriate

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
file size:
chroot: nothing appropriate

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
ls: cannot access /usr/bin/fakefile: No such file or directory
fakefile: nothing appropriate

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
file size:
badblocks: nothing appropriate

./fileinfo.sh: line 13: [: −e: unary operator expected
ls: cannot access −l: No such file or directory
file size:
ypbind: nothing appropriate
 
Old 10-16-2007, 07:01 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The for statement probably takes the entire string as a single argument. Try this instead of the for statement:
Code:
echo "$FILES" | while read file
Or if that doesn't work:
Code:
for file in `echo "$FILES"`
ta0kira
 
Old 10-16-2007, 07:19 AM   #3
hinetvenkat
Member
 
Registered: Nov 2004
Location: Mumbai
Posts: 80

Original Poster
Rep: Reputation: 15
Hi

I can able to use the for loop perfectly...


FILES="/usr/sbin/accept
/usr/sbin/pwck
/usr/sbin/chroot
/usr/bin/fakefile
/sbin/badblocks
/sbin/ypbind"
for file in $FILES
di
do
ls -l $file
done

prob is not in for loop. i asked the bug in "if" condition line..

Thanks
 
Old 10-16-2007, 10:01 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
On this line:

Code:
if [ ! −e "$file" ] # Check if file exists.
The "−" is not a proper hyphen character. Replace it with a proper "-" (hyphen).
 
Old 10-16-2007, 01:32 PM   #5
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
Using double brackets should fix it [[ your-code ]]
 
Old 10-17-2007, 12:13 AM   #6
hinetvenkat
Member
 
Registered: Nov 2004
Location: Mumbai
Posts: 80

Original Poster
Rep: Reputation: 15
Hi

Problem got resolved. yes... There is "-" only issue..

moreover [["code"]] always will work.

Thank you
 
Old 10-26-2008, 09:38 AM   #7
robertoramsis
LQ Newbie
 
Registered: Oct 2008
Posts: 1

Rep: Reputation: 0
whats the difference between both hyphens ?

Hi ,

Thanks for the thread.
I used [[code]] and it worked.
But I couldn't understand what's a proper and improper hyphen , its only 1 hyphen,the "-" in the minus button ?

Please correct my understanding
 
Old 10-26-2008, 11:22 AM   #8
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,897

Rep: Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578Reputation: 578
Try this:
if [ ! −e $file ]

When using single brackets, the builtin test is more demanding -quotes are used for strings and in this case it may be expecting a non-string value.
 
  


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
Binary operator expected - error mike9287 Linux - Newbie 9 07-17-2006 08:27 AM
Unary Operator expected. Bash script Blackout_08 Programming 2 06-22-2006 02:21 PM
Binary/Unary Functors for Sorting/Finding (C++)? ta0kira Programming 1 05-30-2005 03:12 PM
shell script provides an error that a binary operator is required max_rsr Linux - General 1 03-12-2005 08:26 AM
Unary expressions loke137 Programming 1 08-17-2003 02:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:04 AM.

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