Not working

Hi,
I have a file whose first line has some name followed by the timestamp and the sequence number. The last 3 digits of that line is seq number. If I get seq num other than 001, 004, 007 then I should generate an error saying, incorrect seq number else success. I am trying to run the following script but it is not giving me the exact result. It is working if I give -eq but with -ne it is not working. Can anyone help me out plz? sys_nam and timestamp I want for other purpose.

Script :

#!/bin/ksh

eval $(awk 'NR==1 {
printf "sys_nam=\"%s\"\n", substr($0,1,length-17) ## system_name
printf "header_tstamp=\"%s\"\n", substr($0,length-16, 14) ## timestamp
printf "in_seq_num=\"%s\"\n", substr($0, length-2) ## seq_num
}' $1)

if [[ $in_seq_num -ne "001" || $in_seq_num -ne "004" || $in_seq_num -ne "007" ]];
then
notice_val="103"
notice_desc="Seq number not as exptected"
else
notice_val="000"
notice_desc="Success"
fi

echo "$in_seq_num"
echo "$notice_val"
echo "$notice_desc"

Try to modify your test :

if [[ $in_seq_num = "001" || $in_seq_num = "004" || $in_seq_num = "007" ]];

Jean-Pierre.

#!/bin/ksh                                                                       
                                                                                 
okay=$(head -1 $1 | awk '{print substr($0, length($0)-2)}' | \
       awk '{print index("001|004|007",$0)}')                                                              
if  [[ $okay -gt 0 ]]  then
      print "okay"
else
      print "not okay"
fi

Thank you very much for your quick response.
As I said earlier also, that it is working if I check for -eq, but if I want to check for -ne or != it is not working. Please help me out !!

jim nice idea, it does not work - at least not with Solaris's 'nawk'.
Couple of things:

  1. the 'index' signature is wrong: index(s,t): Return the position, in characters, numbering from 1,
  2. 't' is a STRING and not a regex (at least on Solaris' "nawk"
if [[ $in_seq_num != "001" && $in_seq_num != "004" && $in_seq_num != "007" ]];
then
   notice_val="103"
   notice_desc="Seq number not as exptected"
else
   notice_val="000"
   notice_desc="Success"
fi

Or Try this test syntax :

if [[ $in_seq_num != @(001|004|007) ]];
then
   notice_val="103"
   notice_desc="Seq number not as exptected"
else
   notice_val="000"
   notice_desc="Success"
fi

Jean-Pierre.

Please put code in CODE tags.

You don't need awk for that:

read line < "$1"
_substr "$line" 1 $(( ${#line} - 17 ))
sys_nam=$_SUBSTR

_substr "$line" $(( ${#line} - 16 )) 14
header_tstamp=$_SUBSTR

_substr "$line" $(( ${#line} - 2 ))
in_seq_num=$_SUBSTR


(And if you do use awk, put in an exit command so that it doesn't read the whole file for nothing.)

See below for the _substr function.

Don't use the non-portable [[...]] syntax, and remember that numbers with leading zeroes are interpreted as octal number.

The best method in this instance is case:

case $in_seq_num in
  001|004|007)
        notice_val="000"
        notice_desc="Success"
         ;;
  *)
         notice_val="103"
         notice_desc="Seq number not as expected"
        ;;
esac


The _substr function:

There are two different _substr functions, the first for ksh93 or bash (version 2 or later), the second for any POSIX shell:

_substr() {
    if [ $2 -lt 0 ]
    then
      _SUBSTR=${1:$2:${3:-${#1}}}
    else
      _SUBSTR=${1:$(($2 - 1)):${3:-${#1}}}
    fi
}


_substr()
{
   _SUBSTR=

    ## store the parameters
    ss_str=$1
    ss_first=$2
    ss_length=${3:-${#ss_str}}

    ## return an error if the first character wanted is beyond end of string
    if [ $ss_first -gt ${#ss_str} ]
    then
      return 1
    fi

    if [ $ss_first -gt 1 ]
    then

      ## build a string of question marks to use as a wildcard pattern
      _repeat "?" $(( $ss_first - 1 ))

      ## remove the beginning of string
      ss_str=${ss_str#$_REPEAT}
    elif [ ${ss_first} -lt 0 ] ## ${#ss_str} ]
    then
      ## count from end of string
      _repeat "?" ${ss_first#-}

      ## remove the beginning
      ss_str=${ss_str#${ss_str%$_REPEAT}}
    fi

    ## ss_str now begins at the point we want to start extracting
    ## print the desired number of characters
    if [ ${#ss_str} -gt $ss_length ]
    then
      _repeat "${ss_wild:-??}" $ss_length
      ss_wild=$_REPEAT
      _SUBSTR=${ss_str%${ss_str#$ss_wild}}
    else
      _SUBSTR=${ss_str}
    fi
}



 awk 'NR==1 {	
	sysname = sprintf("%s",substr($0,1,length-17) )
	hdtimestamp = sprintf("%s",substr($0,length-16, 14) )
	seqnum = sprintf("%s",substr($0, length-2) )  
        if (seqnum!="001" || seqnum!="004" || seqnum!="007" ) {
          notice_val="103"
          notice_desc="Seq number not as exptected"
        } 
        else {
           notice_val="000"
           notice_desc="Success"
        }
        printf "%s %s %s" , seqnum,notice_val,notice_desc
} ' "file"