0
#!/bin/bash
#ps -aux | grep abcd > /home/test1.txt
var= grep -o -i abcd /home/test1.txt | wc -l
threshold=15
if [ $var -lt $threshold ]; then
echo "One of the service is down on $HOSTNAME" >mail.txt
mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
if [ $var -eq $threshold ]; then
echo "All services are up and running fine on $HOSTNAME" >mail.txt
mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
exit;

I am getting [: -lt: unary operator expected and [: -eq: unary operator expected when the if loops are initiated. Can anyone please help why is this happening ?

rb16
  • 129
  • 4
  • 11
  • The space after `var=` means `$var` is never assigned a value, so `[ $var -lt $threshold ]` becomes `[ -lt 15 ]`. https://www.shellcheck.net/ is your friend. And it looks like you’re missing some `$(…)`. – Biffen May 15 '18 at 12:33
  • Possible duplicate of [unary operator expected](https://stackoverflow.com/questions/13617843/unary-operator-expected) – Biffen May 15 '18 at 12:46
  • **1** The `var=` line should be `var=$(grep -o -i abcd /home/test1.txt | wc -l)`. (*No spaces around `=`!*) **2** `[ $var -lt $threshold ]` should be `[ "$var" -lt "$threshold" ]`. **3** There’s no need for a temporary file: `echo "One of the service is down on $HOSTNAME" | mailx -s "Application alert on $HOSTNAME" myname@domain.com` **4** There’s no need for a `;` after `exit`. **5** There’s no need for `exit`. – Biffen May 15 '18 at 12:49
  • 1
    Being very nit-picky here, but the space after `var= ` does not mean that `var` is never assigned a value. It is assigned the empty string as a value, but that value persists only in the environment of the `grep` command. – William Pursell May 15 '18 at 14:21
  • Is `mail.txt` used later? If not, it would be better to use a heredoc. – William Pursell May 15 '18 at 14:23
  • Thanks a lot. I did those changes and it worked like a charm. I will definitely try to do more. Oh, and mail.txt i just use it as a reference to see if the script picked the desired output. Kind of log file. Thank you all. The problem is resolved now. – rb16 May 17 '18 at 07:12
  • We have two incorrect answers and correct solutions in the comments. The suggested [duplicate](https://stackoverflow.com/questions/13617843/unary-operator-expected) covers the need to quote variable references but doesn't address the `var= grep ...` that's the main problem. I'm tempted to post an answer myself, but I'd rather defer to @Biffen. (There may be another duplicate for the `var= grep ...` problem, but I'm too lazy to search for it.) – Keith Thompson Sep 12 '21 at 22:39

2 Answers2

1

I've make some changes in your code, its running well...

#!/bin/bash
#ps -aux | grep abcd > /home/test1.txt
var=$(grep -o -i abcd /home/test1.txt | wc -l)
threshold=15
if [ "$var" -lt "$threshold" ]
    then {
        echo "One of the service is down on $(hostname)" > mail.txt
        mailx -s "Application alert on $HOSTNAME" myname@domain.com < mail.txt
    }
elif [ "$var" -eq "$threshold" ]
    then {
        echo "All services are up and running fine on $(hostname)" > mail.txt
        mailx -s "Application alert on $HOSTNAME" myname@domain.com < mail.txt
    }
    else echo "No emails to send." && exit 1
fi

obs: i not tested the mail send...

Marcelo Guedes
  • 1,419
  • 11
  • 10
  • 1
    Necessary but not sufficient. The main problem is on line 3; `var= grep` assigns the empty string to `$var`, and only for the duration of the `grep` command. The solution is discussed in comments on the question; so far nobody has posted a correct answer. – Keith Thompson Sep 12 '21 at 22:36
  • ty for your reply. i've tested the code and now, finally, it's running now. – Marcelo Guedes Sep 13 '21 at 00:52
  • the " [: -lt: unary operator expected" its caused by "if [ $var", if $var empty, the expansion will be something like "if [ -lt 15 ]", we need put between "". – Marcelo Guedes Sep 13 '21 at 01:08
  • Yes, but the fact that `$var` is empty in the first place is the result of the bug on line 3. `$var` is *supposed* to be the output of the `grep` command. – Keith Thompson Sep 13 '21 at 02:55
-1

Indent your code blocks inside the if statement:

#!/bin/bash
var= grep -o -i abcd /home/test1.txt | wc -l
threshold=15
if [ "$var" -lt "$threshold" ]; then
    echo "One of the service is down on $HOSTNAME" >mail.txt
    mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
if [ "$var" -eq "$threshold" ]; then
    echo "All services are up and running fine on $HOSTNAME" >mail.txt
    mailx -s "Application alert on $HOSTNAME" myname@domain.com <mail.txt
fi
exit;

(or delete the echos)

rb16
  • 129
  • 4
  • 11
4ndy
  • 550
  • 1
  • 9
  • 23
  • 2
    Indenting the code will not solve anything (apart from readability). You still have the error in your code that gave rise to the issue. The `var` variable is never given a value. That assignment at the top is probably supposed to be a command substitution. You also need to double quote _all_ variable expansions and command substitutions, or you will have issues with spaces, newlines etc. – Kusalananda May 15 '18 at 11:55
  • Thanks for the quick reply. I am new to scripting and i am not sure how to do a command substitution here. I have added the double quotes and now i get " [: : integer expression expected " – rb16 May 15 '18 at 12:42