AWK in Shell and Bash Scripts Called in Crontab

Published on Author admin


AWK in Shell and Bash Scripts Called in Crontab

In brief:
You need to put the backslash before variable:

awk '{print \$3}'

AWK is the programming language that regularly used by admins to parse data from files, stdin, stdout. A common issue to use it, especially print function in shell/bash scripts. But using it in your scripts called from cron will face you with the hidden problem.
Here is some example to explain the problem:
It is totally okay to parse, for example, ip of your server’s interface. There are several ways to find it out, but we need something real to parse.

So everything seems to be fine… but…
But this won’t work if you put this script in your cron. The reason is too simple and too complicated to debug. No problem, no mistake will occur, but the result won’t be the same with the result you’ve got from command line.
The only one thing you need to do is to put backslash before variable \$2. This will cause you a problem of error called script from the command-line, but everything will be fine with cron.
Hope, this will save your time.