HP-UX: Shell Script giving " 0^J30: Syntax error"

Hi All,
We are getting a very unique error while running a shell script on HP-UX box.
Can somebody help in this regards?

The shell script is working fine on linux/solaris box.

Error:
++++++++++++++++++++++++

$/test.sh
./test.sh[48]: 0^J30: Syntax error
$

++++++++++++++++++++++++

TIA.

PS: If required, can post the shell script.

Yes that would be required, or at least the relevant section, anonymized if need be, and used with [code] tags.

1 Like

Please see the full script:
(Note: it works on Linux, but gives the error on HP-UX)

 
#!/bin/sh

#=============================================================
# Variable Declaration
#=============================================================
server=`hostname`
configfile="$HOME/config_file"
flagfile="$HOME/flag_file"
mailfile="$HOME/mail_file"
tmpflagfile="$HOME/tmp_flag_file"
#=============================================================
cat abc|tail -n +5 |tr -s " "|sed '$d'|cut -d " " -f3,4 > ${configfile}
for queue in `cat ${configfile}|cut -d ' ' -f1`
do
   if [ $queue = "q_ft_out" ]
   then
      threshold=30
   else
      threshold=20
   fi
   size=`cat ${configfile}|grep "$queue"|cut -d " " -f2`
   if [ -f "${flagfile}" ]
   then
      x=`grep $queue ${flagfile} | wc -l`
      if [ "$x" -ge 1 ]
      then
         if [ "$size" -ge "$threshold" ]
         then
            echo " SERVER:$server       QUEUENAME:$queue             SIZE:$size " >> ${mailfile}
         else
           echo "`grep -v $queue ${flagfile}`" > ${tmpflagfile} && mv ${tmpflagfile} ${flagfile}
         fi
      else
        if [ "$size" -ge "$threshold" ]
        then
           echo "$server $queue $size" >> ${flagfile}
        fi
      fi
   else
   if [ "$size" -ge "$threshold" ]
   then
      echo "$server $queue $size" >> ${flagfile}
      fi
   fi
done
if [ -f ${mailfile} ]
then
cat ${mailfile} | mailx -s "TEST : Queues Which are Above Threshold on $server" "*****@*****"
#============================================================
# Remove Unwanted Files
#============================================================
rm ${mailfile}
fi

Hmm, some UUOC but cannot see a syntax error.
Even not in line 48.
Were there some special characters?
Perhaps they were removed when you copied to this Wiki page?
Can you copy back to testnew.sh and run

diff test.sh testnew.sh
1 Like

Thanks for checking.

Sorry: please dont conisider "line 48" as that giving the error.
Actaully, while copying the script on this page, I removed some comments and extra lines.
So, if I run the above code exactly without changes ON HP UX, I am getting the error on line 27.
Please see below.

Note:
I switch to another user (su) while running the script, might that be an issue?
Can you please guide how I can ensure that the user's profile and environment is not causing an issue?

>./test.sh
./test.sh[27]: 0^J30: Syntax error
>uname -a
HP-UX **** B.11.31 U ia64 0320060873 unlimited-user license

Looks like your configfile has some special characters.
Can you paste the output of od -bc configfile

--ahamed

Line 24 has an unquoted argument for grep that does an unwanted match against the current directory (and eventual replacement).
Please change it to this:

...
      x=`grep -c "$queue" ${flagfile}`
      if [ "$x" -ge 1 ]
...

Dito line 15. Please change to this:

   if [ "$queue" = "q_ft_out" ]
   then

Indeed the queue variable is fed from configfile that likely contains strange characters.

1 Like

Hi All,

Please see the output of "od -bc configfile"

0>od -bc config_file
0000000   t   e   l   l   a   b   s   7   1   0   0       0  \n   t   e
        164 145 154 154 141 142 163 067 061 060 060 040 060 012 164 145
0000020   l   l   a   b   s   7   1   0   0   N       3   0  \n   t   e
        154 154 141 142 163 067 061 060 060 116 040 063 060 012 164 145
0000040   m   s   _   x   m   l   _   i   f   _   s   n   d   _   q
        155 163 137 170 155 154 137 151 146 137 163 156 144 137 161 040
0000060   0  \n   q   _   f   t   _   o   u   t       4   0   0   0  \n
        060 012 161 137 146 164 137 157 165 164 040 064 060 060 060 012
0000100   z   h   o   n   e   A   r   c   a   d   a   c   s       0  \n
        172 150 157 156 145 101 162 143 141 144 141 143 163 040 060 012
0000120

Please correct the code tags, it is difficult to read.
Also, paste the output of sh -x yourscript

1 Like

Hello,

I made the below changes, but I getting the same error on line 27.

x=`grep -c "$queue" ${flagfile}`
...
...
if [ "$queue" = "q_ft_out" ]

Please paste the output of

sh -x yourscript

The code tags you are using is wrong, it is a forward slash

output of sh -x <script>

>sh -x test.sh
+ + hostname
server=******
+ configfile=/home/spgroup/sup/config_file
+ flagfile=/home/spgroup/sup/flag_file
+ mailfile=/home/spgroup/sup/mail_file
+ tmpflagfile=/home/spgroup/sup/tmp_flag_file
+ cut -d   -f3,4
+ sed $d
+ cat abc
+ tail -n +5
+ tr -s
+ 1> /home/spgroup/sup/config_file
+ cat /home/spgroup/sup/config_file
+ cut -d   -f1
+ [ tellabs7100 = q_ft_out ]
+ threshold=20
+ + cat /home/spgroup/sup/config_file
+ cut -d   -f2
+ grep tellabs7100
size=0
30
+ [ -f /home/spgroup/sup/flag_file ]
+ + grep -c tellabs7100 /home/spgroup/sup/flag_file
x=1
+ [ 1 -ge 1 ]
+ [ 0
30 -ge 20 ]
test.sh[27]: 0^J30: Syntax error
+ grep -v tellabs7100 /home/spgroup/sup/flag_file
+ echo lhprod18 q_ft_out 4000
+ 1> /home/spgroup/up/tmp_flag_file
+ mv /home/spgroup/sup/tmp_flag_file /home/spgroup/sup/flag_file
+ [ tellabs7100N = q_ft_out ]
+ threshold=20
+ + cat /home/spgroup/sup/config_file
+ cut -d   -f2
+ grep tellabs7100N
size=30
+ [ -f /home/spgroup/sup/flag_file ]
+ + grep -c tellabs7100N /home/spgroup/sup/flag_file
x=0
+ [ 0 -ge 1 ]
+ [ 30 -ge 20 ]
+ echo lhprod18 tellabs7100N 30
+ 1>> /home/spgroup/sup/flag_file
+ [ tems_xml_if_snd_q = q_ft_out ]
+ threshold=20
+ + cat /home/spgroup/sup/config_file
+ cut -d   -f2
+ grep tems_xml_if_snd_q
size=0
+ [ -f /home/spgroup/sup/flag_file ]
+ + grep -c tems_xml_if_snd_q /home/spgroup/sup/flag_file
x=0
+ [ 0 -ge 1 ]
+ [ 0 -ge 20 ]
+ [ q_ft_out = q_ft_out ]
+ threshold=30
+ + cat /home/spgroup/sup/config_file
+ grep q_ft_out
+ cut -d   -f2
size=4000
+ [ -f /home/spgroup/sup/flag_file ]
+ + grep -c q_ft_out /home/spgroup/sup/flag_file
x=1
+ [ 1 -ge 1 ]
+ [ 4000 -ge 30 ]
+ echo  SERVER:lhprod18       QUEUENAME:q_ft_out             SIZE:4000
+ 1>> /home/spgroup/sup/mail_file
+ [ zhoneArcadacs = q_ft_out ]
+ threshold=20
+ + cat /home/spgroup/sup/config_file
+ cut -d   -f2
+ grep zhoneArcadacs
size=0
+ [ -f /home/spgroup/sup/flag_file ]
+ + grep -c zhoneArcadacs /home/spgroup/sup/flag_file
x=0
+ [ 0 -ge 1 ]
+ [ 0 -ge 20 ]
+ [ -f /home/spgroup/sup/mail_file ]
+ mailx -s TEMS : Queues Which are Above Threshold on lhprod18 *****@*****
+ cat /home/spgroup/sup/mail_file
+ rm /home/spgroup/sup/mail_file

The grep command is getting 2 values for tellabs7100

0 for tellabs7100
30 for tellabs7100N

and that is why the value 0^J30 is seen, ^J is actually unix line feed.

Since grep-ing for tellabs7100 will match both the lines containing tellabs7100 and tellabs7100N .

Add the switch -w to the grep statement.

...
size=`grep -w "$queue" ${configfile} | cut -d " " -f2`
...

And try to avoid the UUoC

2 Likes

I made the below change and the script ran fine...!

Thank you all for your time; pointing out the issues and getting it resolved.

:slight_smile:

will remember to check UUOC from next time...

size=`grep -w "$queue" ${configfile} | cut -d " " -f2`

Thanks Again..

A better approach would be to use a while read loop instead:

while read queue size
do
  ...
done < $configfile

Instead of

for queue in `cat ${configfile}|cut -d ' ' -f1`
do
  ...
  size=`cat ${configfile}|grep "$queue"|cut -d " " -f2`
  ...
done
1 Like

Thanks for suggestion on using 'WHILE' loop with 'READ'.
Will try to use this in the script.

Another tip:

size=`awk '$1==q {print $2; exit}' q="$queue" ${configfile}`

compares exactly the first column, and returns after the first match, so is more robust than

size=`grep -w "$queue" ${configfile} | cut -d " " -f2`

In contrast to grep it does not use regular expressions e.g. "tellabs[78]100" won't match.