[GTALUG] Help with if else statement

William Park opengeometry at yahoo.ca
Sat Mar 28 22:19:29 UTC 2015


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!

It doesn't matter if you're using "for" or "while".  Do
    for ...; do
	if ...; then
	    ...
	else
	    ...
	fi
    done
or 
    while ...; do
	if ...; then
	    ...
	else
	    ...
	fi
    done

> The if else bit has me stumped though...
> somewhere I need a "if [ $? -eq 0 ]; then"
> Any help would be greatly appreciated.
> 
> #!/bin/bash
> #
> umount -a
> #
> df -hT
> echo -e '\n\tCAUTION!!! are all the disks unmounted!\n'
> read -p '       hit [enter] to continue or ^C to exit'
> read -p '       are you Sure? hit [enter] to continue'
> # get a list of disks attached
> ls -lh /dev/disk/by-label/ | sort -k 9 > disks-by-label-new.txt
> # clean it up with awk
> awk '$8 ~ /svr2arch/ { print $10; }' < disks-by-label-new.txt | awk -F
> '/' '{ print "/dev/"$3; }' > fsck-list-new.txt
> # this gives me a list like this "cat fsck-list-new.txt"
> /dev/sdb1
> /dev/sdc1
> /dev/sdd1
> /dev/sde1
> /dev/sde2
> /dev/sde3
> /dev/sde4
> /dev/sdf1
> /dev/sdf2
> /dev/sdf3
> /dev/sdf4
> /dev/sdg10
> /dev/sdg11
> /dev/sdg12
> /dev/sdg5
> /dev/sdg6
> # now start running the fsck on all disks
> for i in $(cat fsck-list-new.txt); do e2fsck -fvy -C 0 $i; done
> or
> while $(cat fsck-list-new.txt); do e2fsck -fvy -C 0 ; done
> 
> -- 
> 
> _______________________________________________
> 
> 
> 
> ---
> Talk Mailing List
> talk at gtalug.org
> http://gtalug.org/mailman/listinfo/talk


More information about the talk mailing list