Issue with IF condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with IF condition
# 8  
Old 06-25-2014
Code:
if [[ -f /usr3/tstsrc/test/anitha/*.done || -f /usr3/tstsrc/test/anitha/*.zip ]]
then
echo "Found"
fi

# 9  
Old 06-25-2014
Quote:
Originally Posted by anijan
Getting syntax error when the condition after -o returns more than one file

Code:
if [ -f /usr3/tstsrc/test/anitha/*.done -o -f /usr3/tstsrc/test/anitha/*.zip ]
then
echo "Found"
fi

output:
Code:
=>ls -l
total 5
-rw-rw-r--   1 wes      wes            0 Jun 25 04:51 a.done
-rw-rw-r--   1 wes      wes            0 Jun 25 04:51 a.zip
-rw-rw-r--   1 wes      wes            0 Jun 25 04:51 b.zip
-rwxrwxrwx   1 wes      wes          101 Jun 24 06:37 test.sh
<SunOS>/usr3/tstsrc/test/anitha
=>./test.sh
./test.sh: test: syntax error
<SunOS>/usr3/tstsrc/test/anitha
=>


Input Output
2 done files , 2 Zip files Pass
2 done files , 1 Zip files Pass
1 Done file, 2 Zip files Syntax error
1 Done file, 1 Zip files Pass
0 Done file, 1 Zip file Pass
0 Done file, 2 Zip file Syntax error
Anytime a filename pattern expansion yields two or more pathnames, tests like:
Code:
[ -f pattern ]

will yield a syntax error or (due to the names and number of files involved) unexpected, unreliable test results. You will only get valid results if pattern expands to a single word. NEVER use an * in a pattern in a test like this.

Instead try something like:
Code:
for i in /usr3/tstsrc/test/anitha/*.done /usr3/tstsrc/test/anitha/*.zip
do      if [ -f "$i" ]
        then    echo "Found"
                break
        fi
done

# 10  
Old 06-25-2014
How about
Code:
 [ 0 -lt $(wc -w < <(ls *.done *.zip)) ] && echo found || echo NOK

(requires recent bash / ksh)

EDIT: or
Code:
[ $(ls *.done *.zip 2>/dev/null) ] && echo found || echo NOK


Last edited by RudiC; 06-25-2014 at 03:32 PM..
# 11  
Old 06-25-2014
Quote:
Originally Posted by RudiC
How about
Code:
 [ 0 -lt $(wc -w < <(ls *.done *.zip)) ] && echo found || echo NOK

(requires recent bash / ksh)

EDIT: or
Code:
[ $(ls *.done *.zip 2>/dev/null) ] && echo found || echo NOK

The last one won't work. The ls utility will return a non-zero exit status if any file operand names a file that does not exist. And, you don't need command substitution here. Perhaps you meant something like:
Code:
(ls *.done 2>&1 > /dev/null || ls *.zip 2>&1 >/dev/null) && echo Found || echo NONE

The first one also needs to redirect stderr to /dev/null from the ls to avoid unwanted diagnostic output.
# 12  
Old 06-25-2014
Hmm - works for me with bash. It's testing for the non-zero string the ls command substitution gives back and reacts correctly. If there's two non-existing and one existing pattern, it echoes "found". If none of the patterns exists, it echoes "NOK" - I can't say if due to the zero string or due to the exit code.
# 13  
Old 06-25-2014
Hi RudiC,
If I don't have any files matching *.done and 2 files matching *.zip (a.zip and b.zip), I get:
Code:
-ksh: [: argument expected
NOK

with the ksh, and:
Code:
bash: [: a.zip: unary operator expected
NOK

with bash.
I believe the desired result was:
Code:
Found

This User Gave Thanks to Don Cragun For This Post:
# 14  
Old 06-25-2014
You are right, I was not testing thoroughly. Try like
Code:
 [ "$(ls *.done *.zip 2>/dev/null)" ] && echo found || echo NONE

This User Gave Thanks to RudiC 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

Issue with condition "if then elif else fi"

Hi everybody, I must get trought a shell script 3 arguments. 1st argument = $1 (can take values REP1..4) 2nd argument = $2 (can take values A..Z) 3rd arguement = $3 (also can take values A...Z) I've written this code : #!/bin/bash if then liste=/data/folder1 echo... (6 Replies)
Discussion started by: shellX
6 Replies

2. Shell Programming and Scripting

Solaris 11 ksh93 if condition issue

Solaris 11 ksh93 if condition issue Has anyone run into issues with Solaris 11 with ksh93 if condition where it intermittently return wrong return code? We did not see this issue in Solaris 10 with ksh88 Any thoughts? Thanks! Solaris version: SunOS t52-ccc-28 5.11 11.2... (4 Replies)
Discussion started by: nugent
4 Replies

3. Shell Programming and Scripting

If Condition Issue in UNIX

Hi I am trying to do a "IF" Condition in UNIX where we compare EACH file size in a directory with a SIZE (Parameter passed) If Each File size EXCEEDS parameter passed SIZE then we manipulate the file. Somehow the IF condition do not work ?? (is this Variable decalration issue ??) ... (9 Replies)
Discussion started by: Pete.kriya
9 Replies

4. Shell Programming and Scripting

Condition checking issue while if

hi, i am using a simple condition end_ct=`sqlplus -s $ORACLE_USER/$ORACLE_PASS@$ORACLE_SID << EOF select description from bravo_statistics where trunc(time_stamp)=trunc(sysdate)-1 and description='END CAT'; EOF` echo $end_ct; echo... (30 Replies)
Discussion started by: lovelysethii
30 Replies

5. Shell Programming and Scripting

if condition issue

Hi, I have prepared the below shell script for one of the requirement which accesses Informatica application and performs certain functions. But for some reason, if condition fails and scripts stops abruptly with the below error. Please let me know the where the problem is #!/bin/ksh ... (2 Replies)
Discussion started by: svajhala
2 Replies

6. Shell Programming and Scripting

Unix and db2 where condition issue(new line)

Hi I am extracting a column value(DESCRIPTION) from one table and passing it to another db2 statement in a shell code to fetch some value(ID) but the value when passed in where condition is taking as newline+value. Please find the out put when executed: + echo description is ::::... (1 Reply)
Discussion started by: msp2244
1 Replies

7. Shell Programming and Scripting

redirect stdout echo command in condition A run in condition B

hi, I have some problems in my simple script about the redirect echo stdout command inside a condition. Why is the echo command inside the elif still execute in the else command Here are my simple script After check on the two diff output the echo stdout redirect is present in two diff... (3 Replies)
Discussion started by: jao_madn
3 Replies

8. Shell Programming and Scripting

if condition issue

Hi, Unix Gurus, I got some issue with my script. #!/usr/bin/ksh while read newstatus do if ; then echo "It is finished " else echo "Not Finished" fi done <checkresult.txt basically, I want to read one file (the file only contains one word, but it dynamically changed),... (2 Replies)
Discussion started by: ken002
2 Replies

9. UNIX for Dummies Questions & Answers

If condition issue

Hello, I am using the following code #!/bin/sh chris='rerun' chris2=$1 echo "chris=$chris" echo "chris2=$chris2" if then echo "RERUN" else echo "NOT RERUN" fi (6 Replies)
Discussion started by: chriss_58
6 Replies

10. Shell Programming and Scripting

An issue with condition statement in shell script

Hello forum members. please go through the below mentioned issue and let me know the right solution. I have to write a script which runs another script .the executable script take input parmeters.so iam writing the the script below . Sample Code:Begins #! /bin/ksh echo " enter... (2 Replies)
Discussion started by: rajkumar_g
2 Replies
Login or Register to Ask a Question