Bash error: Integer expression expected

坚强是说给别人听的谎言 提交于 2019-11-27 09:49:38

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

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