The UNIX and Linux Forums  

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


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
set Working day in ksh Rafael.Buria Shell Programming and Scripting 1 10-09-2007 01:42 AM
FTP is not working. ronald_brayan Security 10 04-04-2007 05:23 AM
^P Not working on AIX. akbar AIX 0 02-24-2007 03:52 PM
sed not working baanprog UNIX for Advanced & Expert Users 5 09-18-2006 05:11 AM
why is this not working? matt2kjones UNIX for Dummies Questions & Answers 2 07-09-2002 12:58 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-11-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
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"
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-11-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,199
Try to modify your test :
Code:
if [[ $in_seq_num = "001" || $in_seq_num = "004" || $in_seq_num = "007" ]];

Jean-Pierre.
Reply With Quote
  #3 (permalink)  
Old 05-11-2007
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 3,494
Code:
#!/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
Reply With Quote
  #4 (permalink)  
Old 05-11-2007
Registered User
 

Join Date: Apr 2007
Posts: 22
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 !!
Reply With Quote
  #5 (permalink)  
Old 05-11-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
Quote:
Originally Posted by jim mcnamara
Code:
#!/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
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"
Reply With Quote
  #6 (permalink)  
Old 05-11-2007
aigles's Avatar
Registered User
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,199
Code:
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 :
Code:
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.
Reply With Quote
  #7 (permalink)  
Old 05-11-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 487
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
Google UNIX.COM
Reply

Tags
bash, bash eval, eval

Thread Tools
Display Modes




All times are GMT -7. The time now is 04:10 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0