Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
I'm trying to write a shell script which parses the output of the Cyrus IMAP's quota command, which spits out quota usage information for each user. The idea is that if a user is over quota, certain actions will be taken by the script.
The output is space separated (in contrast to commas or tabs). Each row in the output constitutes a user.
Now, I can pull the values (quota usage, user name) I want out of each row with gawk with no problems. The problem I'm having is finding a way to iterate through the output, line-by-line (so that each line is an interation in the loop), and I'd prefer not saving temp files in this process.
I suppose I could write a perl script to do all of this, but given the situation (long story), I'd rather do it with a shell script.
I run this command:
su cyrus -c /usr/lib/cyrus/bin/quota
It spits formatted data like this:
Quota % Used Used Root
102400 0 0 email@example.com
102400 24 333 firstname.lastname@example.org
262144 84 220504 email@example.com
102400 104 3 firstname.lastname@example.org
I want a loop that will give me each row as a string per indice.
i.e. indice 1, the string would contain " 102400 0 0 email@example.com". I can then process this line as needed.
I guess the other part of the question is, how do I get the info into the loop without saving it to a file? Do I do VAR=`cyrus_cmd_above` with the tick marks, and then somehow use that VAR in the loop? Or do I pipe the actual command in somehow?
I need very specific instructions. Help is greatly appreciated.
You could do it that way as well. You left out the "read" command after while in your generalized description but I think I caught the meaning.
I will use the other form quite often at the end of cp and mv commands with the -i (interactive option):
< <(yes n)
This will automatically respond "n" to any question.
You can use this form "<( command statements )" to use the output of a command in place of a filename.
I will use this form to do things like sort the input file. For example, the "comm" command can provide common entries or unique entries in two files. It is required that the files be sorted.
So if file1 & file2 are already sorted, then you could use "comm -23 file1 file2" to print out items unique to file1. If they aren't presorted, you could use: "comm -23 <(sort file1) <(sort file2)".
This allows you to use the output of 2 programs as input to the command that expects 2 filenames.
You could use something like "sort file2 | comm -23 file1 -" if only file2 were unsorted, but the < <(command) form allows you to do this with both files. Plus the < <(...) is located at the same place that a file would be in the arguments making it readable once you learn what it is doing.
For bash, the best is the Advanced Bash Scripting Guide on the www.tldp.org website. ( The pdf file is named "abs-guide.pdf") It consists almost entirely of examples that you can try yourself, which is the best way to learn. Don't let the word "advanced" scare you. The work "Thorough" would be a better description. If there is something that you don't understand in the "info bash" manual, it will probably be explained with examples in the "abs-guide.pdf". Not having enough examples is the problem I have with a lot of documentation. After covering command substitution, there are 15 links to other scripts in the Guide.
One of the first chapters deals with special characters. You can look there to find out what && does, or <<.