LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 10-30-2006, 10:01 PM   #1
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Rep: Reputation: 0
Bash shell scripting using if else


Hi guys
I'm having a problem trying to use && clause in the if command. If I use the && command in the if clause, it keeps prompting me for a missing ']'. I've attached my if command here, hope anyone can help me.

if [ -z "`ls $LOG`" && -z "`ls $BACKUP`" ] ; then
echo "Empty"
else
echo "Nt Empty"
fi


Thanks alot!
Dave
 
Old 10-30-2006, 10:32 PM   #2
chakkerz
Member
 
Registered: Dec 2002
Location: Brisbane, Australia
Distribution: RedHat (RHEL, FC, CentOS), openSuSE, Mac OS X
Posts: 653

Rep: Reputation: 32
So give it the bracket :P

Quote:
#!/bin/bash

if [ -z "`ls $LOG`"i ] && [ -z "`ls $BACKUP`" ] ; then
echo "Empty"
else
echo "Nt Empty"
fi
 
Old 10-30-2006, 11:46 PM   #3
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chakkerz
So give it the bracket :P
Hey great! Thanks alot!
Please forgive me for making such a error.
 
Old 10-31-2006, 01:49 AM   #4
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by chakkerz
So give it the bracket :P
Another question. I've put the above into a bash file and executed from the command prompt. But it always gives me the error "syntax error: unexpected end of file". But if I were to run the if else statements on the command line, I don't get any error. Any idea?

Regards
Dave
 
Old 10-31-2006, 01:53 AM   #5
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
What values do $LOG and $BACKUP have?
 
Old 10-31-2006, 02:00 AM   #6
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by timmeke
What values do $LOG and $BACKUP have?
Well in the script,

LOG=/opt/oracle/logs
BACKUP=/backup/oracle

That is the exact synthax used. Is there a problem by defining in such a way?

Regards
Dave
 
Old 10-31-2006, 03:24 AM   #7
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
No, the assignments to $LOG and $BACKUP seem just fine to me.

Can you then please post the complete code?
The "unexpected end of file" error usually occurs when
a block (ie "if", "for", etc) isn't terminated properly
(even if it doesn't seem to be at first sight).
 
Old 10-31-2006, 09:07 PM   #8
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by timmeke
No, the assignments to $LOG and $BACKUP seem just fine to me.

Can you then please post the complete code?
The "unexpected end of file" error usually occurs when
a block (ie "if", "for", etc) isn't terminated properly
(even if it doesn't seem to be at first sight).
Here is code -

#!/bin/bash

LOG=/opt/oracle/logs
BACKUP=/backup/oracle


if [ -z "`ls $LOG`" ] && [ -z "`ls $BACKUP`" ] ; then
echo "Empty";
else
echo "Nt Empty";
fi
exit


Thanks for helping.
 
Old 10-31-2006, 10:01 PM   #9
chakkerz
Member
 
Registered: Dec 2002
Location: Brisbane, Australia
Distribution: RedHat (RHEL, FC, CentOS), openSuSE, Mac OS X
Posts: 653

Rep: Reputation: 32
Hmm ...

I'm guessing you have a control character in there somewhere, cause for me there is no such error:

Quote:
[chakkerz@tigerente ~]$ ./test
ls: /opt/oracle/logs: No such file or directory
ls: /backup/oracle: No such file or directory
Empty
[chakkerz@tigerente ~]$ cat test
#!/bin/bash

LOG=/opt/oracle/logs
BACKUP=/backup/oracle


if [ -z "`ls $LOG`" ] && [ -z "`ls $BACKUP`" ] ; then
echo "Empty";
else
echo "Nt Empty";
fi
exit


[chakkerz@tigerente ~]$
 
Old 11-05-2006, 09:14 PM   #10
nautz
LQ Newbie
 
Registered: Jun 2005
Location: Singapore
Distribution: RedHat ES 3
Posts: 16

Original Poster
Rep: Reputation: 0
Hi
Well I've re-written the script into a new file and it didn't gave me tat problem anymore. Thanks for the help!
 
Old 11-07-2006, 09:47 AM   #11
ioerror
Member
 
Registered: Sep 2005
Location: Old Blighty
Distribution: Slackware, NetBSD
Posts: 536

Rep: Reputation: 30
The problem is that you are mixing syntaxes, [ and [[ have a different syntax.

[ requires -a for AND and -o for OR
[[ uses && and || respectively

Thus,

Code:
if [[ -z "`ls $LOG`" && -z "`ls $BACKUP`" ]] ; then
    ...

which is not the same as the previously suggested

Code:
if [ ... ] && [ ... ]; then
    ...
Note though that some shell do not support [[ syntax, in which case the latter should be used, or the '[ ... -a ... ]' syntax.
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash shell scripting Sco Linux - Newbie 1 11-09-2004 11:58 AM
Help with I/O on bash shell scripting Dave6383 Programming 1 06-03-2004 05:24 PM
some bash shell scripting eltongeoff Linux - Newbie 2 10-22-2003 01:10 PM
Bash Shell Scripting Help Tangerine Programming 6 05-06-2003 02:10 PM
BASH Shell scripting help ewarmour Linux - General 8 05-25-2002 07:10 AM


All times are GMT -5. The time now is 01:21 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration