Visit The New, Modern Unix Linux Community


Output checker setting variable to TRUE or FALSE


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Output checker setting variable to TRUE or FALSE
# 1  
Output checker setting variable to TRUE or FALSE

Hi All,
I'm trying to come up a way to check the output of some data i have. I need to be able to check for the order of the output and if its correct set a variable to false if it isnt.

Currently the data is in the below format, this is the value which should cause the variable be set to true.
Code:
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb

If the data is out of order (example below) then the variable should be set to FALSE. I then have a script that will send me a notification if the variable equals FALSE.

Code:
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb
  Flags: x              Service: aaa
  Flags: x              Service: bbb

The string after
Code:
Service:

should be able to be modified as i have a few different types of data to check.

Any help anyone could offer along with explaining would be great.

Thanks
# 2  
This might help, but i have a feeling the problem is more complex than you mentioned. You need to fill the arrays with your values correspondingly. If "aaa" is expected to be followed by "bbb" then you need to have "aaa" as array element in achFirst[] and "bbb" as the array element with the same index in achSecond[].

Code:
#! /bin/ksh

typeset fInput="/path/to/input"

typeset achFirst[1]="aaa"
typeset achFirst[2]="..."         # enter other possible values for "aaa"
[...]

typeset achSecond[1]="bbb"
typeset achSecond[2]="..."         # enter other possible values for "bbb"
[...]

typeset    chLineBuffer=""
typeset -i iIndexFound=0
typeset -i iLineCounter=0

while read chLineBuffer ; do
     (( iLineCounter += 1 ))
                                                # cut off everything leaving service name
     chLineBuffer="${chLineBuffer## Flags: x              Service: }"

     if [ $iIndexFound -eq 0 ] ; then     # first line of a pair
          while [ $iIndexFound -le ${#achFirst[$*]} ] ; do
               if [ "${achFirst[$iIndexFound]" == "$chLineBuffer" ] ; then
                    break
               fi
               (( iIndexFound += 1 ))
          done
          if [ $iIndexFound -gt ${#achFirst[$*]} ] ; then
               print -u2 "unkown Service identifier found, aborting (Line: $iLineCounter)"
               exit 1
          fi
     else
          if [ "$chLineBuffer" != "${achSecond[$iIndexFound]}" ] ; then
               print -u2 "expected: ${achSecond[$iIndexFound]}   found: $chLineBuffer (Line: $iLineCounter)"
               exit 2
          fi
     fi
done < "$fInput"

I hope this helps.

bakunin

Last edited by bakunin; 02-06-2016 at 08:05 AM..
# 3  
I forgot to mention it may not always start with aaa for example, it may start with bbb then aaa. I'm not bothered what the first one is as long as it's not got two lines one after the other with the same value.

Not sure if the above would work based on that.


Cheers
# 4  
Put this (bash or ksh required)
Code:
IF=$1
shift
CB=($@)
{ read _ _ _ SQ
  CNT=0
  [ $SQ = ${CB[$((CNT++))]} ] || [ $SQ = ${CB[$((CNT++))]} ] || exit 1
  while read _ _ _ SQ;
    do  ((CNT%=2))
        [ $SQ = ${CB[$((CNT++))]} ] || exit 1
    done
} < $IF

into a script file, make it executable and run like
Code:
./scriptfile datafile aaa bbb && STAT=0 || STAT=1

# 5  
Quote:
Originally Posted by mutley2202
I forgot to mention it may not always start with aaa for example, it may start with bbb then aaa.
I forgot to mention that to understand the code it is necessary to read the accompanying explanation. To quote from what i have written:

Quote:
Originally Posted by bakunin
You need to fill the arrays with your values correspondingly. If "aaa" is expected to be followed by "bbb" then you need to have "aaa" as array element in achFirst[] and "bbb" as the array element with the same index in achSecond[].

If "aaa" should be followed by "bbb" and "bbb" should be followed by "aaa" the prior quote might serve as a remote indication that

Code:
typeset achFirst[1]="aaa"
typeset achFirst[2]="bbb"

typeset achSecond[1]="bbb"
typeset achSecond[2]="aaa"

incidentally may do what you want. To answer your question:

Quote:
Originally Posted by mutley2202
Not sure if the above would work based on that.
In general reading and analysing a program helps with understanding how it works. In fact the program can do what you want although it is overly complicated for that purpose because you changed the requirements from:

Quote:
I need to be able to check for the order of the output
to
Quote:
I'm not bothered what the first one is
Before you ask: yes, in fact there is a miniscule difference between "checking the order" and "not checking the order".

I hope this helps.

bakunin
 

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #494
Difficulty: Easy
A block of code is a section of code that has been logically grouped together.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

using awk for setting variable but change the output of this variable within awk

Hi all, Hope someone can help me out here. I have this BASH script (see below) My problem lies with the variable path. The output of the command find will give me several fields. The 9th field is the path. I want to captured that and the I want to filter this to a specific level. The... (6 Replies)
Discussion started by: Cowardly
6 Replies

2. Solaris

True or false ? - Sun cluster 3.2 U3 questions...

I'm using clustered zones on my machine. i'm only at the test phase of my design and ultimately the oracle zones will be using VxVM. When the testing phase is complete, VxVM will be used in the containers. It is necessary for VxVM to run in the global zone for the containers to use it (is... (5 Replies)
Discussion started by: frustin
5 Replies

3. Shell Programming and Scripting

Interesting TCL behavior: 007 == 7 is true; 008==8 is false.

Hi all, If anyone has the explanation for the following issue, please share it with me. I am comparing two variable a and b with the values of 007 and 7, for these values it get evaluated as True. For a=008 and b=8, for these values it get evaluated as false. #!/bin/tclsh set a 007 ... (3 Replies)
Discussion started by: sarwan
3 Replies

4. Shell Programming and Scripting

Help with setting a variable!

I am working within a while loop and i am trying to set a variable that will read out each count of the files. the problem is the count variable i have set up gives me a total and not the individual count of each file. in the data area there is 4 abc.dat and 1 def.dat. how can i do this??? ... (2 Replies)
Discussion started by: Pablo_beezo
2 Replies

5. UNIX for Dummies Questions & Answers

setting a variable

In my script, I have the following command.... du -sk `ls -ltd sales12|awk '{print $11}'`|awk '{print $1}' it returns the value 383283 I want to modify my script to capture that value into a variable. So, I try doing the following... var1=`du -sk `ls -ltd sales12|awk '{print... (5 Replies)
Discussion started by: tumblez
5 Replies

6. Shell Programming and Scripting

Setting variable

How do you set a varible with information that contains a string and also another variable? For example: subject="Attention: $name / This $type needs your attention" The $xxxx are of course other variables that I instantiated earlier. Is it like Java where you have to use double quotes and... (1 Reply)
Discussion started by: briskbaby
1 Replies

7. Shell Programming and Scripting

Variable setting help please

L=0 cat test.sh | while read line do L='expr $1 + 1' echo $L done echo $l >>> the echo $L at the end produces 0 but i actually want it to produce the number of lines - any idea why this is happening? (16 Replies)
Discussion started by: penfold
16 Replies

8. UNIX for Dummies Questions & Answers

Setting a variable

I want to set a variable to be any number of dashes. Rather than doing the following: MYVAR="------------------" I'd like to be able to set the variable to, say, 80 dashes but don't want to have to count 80 dashes. Is there a way to do this? (2 Replies)
Discussion started by: photh
2 Replies

9. Programming

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

10. UNIX for Dummies Questions & Answers

setting CC variable

I am trying to install GCC-3.1.1 on an SGI Indigo2. I already have MIPSpro 7.2.1 installed. However, when I try to configure GCC-3.1.1, I get the message "cc ERROR: cc -o conftest -g failed, You must set the environment variable CC to a working compiler." What is the name of the MIPSpro c++... (1 Reply)
Discussion started by: mdbanas
1 Replies

Featured Tech Videos