Bash error: Integer expression expected

后端 未结 1 705
一个人的身影
一个人的身影 2020-12-02 03:01

In the sections below, you\'ll see the shell script I am trying to run on a UNIX machine, along with a transcript.

When I run this program, it gives the expected out

相关标签:
1条回答
  • 2020-12-02 03:23

    Before the line that says:

    if test $E -ge $I
    

    temporarily place the line:

    echo "[$E]"
    

    and you'll find something very much non-numeric, and that's because the output of df -k looks like this:

    Filesystem     1K-blocks      Used Available Use% Mounted on
    /dev/sdb1      954316620 212723892 693109608  24% /
    udev               10240         0     10240   0% /dev
    : :
    

    The offending line there is the first, which will have its fifth field Use% turned into Use, which is definitely not an integer.

    A quick fix may be to change your usage to something like:

    df -k | sed -n '2,$p' | ./filter -c 50
    

    or:

    df -k | tail -n+2 | ./filter -c 50
    

    Either of those extra filters (sed or tail) will print only from line 2 onwards.


    If you're open to not needing a special script at all, you could probably just get away with something like:

    df -k | awk -vlimit=40 '$5+0>=limit&&NR>1{print $5" "$6}'
    

    The way it works is to only operate on lines where both:

    • the fifth field, converted to a number, is at least equal to the limit passed in with -v; and
    • the record number (line) is two or greater.

    Then it simply outputs the relevant information for those matching lines.

    This particular example outputs the file system and usage (as a percentage like 42%) but, if you just want the file system as per your script, just change the print to output $6 on its own: {print $6}.

    Alternatively, if you do the percentage but without the %, you can use the same method I used in the conditional: {print $5+0" "$6}.

    0 讨论(0)
提交回复
热议问题