The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


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

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to giv two conditions in IF statement..?? RRVARMA Shell Programming and Scripting 6 04-25-2008 06:33 AM
problem with if, while, for conditions kittusri9 Shell Programming and Scripting 3 04-24-2008 06:15 AM
reduce the or conditions hitmansilentass Shell Programming and Scripting 8 05-03-2007 02:27 PM
multiple conditions in if/then grandtheftander UNIX for Dummies Questions & Answers 4 07-21-2006 10:58 AM
if statement with two conditions cin2000 Shell Programming and Scripting 1 01-23-2006 12:21 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 06-14-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 
Reply With Quote
Forum Sponsor
  #2  
Old 06-14-2005
Registered User
 

Join Date: May 2005
Posts: 46
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
Reply With Quote
  #3  
Old 06-14-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 07:29 AM.
Reply With Quote
  #4  
Old 06-14-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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 07:37 AM.
Reply With Quote
  #5  
Old 06-14-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 07:54 AM.
Reply With Quote
  #6  
Old 06-14-2005
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 3,029
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?
Reply With Quote
  #7  
Old 06-14-2005
Registered User
 

Join Date: Jun 2005
Location: New York City
Posts: 95
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 Im 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 11:54 AM.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 05:11 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0