more elegant way for conditional statement needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting more elegant way for conditional statement needed
# 1  
Old 05-18-2006
Java more elegant way for conditional statement needed

Hi all,

I have a script which gets its input from a text file (file.txt) and processes each line within a loop.

I have a counter which increases by one and I want something to happen every 7th, 14th, 21st, etc. line read.

Currently the code looks and works perfectly like this:

------------------------------------------
count="1"

while read WORKSTATION
do
if [ $count -eq 7 ] || [ $count -eq 14 ] || [ $count -eq 21 ] ||\
[ $count -eq 28 ] || [ $count -eq 35 ] ; then

some code here ....

((count=count+1))
fi

done < file.txt ;

------------------------------------------

However, it doesn't look beautiful and with that many conditional statements (I shortened it, it goes further than just 35) I'm, looking for a more elegant solution.
I have tried this

if [ $count -eq '[7,14,21,28,35]' ] ; then

but it doesn't work...

Does anyone know the correct syntax - or is this not possible?

Many thanks in advance!

Cheers,
Tom
# 2  
Old 05-18-2006
If you include what the 7th line is (is there a repetitive pattern, etc) and what you are trying to do with/after that line, maybe there's a one-liner sed or awk solution.
# 3  
Old 05-18-2006
Code:
$ cat mod
#! /usr/bin/ksh
count=1
while ((count < 50 )) ; do
        if (( !(count%7) )) ; then
                echo count = $count
        fi
        ((count=count+1))
done
exit 0
$ ./mod
count = 7
count = 14
count = 21
count = 28
count = 35
count = 42
count = 49
$

# 4  
Old 05-18-2006
Many thanks for the fast replies!

Perderabo's solution works like a charm Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional statement in SFTP

Hi, I wanted to know if we can use conditional statements like if--else--fi inside an automated SFTP script session. (1 Reply)
Discussion started by: jhilmil
1 Replies

2. Shell Programming and Scripting

conditional statement in awk

Hi all, I have a file containing the values that would be use as the basis for printing the lines of another set of files using awk. What I want to do is something like the one below: stdev.txt 0.21 0.42 0.32 0.25 0.15 file1.txt file2.txt file3.txt ..filen.txt 0.45 0.23 ... (4 Replies)
Discussion started by: ida1215
4 Replies

3. UNIX for Advanced & Expert Users

conditional statement

I need to implement something like this. 1) search for a file(say *.doc) from a path (say /home/user/temp) 2) if file found & if file size > 0 : yes --> file valid else : print file not valid. I am trying to implement something like this, but seems i am terribly wrong somewhere.. ... (1 Reply)
Discussion started by: animesharma
1 Replies

4. 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

5. Shell Programming and Scripting

if statement with grep as conditional

Please see the script segment below for i in $files do echo $i if ; then case "$1" in "IE0263"|"IE0264"|"IE0267"|"IE0268") short_filename=`ls -l $i | cut -c108-136 | sort` ;; "IE0272"|"IE0273") short_filename=`ls -l $i | cut... (4 Replies)
Discussion started by: jmahal
4 Replies

6. Shell Programming and Scripting

if conditional statement

Hi, I have a script like this: sample.sh mapping=$1 if then echo "program passed" fi I'm running the above script as ./sample.sh pass The script is not getting executed and says "integer expression expected" Could anyone kindly help me? (2 Replies)
Discussion started by: badrimohanty
2 Replies

7. UNIX for Dummies Questions & Answers

Problem with conditional statement

Hi, I'm getting a "bad number" error from the following conditional if statement. I understand the results of the grep command are not being treated a an integer but am unsure of the correct syntax. Any help would be appreciated. if Thanks (2 Replies)
Discussion started by: dlafa
2 Replies

8. Shell Programming and Scripting

conditional statement

Hi all, The following code is to find if a list of numbers from one file are within the range in another file. awk -F, '\ BEGIN { while ((getline < "file2") > 0) file2=$3 } {for (col1 in file2) if ($0>=30 && $1<=45) print $0} ' FILE1 But where I have the number 30 and 45, I... (3 Replies)
Discussion started by: dr_sabz
3 Replies

9. Shell Programming and Scripting

conditional statement

Hi Does Unix have a conditional statement like Java as follows: Condition ? Statement1 : Statement2 Thanks (8 Replies)
Discussion started by: lalelle
8 Replies

10. Shell Programming and Scripting

quoting in conditional statement

can somebody help, what quote i should use in below statement or what wrong of it ? the 1st (*) is a char, the 2nd and 3rd (*) is a wildcard if ] && ] && ] ................^ .............^ then echo "ok" fi thanks in advance. (2 Replies)
Discussion started by: 3Gmobile
2 Replies
Login or Register to Ask a Question