Conditional OR in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Conditional OR in shell
# 1  
Old 04-25-2016
Conditional OR in shell

Hi

I have been trying to write a simple code:

Code:
if [[ `(ls -A $apache_log_dir | egrep *.log)` ]] || [[ `(ls -A $jboss_log_dir | egrep *.log)` ]]
then
echo "Log Directory is not empty, we will continue with archive operation"
else
echo "Log Directory is empty, Exiting......."
exit 1
fi

It never checks for the second OR condition i.e.
Code:
 [[ `(ls -A $jboss_log_dir | egrep *.log)` ]]

Could you please help?

Thanks
# 2  
Old 04-25-2016
If the result is true for the first test, the second test will not be executed due to "short circuit evaluation".

The egrep seems redundant, what do you expect it to do what a ls *.log would not deliver? And it may not yield what you're after: it would show e.g. alogin as well.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-25-2016
Hi Rudi,

Yes you are correct. Any way to check both the conditions?

Also *.log are the on;ly files present in the folders, so I am fail safe in this egrep.

Thanks
# 4  
Old 04-25-2016
Try
Code:
if [[ $(ls -A $apache_log_dir/*.log $jboss_log_dir/*.log) ]]

It will check in both directories for .log files and evaluate to true if a file exists in either.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 04-25-2016
thats brilliant Rudy, Many thanks...
# 6  
Old 04-25-2016
Quote:
Originally Posted by RudiC
Try
Code:
if [[ $(ls -A $apache_log_dir/*.log $jboss_log_dir/*.log) ]]

It will check in both directories for .log files and evaluate to true if a file exists in either.
Note that if either of those directories are empty, or at least do not contain any files with names ending in .log, the command substitution will print a diagnostic message. You can avoid having those appear on the screen when you run this script by throwing away any diagnostics produced by ls:
Code:
if [[ $(ls -A $apache_log_dir/*.log $jboss_log_dir/*.log 2>/dev/null) ]]

This User Gave Thanks to Don Cragun For This Post:
# 7  
Old 04-25-2016
This could be simplified further still:
Code:
if ls -A "$apache_log_dir"/*.log "$jboss_log_dir"/*.log 1>/dev/null 2>&1; then


Last edited by Scrutinizer; 04-25-2016 at 07:38 AM..
This User Gave Thanks to Scrutinizer For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional Vlookup

Hi everyone, I need to replace values of column 2 array1 with values of column 2 array2 based on a lookup of column 4 value, but only return a value IF the values in column 1 of BOTH array1 and array2 match, otherwise keep original value in column 2 of array1. Both files are tab delimited... (2 Replies)
Discussion started by: Geneanalyst
2 Replies

2. UNIX for Beginners Questions & Answers

Conditional Split

Greetings, I need help in splitting the files in an efficient way while accommodating the below requirements . I am on AIX. Split condition Split the file based on the record type and the position of the data pattern that appears on the on the record type. Both record type and and the... (9 Replies)
Discussion started by: techedipro
9 Replies

3. Shell Programming and Scripting

How to append to array within conditional block in ksh/korn shell?

Hi, I have one array created and some values are there in ksh. I want to append some other values to it based on some condition in if statement. #!/bin/ksh echo "---------------------------------------------------" set -A ipaddr_arr $(egrep -v '^#|^::|^$' /etc/hosts |awk '{print $1}'... (2 Replies)
Discussion started by: sanzee007
2 Replies

4. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

5. Shell Programming and Scripting

Conditional Shell Statement

I want to add a conditional statement to a user's .profile file. I have a certain number of users that log in and use the rksh (Restricted Korn Shell). When they log in, it starts a certain program and when they exit this program, the system logs them out. When they are in this program, they can... (2 Replies)
Discussion started by: rjulich
2 Replies

6. Shell Programming and Scripting

Help on shell script conditional execution when CPU Idle > 60%

I need a shell script that will monitor a few conditions and not execute until the these conditions are met. The problem I'm having is that I can not perform a database snapshot (backup) of a sybaseIQ database unless the CPU Status Idle % is above 60% or the snapshot (backup) fails. If... (2 Replies)
Discussion started by: pancona99
2 Replies

7. Shell Programming and Scripting

conditional extracting

Hi, I need to extract lines based on some conditions as explained below: File format details: 1. each set starts with AAA only 2. number of columns is fixed 3. number of rows per set may vary (as one set is upto DDD - 4 rows) Now, i need to extract only the lines starting with AAA and... (2 Replies)
Discussion started by: prvnrk
2 Replies

8. Shell Programming and Scripting

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (12 Replies)
Discussion started by: abhinavsinha
12 Replies

9. UNIX for Dummies Questions & Answers

If conditional

Hi, I am new to unix and shell scripting.In my script,there is a line using the "if" conditional - if && ; then do something Here "x" is a variable holding string value.If it is not equal to a comma or a string,only then I want to enter the "if" loop. But I am getting error while... (1 Reply)
Discussion started by: abhinavsinha
1 Replies

10. UNIX for Dummies Questions & Answers

conditional

conditional is not wworking can any one figure out what goes wrong xx1=`$ORACLE_HOME/bin/sqlplus -s apps/ostgapps1 2>/dev/null << EOF WHENEVER SQLERROR EXIT 1 set head off feedback off ; WHENEVER SQLERROR EXIT SQL.SQLCODE; select count(*) from CMS_INVOICE_ALL... (2 Replies)
Discussion started by: u263066
2 Replies
Login or Register to Ask a Question