if statement with two conditions -e, && | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

if statement with two conditions -e, &&

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 06-14-2005
yongho yongho is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 4 August 2005, 5:20 PM EDT
Location: New York City
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
if statement with two conditions -e, &&

Wow I'm so zoned out I don't even know if I posted this question up already (I couldn't find it in my book marks or in "yesterday's" post).

My question is, I'm writing a korn script that does something like the following, but I don't yet completely understand the syntax. I need to check that the first field equals a string and that the second string equals a space. This isn't working -- is it right or wrong?

PHP Code:
FIELD1=`cut -c2-5 $FILENAME`
FIELD5=`cut -c68 $FILENAME`
 if [ 
$FIELD1 -eq 'ABC' && $FIELD5 -eq ' ' ]; then
$count
++
 
fi 
Sponsored Links
    #2  
Old 06-14-2005
rahul123_libra rahul123_libra is offline
Registered User
 
Join Date: May 2005
Last Activity: 13 November 2006, 1:04 AM EST
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
problem

Could u please provide us with the file that u r using

As the result depends upon wether the file contains 1 record or many
Sponsored Links
    #3  
Old 06-14-2005
yongho yongho is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 4 August 2005, 5:20 PM EDT
Location: New York City
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
apologies

Ah yes, of course. Let me type out the larger picture so you can see how it's working.

The ls.txt is a cut listing of filenames (one filename per line).
As I'm opening each file, which happen to be all fixed-length csv files, I'm cutting the first field and the fifth field.

The following would be an example of two records (each line in each fixed-length csv file is a record with aproximately 32 fields). I have isolated the two fields for you.

Example fields (the forum deletes extra spaces so it would be easier I think if I describe it instead: FIELD1 is in columns 2-4 (a string of 3 letters), and the the second column aka FIELD5 can either be filled with a one digit number (1-9) or a single space (" ").

PHP Code:
while read FILENAME; do
    
FIELD1=`cut -c2-5 $FILENAME`
    
FIELD5=`cut -c68 $FILENAME`
    if [ 
$FIELD1 -eq "PBR" && $FIELD5 -eq " " ]; then
        $count
++
    
fi
done 
ls.txt 
I'm kind of thinking that maybe it's not working because... if you try to use the "cut" command and it cuts a single space, maybe the shell doesn't assign anything to the variable?

Last edited by yongho; 06-14-2005 at 10:29 AM..
    #4  
Old 06-14-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 October 2014, 7:17 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts

Code:
#!/bin/ksh
..........
..........

typeset -i count

    if [ "$FIELD1" = 'PBR' -a "$FIELD2" = ' ' ]; then
        count=$((count + 1))
    fi

man test

Last edited by vgersh99; 06-14-2005 at 10:37 AM..
Sponsored Links
    #5  
Old 06-14-2005
yongho yongho is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 4 August 2005, 5:20 PM EDT
Location: New York City
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
HrrmMm..

Question: I just started learning shell scripting a few days ago and I was under the assumption that you didn't need to typeset anything.

One of my previous counters looked like this (in a while statement, not an if statement:
var1=`expr $var1 + $var2`
..and it worked.

But when I tried the expr command in the if statement, it didn't increment at all. Would you know why this is? Could it be because once you enter an if statement, you must use different rules?

Last edited by yongho; 06-14-2005 at 10:54 AM..
Sponsored Links
    #6  
Old 06-14-2005
vgersh99's Avatar
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 24 October 2014, 7:17 PM EDT
Location: Foxborough, MA
Posts: 7,667
Thanks: 152
Thanked 586 Times in 551 Posts
Quote:
Originally Posted by yongho
Question: I just started learning shell scripting a few days ago and I was under the assumption that you didn't need to typeset anything.
you don't.
if you don't,

count=$((count + 1))
becomes
count=$(($count + 1))

Quote:
Originally Posted by yongho
One of my previous counters looked like this (in a while statement, not an if statement:
var1=`expr $var1 + $var2`
..and it worked.
this NOT using ksh/bash builtin capabilities for doing simple integer math.
Quote:
Originally Posted by yongho
But when I tried the expr command in the if statement, it didn't increment at all. Would you know why this is? Could it be because once you enter an if statement, you must use different rules?
nope, that's not it.
are you saying that the 'expr ' increment did not work, but the 'count=$((count+1))' did?
there must be something else different in your 'while' loop.
is your 'while' loopbeing piped with data?
Sponsored Links
    #7  
Old 06-14-2005
yongho yongho is offline
Registered User
 
Join Date: Jun 2005
Last Activity: 4 August 2005, 5:20 PM EDT
Location: New York City
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
i think you're on to something..

Quote:
is your 'while' loopbeing piped with data?
I think it's because I'm doing something terribly wrong with my nested while statement.. Sorry I didn't mention it before, I didn't think it was the fault.

Here's the big picture.

PHP Code:
while read FILENAME; do #get filename in file_names.txt

 
while read RECORD; do #read/open each filename
 
. . . (This is where I'm putting the if statement.)
 done < $FILENAME

done < file_names.txt 
I have a while statement inside of another while statement because the first while reads the filenames I need from file_names.txt. I then need to use the 2nd while statement to begin reading each filename.

edit: Hmm.... You know what, let me work on this for another hour and come back and tell you if it's still broken.

Last edited by yongho; 06-14-2005 at 02:54 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Multipe conditions in if statement spacemancw Shell Programming and Scripting 5 08-28-2009 02:36 PM
If statement with multiple conditions cpolikowsky Shell Programming and Scripting 1 06-22-2009 12:33 PM
Two conditions in one if statement borobudur UNIX for Dummies Questions & Answers 5 07-28-2008 09:51 PM
How to giv two conditions in IF statement..?? RRVARMA Shell Programming and Scripting 6 04-25-2008 09:33 AM
if statement with two conditions cin2000 Shell Programming and Scripting 1 01-23-2006 02:21 PM



All times are GMT -4. The time now is 10:33 AM.