LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (http://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash shell scripting using if else (http://www.linuxquestions.org/questions/linux-general-1/bash-shell-scripting-using-if-else-497090/)

nautz 10-30-2006 10:01 PM

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

chakkerz 10-30-2006 10:32 PM

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

nautz 10-30-2006 11:46 PM

Quote:

Originally Posted by chakkerz
So give it the bracket :P

Hey great! Thanks alot! :D
Please forgive me for making such a :newbie: error.

nautz 10-31-2006 01:49 AM

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

timmeke 10-31-2006 01:53 AM

What values do $LOG and $BACKUP have?

nautz 10-31-2006 02:00 AM

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

timmeke 10-31-2006 03:24 AM

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).

nautz 10-31-2006 09:07 PM

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.

chakkerz 10-31-2006 10:01 PM

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 ~]$

nautz 11-05-2006 09:14 PM

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!

ioerror 11-07-2006 09:47 AM

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.


All times are GMT -5. The time now is 12:41 PM.