Bash error: Integer expression expected

限于喜欢 提交于 2019-11-26 14:51:56

问题


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 output but it also gives an error shown in the transcript. What could be the problem and how can I fix it?

First, the script:

#!/usr/bin/bash

while read A B C D E F
do
    E=`echo $E | cut -f 1 -d "%"`
    if test $# -eq 2
    then
        I=`echo $2`
    else
        I=90
    fi
    if test $E -ge $I
    then
        echo $F
    fi
done

And the transcript of running it:

$ df -k | ./filter.sh -c 50
./filter.sh: line 12: test: capacity: integer expression expected
/etc/svc/volatile
/var/run
/home/ug
/home/pg
/home/staff/t
/packages/turnin
$ _

回答1:


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}.



来源:https://stackoverflow.com/questions/30140228/bash-error-integer-expression-expected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!