Sponsored Content
Top Forums Shell Programming and Scripting if statement with two conditions -e, && Post 74910 by yongho on Tuesday 14th of June 2005 10:23:01 AM
Old 06-14-2005
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 11:29 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using && in if statement ..

Hi All, Can some one tell me how to get run the following: data1="hello" data2="world" if then { echo "good afternnon" } else { echo " good morning" } fi The above code gives me an error ad below : ./if.h: line 3: ' (7 Replies)
Discussion started by: jisha
7 Replies

2. Shell Programming and Scripting

multiple conditions in if using && operator

VARIABLE="project" if && ] then echo "VARIABLE is not empty" fi this is not working what is wrong in the syntax?? (2 Replies)
Discussion started by: codeman007
2 Replies

3. Shell Programming and Scripting

PHP read large string & split in multidimensional arrays & assign fieldnames & write into MYSQL

Hi, I hope the title does not scare people to look into this thread but it describes roughly what I'm trying to do. I need a solution in PHP. I'm a programming beginner, so it might be that the approach to solve this, might be easier to solve with an other approach of someone else, so if you... (0 Replies)
Discussion started by: lowmaster
0 Replies

4. Programming

IF && statement problem

Hello there, My first time on the forums, glad to be here :) I'm completely new to programming in PHP and I have a question which I hope someone could help me with. I am currently using this statement: if(($session == 2) && ($item == Dagger) && ($item2 == Dagger)){ ... (5 Replies)
Discussion started by: Hero
5 Replies

5. Shell Programming and Scripting

How to write If statement using && and operator in Unix

Hi What is the syntax for if statement using && and || operator? if && ] || here its giving me an error to this if statement any suggestion?? (2 Replies)
Discussion started by: Avi
2 Replies

6. Shell Programming and Scripting

if statement with '&&' gives error

Hi, I'm using the && operator in if statement: if ; then exp $UID/$PWD@$ORACLE_SID FILE=./DUMP/$TODAY$CONCAT_STR$USERNAME.dmp STATISTICS=NONE LOG=./LOG/$TODAY$CONCAT_STR$USERNAME.log elif ; then expdp $UID/$PWD@$ORACLE_SID DIRECTORY=./DUMP/ DUMPFILE=$TODAY$CONCAT_STR$USERNAME.dmp... (8 Replies)
Discussion started by: priya001
8 Replies

7. Shell Programming and Scripting

Using && in if statement with 3 expressions

how do you write an if statement for something like if ((expr 1 >= expr 2 && expr 3 >= expr 4) && expr 5 <= expr 6) if ((TRUE && TRUE) && TRUE) then condition... i've done it this way but it doesn't seem to work. if (] && "$ex_day" -le "$curr_day" ); then condition... (3 Replies)
Discussion started by: angilulu
3 Replies

8. Shell Programming and Scripting

GNU & BSD Makefile Directives & Conditions Compatibility

Firstly, I would like to apologize if this is not the appropriate sub-forum to post about GNU/BSD makefile scripting. Though my code is in C++, because I am focusing on the makefile I thought it would go better in shell scripting. Please correct me if I am wrong. Secondly, I am not interested in... (0 Replies)
Discussion started by: AntumDeluge
0 Replies

9. Shell Programming and Scripting

SFTP Shell Script Get & Delete && Upload & Delete

Hi All, Do you have any sample script, - auto get file from SFTP remote server and delete file in remove server after downloaded. - only download specify filename - auto upload file from local to SFTP remote server and delete local folder file after uploaded - only upload specify filename ... (3 Replies)
Discussion started by: weesiong
3 Replies

10. Shell Programming and Scripting

Bash: How to use read with conditions & loops

Hello, Below I try to control that the input is good an IP : #!/bin/bash cp /home/scripts/choice_interfaces.txt /home/scripts/interfaces.txt chmod 644 /home/scripts/interfaces.txt echo -e "Please enter the network informations into the /etc/network/interfaces file, complete them below... (9 Replies)
Discussion started by: Arnaudh78
9 Replies
cut(1)							      General Commands Manual							    cut(1)

NAME
cut - Displays specified parts from each line of a file SYNOPSIS
cut -b list [-n] [file...] cut -c list [file...] cut -f list [-d delim] [-s] [file...] STANDARDS
Interfaces documented on this reference page conform to industry standards as follows: cut: XCU5.0 Refer to the standards(5) reference page for more information about industry standards and associated tags. OPTIONS
Cuts based on a list of bytes. Each selected byte is output, unless you also specify the -n option. For example, if you specify -b 1-72, the cut command writes out the first 72 bytes in each line of the file. Cuts based on a list of characters. It is not an error if you specify a character not in the input. Uses the specified character as the field delimiter (separator) when you specify the -f option. You must quote characters with special meaning to the shell, such as the space character. Any character can be used as delim. The default field delimiter is a tab character. Specifies a list of fields assumed to be separated in the file by a field delimiter character, speci- fied by the -d option or the tab character by default. For example, if you specify -f 1,7, the cut command writes out only the first and seventh fields of each line. If a line contains no field delimiters, the cut command passes them through intact (useful for table subhead- ings), unless you specify the -s option. Does not split characters. When specified with the -b option, each element in list of the form low-high (hyphen-separated numbers) is modified as follows: If the byte selected by low is not the first byte of a character, low is decre- mented to select the first byte of the character originally selected by low. If the byte selected by high is not the last byte of a char- acter, high is decremented to select the last byte of the character prior to the character originally selected by high, or zero (0) if there is no prior character. If the resulting range element has high equal to zero (0) or low greater than high, the list element is dropped from list for that input line without causing an error. Each element in list of the form low- is treated as previously described with high set to the number of bytes in the current line, not including the terminating newline character. Each element in list of the form -high is treated as previously described with low set to 1. Each element in list of the form number (a single number) is treated as previously described with low set to number and high set to number. Suppresses lines that do not contain delimiter characters (use only with the -f option). Unless you include this option, lines with no delimiters are passed through. OPERANDS
The path name of the file to be examined. If you do not specify a file or you specify a hyphen (-), the cut command reads standard input. DESCRIPTION
The cut command locates the specified fields in each line of the specified file and writes the characters in those fields to standard out- put. You must specify the -b option (to select bytes), the -c option (to select characters) or the -f option (to select fields). The list argu- ment (see the -b, -c, and -f options) must be a space-separated or comma-separated list of positive numbers and ranges. Ranges can be in three forms: Two positive numbers separated by a hyphen (-), as in the form low-high, which represents all fields from the first number to the second number. A positive number preceded by a hyphen (-), as in the form -high, which represents all fields from field number 1 to that number. A positive number followed by a hyphen (-), as in the form low-, which represents that number to the last field, inclusive. The elements in list can be repeated, can overlap, and can be specified in any order. Some sample list specifications are as follows: First, fourth, and seventh bytes or fields. First through third and eighth bytes or fields. First through fifth and tenth bytes or fields. Third through last bytes or fields. The fields specified by list can be a fixed number of byte positions, or the length can vary from line to line and be marked with a field delimiter character, such as a tab character. [Tru64 UNIX] You can also use the grep command to make horizontal cuts through a file and the paste command to put the files back together. To change the order of columns in a file, use the cut and paste commands. EXIT STATUS
The following exit values are returned: Successful completion. An error occurred. EXAMPLES
To display several fields of each line of a file, enter: cut -f 1,5 -d : /etc/passwd This displays the login name and full user name fields of the system password file. These are the first and fifth fields (-f 1,5) sepa- rated by colons (-d :). So, if the /etc/passwd file looks like this: su:UHuj9Pgdvz0J":0:0:User with special privileges:/: daemon:*:1:1::/etc: bin:*:2:2::/usr/bin: sys:*:3:3::/usr/src: adm:*:4:4:System Admin- istrator:/usr/adm: pierre:*:200:200:Pierre Harper:/u/pierre: joan:*:202:200:Joan Brown:/u/joan: Then, cut -f 1,5 -d : /etc/passwd produces this output: su:User with special privileges daemon: bin: sys: adm:System Administrator pierre:Pierre Harper joan:Joan Brown ENVIRONMENT VARIABLES
The following environment variables affect the execution of cut: Provides a default value for the internationalization variables that are unset or null. If LANG is unset or null, the corresponding value from the default locale is used. If any of the internationalization vari- ables contain an invalid setting, the utility behaves as if none of the variables had been defined. If set to a non-empty string value, overrides the values of all the other internationalization variables. Determines the locale for the interpretation of sequences of bytes of text data as characters (for example, single-byte as opposed to multibyte characters in arguments and input files). Determines the locale for the format and contents of diagnostic messages written to standard error. Determines the location of message catalogues for the processing of LC_MESSAGES. SEE ALSO
Commands: grep(1), fold(1), join(1), paste(1) Standards: standards(5) cut(1)
All times are GMT -4. The time now is 05:41 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy