views:

61

answers:

4

Hi, 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. alt text

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 greatly appreciated.

A: 

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.

Alan Haggai Alavi
+2  A: 

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)

kotlinski
Thanks a lot for the detailed explanation. but quota -q wasn't available in my system.
+3  A: 

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.

Jean
Thanks for the detailed explanation. it was very helpful
+2  A: 
var=$(quote -v | awk '$2>$4{ print "5"}')
echo "$var"
ghostdog74
Thanks, that code works perfectly. I was wondering, if the condition($2>$4) is true, I want to make the variable x=5. how can I do that? is it similar to quote -v | awk '$2>$4{ print "warning", x=5}'
i am not sure what you want to do with variable x. So i am guessing you want to return back to shell. So, assign the whole command to a variable. see edit
ghostdog74
When I use the code that you provided at home, it works fine with my ubuntu machine. but when I run it at school on banana it gives me the following error. emfile: syntax error at line 21: `var=$' unexpected. can you please tell me if there are other ways of writing this same thing. so I can get some kind of value in to var
make sure you use the appropriate shell. At home, most probably you use bash. In school, try run it with bash also.
ghostdog74
yeah, I use bash at home and this code works well with it. but for the program I am writing, my teacher specified, "Make sure that your script will run even if I happen to be using the C or Korn shell when I run it." That's why I was wondering if there some other ways of writhing the same code
`quote -v | awk '$2>$4{ print "5"}'` will be able to run since they are just tools and if you have them. The only issue here is your shell. If you have ksh, then it should be able to run as in bash. If you use C shell, maybe try this `set var=`quota -v ....``
ghostdog74