开发者

How to read a value in UNIX

开发者 https://www.devze.com 2023-01-20 05:20 出处:网络
I am new to UNIX and can’t figure out how to access a certain number from a file. Following is the result I get when I type in quota -v.

I am new to UNIX and can’t figure out how to access a certain number from a file. Following is the result I get when I type in quota -v.

How to read a value in UNIX

I need to write a script to check and make sure if I am under quota, if not display a warning message. So, I am wanting to access “usage” number and “limit” number from that file and use a 'if' statement to do the comparison. Can someone please tell me how can I access data in that specific place. (Ex: 205539 in “usage” area and 200000 in “limit” area on that file). Any help would be greatl开发者_运维技巧y appreciated.


In UNIX shell it's customary to use small tools that do very small tasks, and combine them to assemble the anwser. The base system supplies you with a great variety of tools that do various basic operations on text. For example, you could run quota like this :

quota | grep undergrad1

Which would run quota, and pass its output into grep whose job is to return lines if they contain its argument. This would return only the undergrad1 line. Then you could feed that line to tools that would hack it up into columns. The prime tool for doing that is called cut. Cut takes a separator and a field specification - or alternatively, character counts. In your case, you may want to wash the output of grep before you pass it to cut, so as not to be dependant on character counts. If quota is using tabs to separate columns, then you can feed that to cut as is :

quota | grep undergrad1 | cut -d "\t" -f 3

...but if it uses a variable number of spaces, then you may want to first collapse them into one with tr and its -s option :

quota | grep undergrad1 | tr -s " " | cut -d " " -f 3

Note that we now ask cut to use space, not tab, as a separator.

With this you should be able to assemble a script that does what you want. Going further, you could grep "Filesystem" on the first line and try to find out the column index before returning them, if you expect to run on systems where the names are the same, but the order different, or whatever fits the scope of your script.


var=$(quote -v | awk '$2>$4{ print "5"}')
echo "$var"


I suggest you use quota -q instead, if available on your system:

   -q, --quiet
          Print a more  terse  message,  containing  only  information  on
          filesystems where usage is over quota.

...and then you can grep for the file system you are interested in.

Like this:

quota -q | grep undergrad4

It should be easy to check the grep return status from a script (0 = found matched line, 1 = no matched lines)


Make use of awk to get the fields at those particular columns. A simple shell script should be able to help you with the conditional.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号