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.
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
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 !!
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
(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
}