Not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Not working
# 1  
Old 05-11-2007
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"
# 2  
Old 05-11-2007
Try to modify your test :
Code:
if [[ $in_seq_num = "001" || $in_seq_num = "004" || $in_seq_num = "007" ]];


Jean-Pierre.
# 3  
Old 05-11-2007
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

# 4  
Old 05-11-2007
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 !!
# 5  
Old 05-11-2007
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"
# 6  
Old 05-11-2007
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.
# 7  
Old 05-11-2007
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
}


Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Disk Space Utilization in HTML format working in one environment and not working on the other

Hi Team, I have written the shell script which returns the result of the disk space filesystems which has crossed the threshold limit in HTML Format. Below mentioned is the script which worked perfectly on QA system. df -h | awk -v host=`hostname` ' BEGIN { print "<table border="4"... (13 Replies)
Discussion started by: Harihsun
13 Replies

2. Shell Programming and Scripting

Working web service call not working with curl

Hello, Newbie here, I have a perfectly well working web service call I can issue from chrome (PC Windows 10) and get the results I want (a dimmer being turned on in Fibaro Home Center 2 at level 40) I am not allowed to post urls but the below works with http and :// and... (3 Replies)
Discussion started by: abigbear
3 Replies

3. Shell Programming and Scripting

PHP cronjob not working but manual working

Hi, Can anyone help me on my PHP cron not working, but when i do the manual it work. # manual run working /usr/local/bin/php /root/dev/test.php # crontab not working 55 8 * * * /usr/local/bin/php /root/dev/test.php Thank in advances Regards, FSPalero Please use CODE tags as... (2 Replies)
Discussion started by: fspalero
2 Replies

4. Shell Programming and Scripting

Automating pbrun /bin/su not working, whenever manually it is working using putty

I am trying to automate a script where I need to use pbrun /bin/su but for some reason it is not passing thru the pbrun as my code below. . ~/.bash_profile pbrun /bin/su - content c h 1 hpsvn up file path I am executing this from an external .sh file that is pointing to this scripts file... (14 Replies)
Discussion started by: jorgejac
14 Replies

5. Red Hat

Nslookup working but ping not working at windows client

Hi Team we have created a DNS server at RHEL6.2 environment in 10.20.203.x/24 network. Everything is going well on linux client as nslookup, ping by host etc in entire subnet. We are getting problem in windows client as nslookup working as well but not ping. all the firewall is disabled and... (5 Replies)
Discussion started by: boby.kumar
5 Replies

6. Shell Programming and Scripting

Script not working in cron but working fine manually

Help. My script is working fine when executed manually but the cron seems not to catch up the command when registered. The script is as follow: #!/bin/sh for file in file_1.txt file_2.txt file_3.txt do awk '{ print "0" }' $file > tmp.tmp mv tmp.tmp $file done And the cron... (2 Replies)
Discussion started by: jasperux
2 Replies

7. Solaris

SSH: internal working but external not working

Hi, This is a strange issue: We have an sftp server. Users can ssh to it from internal LAN without any issue, but they can not ssh to it externally via firewall. Here is what I got: OS is Solaris 9. No hosts.allow and hosts.deny files. Please help. Thank you in advance! (7 Replies)
Discussion started by: aixlover
7 Replies

8. Shell Programming and Scripting

Script is not working from cron while working manually

Hello, I am facing a very strange problem when I run my script manuallu ./Fetchcode which is using to connect with MKS integrity from linux end it workks fine but when I run it from cron it doesn't work.Can someone help me 1) How could I check my script when it is running from cron like... (3 Replies)
Discussion started by: anuragpgtgerman
3 Replies

9. Linux

FTP not working under Linux but working under any other OS ??? Very strange

Dear all, I am totally despaired and puzzled. Using Filezilla under Windows under the same network as our Linux servers is working. Using FTP command-line client under any of our Linux debian servers is not working ! I tried with different FTP servers -> same problem ! All commands are... (12 Replies)
Discussion started by: magix_ch
12 Replies

10. Solaris

GUI not working... CLI is working fine

Hello, I have X4500 running Solaris 10. I can access it through CLI but I cannot see the GUI. When I reboot it, the GUI works till all the files are loaded (ie., the initial boot sequence) and it prompts me to enter username and password and there it ends. The screen just has a blinking cursor... (4 Replies)
Discussion started by: bharu_sri
4 Replies
Login or Register to Ask a Question