0

I run the below code

iostat -x 1 2 | sed 's/,/./g' | awk '/^avg-cpu/        {c++; a=2} 
                c==2 && a && !--a {printf ("%s,%s,%s,", $1, $3, $4)} 
                c==2 && /^sda/    {printf ("%s,", $14)}
                c==2 && /^sdb/    {printf ("%s,", $14)}
                c==2 && /^nvme0n1/    {printf ("%s,",$14)}'

in order to get the values of cpu and disk i/o. What I'm now trying to do is a check if there is nvm disk or not. If there is print its util otherwise print -1.00.

I tried

if [ c==2 && /^nvme0n1/ ] then {printf ("%s,",$14)}; else {printf -1.00}'

and I'm getting

awk: cmd. line:5:                        if [ c==2 && /^nvme0n1/ ] then {printf ("%s,",$14)}; else {printf -1}
awk: cmd. line:5:                        ^ syntax error
awk: cmd. line:5:                        if [ c==2 && /^nvme0n1/ ] then {printf ("%s,",$14)}; else {printf -1}
awk: cmd. line:5:                                                ^ syntax error
awk: cmd. line:5:                        if [ c==2 && /^nvme0n1/ ] then {printf ("%s,",$14)}; else {printf -1}
awk: cmd. line:5:                                                                             ^ syntax error

and

c==2 && !/^nvme0n1/ {printf ("%s,",1.00)}'

which gives me more than one 1.00, I guess each 1.00 for each time cannot find it.

Unfortunately, I got no help from ShellCheck enter image description here

jimakos17
  • 905
  • 4
  • 14
  • 33
  • It should be `[ c==2 && /^nvme0n1/ ]` Note the space around `[` and `]`. Post your full error message. – Rahul Dec 21 '16 at 17:35
  • 1
    There are multiple problems with your code, so please run it through shellcheck.net to fix the more easily correctible ones before asking why it doesn't work. – chepner Dec 21 '16 at 17:39
  • @chepner it's not duplication because its not the spaces. – jimakos17 Dec 21 '16 at 18:04
  • Duplication was the fastest way to close a question that was both too broad and showed little research effort (since you clearly did not use shellcheck.net, as suggested in the `bash` tag wiki, before posting). – chepner Dec 21 '16 at 18:06
  • I'm sorry @chepner but I think my question is not too broad. I have a code which runs and I'm trying to add an if - else statement. Obviously, I cannot do it that's why I'm asking for help. If you don't want to help it's ok. Don't underestimate my question though. – jimakos17 Dec 21 '16 at 18:12
  • 1
    you're trying `bash` syntax inside `awk` script! Replace square brackets with regular ones. – karakfa Dec 21 '16 at 19:05
  • Is your question "how do I extract disk throughput from iostat, or `-1` if not available?" – that other guy Dec 21 '16 at 20:00

1 Answers1

1

It sounds like you're trying to get the disk throughput utilization or -1 is not available with awk.

awk is its own, separate scripting language, and you can't use any bash syntax in it.

The main issue, though, is that there is no way to determine whether or not you've found your number until you've read all lines. Once you've processed all the lines, you can determine whether to print a number or -1. You can use awk's special END block for that. This code additionally uses the special BEGIN block to set the default value:

iostat -x 1 2 | sed 's/,/./g' | awk '
            BEGIN {
              disk = -1;
            }
            /^avg-cpu/        {c++; a=2}
            c==2 && a && !--a {printf ("%s,%s,%s,", $1, $3, $4)}
            c==2 && /^sda/    { disk=$14;}
            c==2 && /^sdb/    { disk=$14; }
            c==2 && /^nvme0n1/    { disk=$14; }
            END {
              printf("%s", disk);
            }
        '
that other guy
  • 116,971
  • 11
  • 170
  • 194