1

I have 2 hosts: one is running Linux, while the other is OmniOS.

The awk on Linux is gawk:

[root@localhost ~]# which awk
/bin/awk
[root@localhost ~]# ls -lt /bin/awk
lrwxrwxrwx. 1 root root 4 Jan 22  2014 /bin/awk -> gawk

And running the following command is OK:

[root@localhost  ~]# awk 'function print_name_and_age(name, age) { print name" is "age" old" } {print_name_and_age($1, $2)}'
Mark 12
Mark is 12 old

I think awk on OmniOS is nawk, but not sure:

root@localhost:/root# which awk
/usr/bin/awk
root@localhost:/root# ls -lt /usr/bin/awk
-r-xr-xr-x   2 root     bin        90664 Apr  3 01:17 /usr/bin/awk
root@localhost:/root# awk
awk: Usage: awk [-Fc] [-f source | 'cmds'] [files]

But executing the same command is error:

root@localhost:/root# awk 'function print_name_and_age(name, age) { print name" is "age" old" } {print_name_and_age($1, $2)}'
awk: syntax error near line 1
awk: bailing out near line 1

I can't figure out the root cause, could anyone give any clues?

Nan Xiao
  • 16,671
  • 18
  • 103
  • 164
  • 2
    If it says `bailing out` then you are using the really old broken awk, you will likely have nawk installed on the machine, use that instead or `/usr/xpg4/bin/awk`.Here is the man page for it(old awk) http://modman.unixdev.net/?sektion=1&page=awk&manpath=SunOS-4.1.3. As you will see it does not support functions. –  Apr 28 '15 at 08:26
  • @JID: Yeah, you re right! Could you give a detailed explanation as an answer? Such as why `bailing out` can infer old broken `awk`? How do you know `/usr/xpg4/bin/awk` is a `nawk`? How to know whether it is a `nawk` or old broken `awk`? Thanks! – Nan Xiao Apr 28 '15 at 08:31
  • 1
    Some `awk` respond on this command `awk --version`. It will show what `awk` you then are running. – Jotne Apr 28 '15 at 08:37
  • 1
    @Jotne I'm not sure if the SunOs one supports that. –  Apr 28 '15 at 08:39
  • 2
    @NanXiao Not really in hte mood for writing an answer sorry ! I'll answer those questions though. `bailing out` as far as i know is only used by the old awk. It will print `bailing out` for any syntax error so you could use this to check whether you are using that awk. `/usr/xpg4/bin/awk` is not `nawk`, it is just a much more featureful version of the old one. If `nawk` is available though it would be best to use that. –  Apr 28 '15 at 08:43
  • @JID: Yes, you are right! After referring `man`, I know there are `nawk`, `/usr/bin/awk`(default `awk`) and `/usr/xpg4/bin/awk` on `OmniOS`. I should always use `nawk`, thanks very much! – Nan Xiao Apr 28 '15 at 08:57
  • 5
    Any time you get the error message `awk: syntax error near line 1 awk: bailing out near line 1` it means you are using old, broken awk (/usr/bin/awk on Solaris). On Solaris you should use `/usr/xpg4/bin/awk`. You can use `nawk` if there's no other options but it's not as POSIX compliant as `/usr/xpg4/bin/awk`. – Ed Morton Apr 28 '15 at 13:19
  • 2
    The 3 are not the same. There you have a guide to compare: http://www.thegeekstuff.com/2011/06/awk-nawk-gawk And maybe a way to know which one you have installed: https://github.com/soimort/translate-shell/issues/3 – Alejandro Teixeira Muñoz May 16 '15 at 12:52

0 Answers0