3

I am trying to get a quick script together to check a file system prior to running resize2fs.

#!/bin/bash
var2=$(dumpe2fs -h /dev/mapper/mylv | grep "Filesystem state:")
var1=test
echo $var1
echo $var2

if [ "$var2" = "Filesystem state: clean" ];
then
        echo "clean"
else
        echo "dirty"
fi

My results

Server1:~ # ./filesystest.sh
dumpe2fs 1.38 (30-Jun-2005)
test
Filesystem state: clean
dirty

It seems even though var2 is in fact "Filesystem state: clean" it still shows up false.

7
  • Do echo "$var2" (with quotes). Perhaps there are other characters in the output, not showing (e.g. double spaces). Commented Aug 28, 2013 at 16:24
  • 2
    try running w/ #!/bin/bash -x, it'll show you the results as they are run. This will also let you see var2, it's probably not what you think. Commented Aug 28, 2013 at 16:26
  • So I just tried echo "$var2" with this output. upbcreld03:~ # ./filesystest.sh dumpe2fs 1.38 (30-Jun-2005) test Filesystem state: clean Filesystem state: clean dirty So it does look like var2 and "var2" were not the same. Commented Aug 28, 2013 at 17:01
  • @bashophil Actually, = (the POSIX-compliant syntax) is more correct than == (an extension added in bash only). Commented Aug 28, 2013 at 17:09
  • @bashophil Added the == to the script. Commented Aug 28, 2013 at 17:27

3 Answers 3

2

You probably have extra characters (maybe spaces) in var2.

Instead of saying:

if [ "$var2" = "Filesystem state: clean" ];

say:

if [[ "$var2" =~ "Filesystem state: clean" ]];

EDIT: In fact, your entire script can be written as:

dumpe2fs -h /dev/mapper/mylv | grep -q "Filesystem state: clean" && echo "clean" || echo "dirty"
Sign up to request clarification or add additional context in comments.

5 Comments

I am looking at this prior to determining if an fsck should be performed prior to resiz2fs. If its clean I see no reason for the fsck, if it is dirty or "not clean" I would run the check.
Instead of echo "dirty" in the above command (see last line), mention your fsck command. If the fs is dirty, then fsck would be performed else not. The subsequent line can be for resiz2fs.
Using foo && foo_was_true || foo_was_false as if it were if foo; then foo_was_true; else foo_was_false; fi is a bad practice, because foo_was_false can be run if foo was true but foo_was_true failed. (In the case of an echo, think of the case where stdout is a closed FD).
@devnull codeecho ****** Checking File System Status ****** var2=$(dumpe2fs -h /dev/mapper/gwvg-gwlv | grep "Filesystem state:") #var1=test #echo $var1 #echo $var2 #echo "$var2" if [[ "$var2" = "Filesystem state: clean" ]]; then echo ****** Skipping FSCK ****** echo ****** Starting Resize filesystem NO FSCK ****** resize2fs -d 1 -f -p /dev/gwvg/mylv else echo ****** Starting FSCK ****** e2fsck -f -v -y /dev/gwvg/mylv fi echo ****** Sending Email ****** mailx -s servername [email protected] @ < resize_complete echo ****** Mounting VOL1 ****** mount /VOL1
I agree with you in principle but if stdout is a closed FD, then chances are there would be bigger problems :)
1

Seeing the varying output of dumpe2fs I think you should check it like this instead:

shopt -s extglob
if [[ $var2 == 'Filesystem state:'*([[:blank:]])'clean' ]]

Or with regex:

if [[ $var2 =~ 'Filesystem state:'[[:blank:]]*'clean' ]]

Also, you could apply the command directly with this:

if dumpe2fs -h /dev/mapper/mylv 2>&1 | grep -q "Filesystem state:[[:blank:]]*clean"
then

If you want to get the state of the filesystem, you can do this:

state=$(exec dumpe2fs -h /dev/mapper/mylv 2>&1 | sed -ne '/Filesystem state:/s/.*state:\s*//p')

2 Comments

When a file system is not clean , it is defined as "not clean" so clean would leave me in an always clean state. I modified the script . #!/bin/bash var2=$(dumpe2fs -h /dev/mapper/myvl | grep "Filesystem state:") var1=test echo $var1 echo $var2 echo "$var2" if [[ "$var2" = "Filesystem state: clean" ]]; then echo "clean" else echo "dirty" fi Output : dumpe2fs 1.38 (30-Jun-2005) test Filesystem state: clean Filesystem state: clean clean
@james I see. I made changes for it.
0

Instead of this check:

if [ "$var2" = "Filesystem state: clean" ];

try this check:

if [[ "$var2" = *"Filesystem state: clean"* ]];

3 Comments

The only thing I would change about this would be using = rather than == even in the [[ ]] case; no point in picking up habits that can't be carried back to POSIX when they add no value.
@CharlesDuffy Added == to the script.
@james pardon? = is more correct; == is (slightly) wrong (because it encourages habits that don't comply with POSIX).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.