[GTALUG] Help with if else statement

Chris F.A. Johnson chris at cfajohnson.com
Sun Mar 29 00:32:09 UTC 2015


On Sat, 28 Mar 2015, Lennart Sorensen wrote:

> On Sat, Mar 28, 2015 at 01:44:01PM -0400, Tim Carroll wrote:
>> Sorry if this type of post does not belong here.
>> I'm a newbie to the group!
>> I would like to write a script like the following but using while loop
>> instead of for.
>> I'm hoping that a while loop with an if else in it would rerun an fsck
>> on only the drives
>> that returned "***** FILE SYSTEM WAS MODIFIED *****" not rerun it on all
>> of them!
>> The if else bit has me stumped though...
>> somewhere I need a "if [ $? -eq 0 ]; then"
>> Any help would be greatly appreciated.
>
> Would something like this work:
>
> for fs in `cat fsck-list-new.txt`; do

   It will only work if there are no spaces in the file; otherwise $fs
   will be assigned to each word in the file, not each line. This is
   almost always the wrong way to read a file. Not to mention the UUOC.

> 	fsck -M $fs || fsck -M $fs
> done
>
> That way if the first fsck returns 0 (meaning no errors found), it is
> done, and otherwise it does another run.  The -M prevents even trying
> if it is currently mounted (seems much better than your warning message).
> If the first fsck does find errors and corrects then, then it returns
> non 0 and hence another fsck is run.
>
> Not sure what you are actually trying to do.  I think you are trying to
> make something more complicated than it has to be.
>
>

-- 
Chris F.A. Johnson, <http://cfajohnson.com>


More information about the talk mailing list