LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2016, 01:07 PM   #1
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Rep: Reputation: Disabled
./queue.sh: line 17: [: : integer expression expected


Dears please I need support regarding my script as you can find below my script to monitor my postfix mailq but when run my scripts I got this error :./queue.sh: line 17: [: : integer expression expected
please your support

#!/bin/bash

queuelength=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
queuecount=`echo $queuelength | grep "[0-9]"`
crit=100

if [ "$queuelength" -eq "$crit" ]; then
(echo open relay.mydomain.com 25
sleep 8
echo helo "my relay IP"
echo mail from: postmaster@mydomain.com
sleep 2
echo rcpt to: '<tashman@mydomain.com>'
sleep 2
echo data
sleep 2
echo subject: Mail Queue Alert of Hosting server
echo
echo
echo This is a mail from Hosting Server There is a problem in Mail Queue Kindly check the server there are ${queuelength} mails in the queue
sleep 5
echo .
sleep 5
echo quit ) | telnet

fi
exit 35

Last edited by tashman1989; 08-31-2016 at 03:22 PM.
 
Old 08-31-2016, 01:28 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
1. Please use [code] and [/code] tags when quoting code.

2. Please quote the actual script, otherwise we couldn't tell what is in line 17
 
Old 08-31-2016, 03:14 PM   #3
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Thanks dear for your reply and kindly find below my script as per your request

Code:
#!/bin/bash
# 30/08/2016 
#else
#
#if [ "$queuecount" == "" ]; then
#        echo 0;
#else
#        echo ${queuelength};
#fi
#
# 

queuelength=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
queuecount=`echo $queuelength | grep "[0-9]"`
crit=100

if [ "$queuelength" -gt "$crit" ]; then
(echo open rely.mydomain.com 25
sleep 8
echo helo 82.212.85.199
echo mail from: postmaster@mydomain.com
sleep 2
echo rcpt to: '<ttashman@mydomain.com>'
sleep 2
echo data
sleep 2
echo subject:  Mail Queue Alert of Hosting server 
echo
echo
echo This is a  mail from Hosting Server There is a problem in Mail Queue Kindly check the server  there are ${queuelength} mails in the queue
sleep 5
echo .
sleep 5
echo quit ) | telnet

fi
exit 35
the problem in if [ "$queuelength" -gt "$crit" ]; then //line 17

Last edited by tashman1989; 08-31-2016 at 03:26 PM.
 
Old 08-31-2016, 04:41 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
do a ' echo "$queuelength" ' to verify what it contains
 
Old 09-01-2016, 01:12 AM   #5
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Hello dear I did but no result
 
Old 09-01-2016, 02:56 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Quote:
Originally Posted by tashman1989 View Post
Hello dear I did but no result
Which is your problem. If the variable contains no data your if does not have a digit to compare to.
So you will need to code for the instance where your variable will not contain anything.

I would also suggest using (()) when performing an arithmetic test.
 
Old 09-01-2016, 02:56 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
I did but no result
give us no information. You ought to tell us what's happened exactly and what was expected. Otherwise we can only guess...

Insert set -xv at the beginning of your script and you will see what's happening and what's wrong. (that will not solve anything, just print a lot of information about the execution)
 
Old 09-01-2016, 03:17 AM   #8
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Dears when running echo "$queuelength" I get below

Code:
[root@hostsvr opt]# echo "$queuelength"

[root@hostsvr opt]#
 
Old 09-01-2016, 03:20 AM   #9
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
dear I get below when running set -xv

Code:
[root@hostsvr opt]# set -xv ./queue.sh
printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
++ printf '\033]0;%s@%s:%s\007' root hostsvr /opt
[root@hostsvr opt]#

printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"
++ printf '\033]0;%s@%s:%s\007' root hostsvr /opt
[root@hostsvr opt]#
 
Old 09-01-2016, 07:48 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You were supposed to place set -xv in the script after the top line
 
Old 09-01-2016, 08:44 AM   #11
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
As already mentioned you should check which is the output of the command:

Code:
/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'
According to your code the output of this command is stored in the variable $queuelength that few lines after (at line 17) is compared by the if command. The if comparison at line 17 will work *ONLY* if the variable $queuelength contains an integer value. If it contains letters (or if it is empty) it will NOT work. I guess this is what happens in your case...
 
Old 09-02-2016, 08:59 AM   #12
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
Sorry dear grail for being late and kindly find below the out


Code:
[root@hostsvr opt]# ./queue.sh
queuelength=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'
++ /usr/sbin/postqueue -p
++ awk '{print $5}'
++ tail -n1
+ queuelength=
queuecount=`echo $queuelength | grep "[0-9]"`
echo $queuelength | grep "[0-9]"
++ grep '[0-9]'
++ echo
+ queuecount=
crit=100
+ crit=100



if [ "$queuelength" -eq  "$crit" ]; then
(echo open relay.mydomain.com 25
sleep 8
echo helo relay.mydomain.com
echo mail from: postmaster@mydomain.com
sleep 2
echo rcpt to: '<tashman@mydomain.com>'
sleep 2
echo data
sleep 2
echo subject:  Mail Queue Alert of Hosting server
echo
echo
echo This is a  mail from Hosting Server There is a problem in Mail Queue Kindly check the server  there are ${queuelength} mails in the queue
sleep 5
echo .
sleep 5
echo quit ) | telnet

fi
+ '[' '' -eq 100 ']'
./queue.sh: line 17: [: : integer expression expected
exit 35
+ exit 35
 
Old 09-02-2016, 09:19 AM   #13
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
Quote:
Originally Posted by tashman1989

Code:
queuelength=`/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'`
/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'
++ /usr/sbin/postqueue -p
++ awk '{print $5}'
++ tail -n1
+ queuelength=
....
+ '[' '' -eq 100 ']'
./queue.sh: line 17: [: : integer expression expected
...
Here is the problem! As you can see $queuelength is not set at all. As I already told you in this case the if command will fail as it requires a numeric field.

So once again you should take of the output of the command

Code:
/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'
Here you are looking for the fifth field (awk '{print $5}') of the last line (tail -n1) of the command /usr/sbin/postqueue -p. It seems that this quantity is not a number. Please check that executing the command reported above (or even better just the command /usr/sbin/postqueue -p) in a terminal and be sure you are catching what you really need.

Last edited by gda; 09-02-2016 at 09:31 AM.
 
Old 09-02-2016, 09:32 AM   #14
tashman1989
LQ Newbie
 
Registered: Aug 2016
Posts: 10

Original Poster
Rep: Reputation: Disabled
dear gda kindly find below /usr/sbin/postqueue -p | tail -n1 | awk '{print $5}' result

Code:
[root@hostsvr /]# /usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'

[root@hostsvr /]#
 
Old 09-02-2016, 09:37 AM   #15
gda
Member
 
Registered: Oct 2015
Posts: 130

Rep: Reputation: 27
Quote:
Originally Posted by tashman1989 View Post
dear gda kindly find below /usr/sbin/postqueue -p | tail -n1 | awk '{print $5}' result

Code:
[root@hostsvr /]# /usr/sbin/postqueue -p | tail -n1 | awk '{print $5}'

[root@hostsvr /]#
This means that the output of the command /usr/sbin/postqueue -p is different from what you expected.

Could you please run in a terminal:

Code:
# /usr/sbin/postqueue -p
and post the output?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script error : integer expression expected linuxandtsm Linux - Newbie 7 01-07-2014 06:01 AM
[SOLVED] Error in Shell script if then - line 21: [: 0.38: integer expression expected Lucard Linux - Newbie 3 04-17-2013 11:42 AM
[: : integer expression expected if else condition Iyyappan Linux - Server 4 11-25-2012 02:09 AM
[SOLVED] if condition error: integer expression expected Kashif_Bash Programming 6 04-18-2012 05:37 AM
Error upgrading kernel, /sbin/mkinitrdi integer expression expected amp2000 Linux - General 2 12-05-2003 05:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration