
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. #!/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 -- _______________________________________________

On Sat, 28 Mar 2015, Tim Carroll wrote: ...
# 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
while IFS= read -r line do : whatever done < fsck-list-new.txt -- Chris F.A. Johnson, <http://cfajohnson.com>

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@gtalug.org http://gtalug.org/mailman/listinfo/talk

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 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. -- Len Sorensen

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>
participants (4)
-
Chris F.A. Johnson
-
Lennart Sorensen
-
Tim Carroll
-
William Park