我正试图在运行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
我的结果
Server1:~ # ./filesystest.sh dumpe2fs 1.38 (30-Jun-2005) test Filesystem state: clean dirty
看来即使var2实际上是“Filesystem state:clean”,它仍然显示为false。
你可能在var2
有额外的字符(也许是空格)。
而不是说:
if [ "$var2" = "Filesystem state: clean" ];
说:
if [[ "$var2" =~ "Filesystem state: clean" ]];
编辑:其实,你的整个脚本可以写成:
dumpe2fs -h /dev/mapper/mylv | grep -q "Filesystem state: clean" && echo "clean" || echo "dirty"
看到dumpe2fs不同的输出我想你应该这样检查:
shopt -s extglob if [[ $var2 == 'Filesystem state:'*([[:blank:]])'clean' ]]
或者用正则表达式:
if [[ $var2 =~ 'Filesystem state:'[[:blank:]]*'clean' ]]
另外,你可以直接使用这个命令:
if dumpe2fs -h /dev/mapper/mylv 2>&1 | grep -q "Filesystem state:[[:blank:]]*clean" then
如果你想获得文件系统的状态,你可以这样做:
state=$(exec dumpe2fs -h /dev/mapper/mylv 2>&1 | sed -ne '/Filesystem state:/s/.*state:\s*//p')
而不是这个检查:
if [ "$var2" = "Filesystem state: clean" ];
尝试这个检查:
if [[ "$var2" = *"Filesystem state: clean"* ]];