The UNIX and Linux Forums  

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



Thread: Not working
View Single Post in UNIX Forums - Click on the Thread or Permalink to View Entire Thread -->
  #7 (permalink)  
Old 05-11-2007
cfajohnson's Avatar
cfajohnson cfajohnson is online now
Shell programmer, author
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 977
Quote:
Originally Posted by Mandab
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 :

Please put code in CODE tags.

Quote:
#!/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)

You don't need awk for that:

Code:
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.

Quote:
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"

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:

Code:
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:

Code:
_substr() {
    if [ $2 -lt 0 ]
    then
      _SUBSTR=${1:$2:${3:-${#1}}}
    else
      _SUBSTR=${1:$(($2 - 1)):${3:-${#1}}}
    fi
}
Code:
_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
}

Reply With Quote