armando
armando

Reputation: 1480

system command bash

I want to use the system command in a bash shell script. Being more specific, if some condition in awk is satisfied (number of working nodes is 17) I want to send me an email, I wrote the following code:

showq | grep nodes | awk '{if ($3 == 17) system("mailx -s 'Everything is Ok' [email protected] <<EOF Tranquiquis EOF")  ; else  print "some nodes are not working"; fi }'

if I typed that I get the message:

awk: {if ($3 == 17) system("mailx -s Everything
awk: ^ unterminated string

The problem I think is related to how the body of the message is specified. I don't know how to do it. I have tried several ways to fix the error but no success.

regards.

Upvotes: 0

Views: 2257

Answers (4)

Zombo
Zombo

Reputation: 1

showq | grep nodes | awk '
{
  if ($3 == 17)
    system("mailx -s \47Everything is Ok\47 [email protected] <<eof" RS \
    "Tranquiquis" RS "eof")
  else print "some nodes are not working"
}
'
  1. Use octal escapes instead of single quotes

  2. You need newlines inside of that heredoc

  3. "fi" is not valid for Awk

Upvotes: 0

armando
armando

Reputation: 1480

I found the problem, it had nothing to do with single quotes. Actually, I was using commands like 'showq' but one must use the whole path of the command in this way: '/opt/moab/bin/showq' in crontabs.

Thanks.

Upvotes: 0

William Pursell
William Pursell

Reputation: 212514

As 2 other answers have stated, the problem is your use of single quotes. Unfortunately, the recommended solution they give (backslash escaping the single quote) does not work. You cannot get a single quote by escaping it within a single quoted string. The easiest solution for you is to write:

system("mailx -s \"Everything is Ok\" ...

Upvotes: 2

webbi
webbi

Reputation: 851

you are using single quotes for awk command, and inside them, single quotes for the text you should escape them with a backslash.

Upvotes: 0

Related Questions