Issue with while loop?

Hi,

I have prepared a script to search for backup file information on the Linux server. Script works fine for the most part except the echo statement inside an IF conditional block displays the message ''snapshot directory not found on xxxxx" even though the .snapshot directory is found a specified path. Here is the code.

#!/bin/bash
fdate=`date "+%Y-%m-%d"`
#set -x
df -h | grep sbl| awk '{print $5}' > /home/greg/fs.out
if [ ! -s /home/greg/fs.out ]
then
 echo "No netapp filesystems found"
 exit 1
else
 echo "netapp filesystems found. continuing to the next step"
while read line
do
echo $line
if [ ! -d "${line}"/.snapshot ]
then
echo "snapshot directory not found on "$line""
else
  cd ${line}/.snapshot
  latest_bkp=`ls -l |grep -v snapmirror | tail -1|awk '{print $9}'`
  bkp_fldr_timestamp=`echo "$latest_bkp"|cut -f3 -d .|cut -f1 -d _`
        if [ ${fdate} != ${bkp_fldr_timestamp} ]
        then
                echo "Today's backup is missing for $line on `hostname`"
        else
                echo "File system backup has been taken for $line on `hostname`"
        fi
fi
done <  /home/greg/fs.out
fi
rm /home/greg/fs.out
exit 0

Here's the output when I execute the script.

[greg@sbldev01 ~]$ ./fs_bkpchk.sh
netapp filesystems found. continuing to the next step

snapshot directory not found on
/sblmnt/sb_devm2_sbl_01
File system backup has been taken for sblmnt/sb_devm2_sbl_01 on sbldev01

snapshot directory not found on
/sblmnt/sb_devm2_sbl_02
File system backup has been taken for /sblmnt/sb_devm2_sbl_02 on sbldev01

snapshot directory not found on
/sblmnt/sb_devm2_sbl_03
File system backup has been taken for /sblmnt/sb_devm2_sbl_03 on sbldev01

Any pointers will be appreciated.

Thanks,
Greg

The NetApp .snapshot is not visible in all cases.
Try

[ -d "${line}"/.snapshot/. ]

or use the exit status of cd

if cd "${line}"/.snapshot
then
   echo "backup"
else 
   echo "does not exist"
fi
1 Like

From what i've found, it seems to valid for mountpoints with spaces in any of the directory structure segments.

With this code, only the first part of a path with a space will be read, and will be missing in the tempfile all together.

df -h | grep sbl| awk '{print $5}' > /home/greg/fs.out

So, one could remove awk alltogether, but then read the file differently:

while read _ _ _ _ _ line

Specialy since the 5th variable is only the percentage used, we only name/use the 6th variable, which will include any later variable (path with spaces) as well.

Did some other small changes.
Hope this helps

#!/bin/bash
#set -x
#
#	Variables
#
	TMP=$HOME/tmp/${0##*/}-fs.out
	HOSTNAME="${HOSTNAME:-$(hostname)}"
	# This will print nothing on my system
	SEARCH="sdb" ## sbl sdb
#
#	Function
#
	check4missing() { # VALUE
	# Compares the current date versus the latest found backup dir
	# Returns false if today's date is not found
		fdate=$(date "+%Y-%m-%d")
		latest_bkp=$(ls -l |grep -v snapmirror | tail -1|awk '{print $9}')
		bkp_fldr_timestamp=$(echo "$latest_bkp"|cut -f3 -d .|cut -f1 -d _)
		if [ "${fdate}" != "${bkp_fldr_timestamp}" ]
		then	return 1
		else	return 0
		fi
	}
#
#	Action
#
	df -h | grep $SEARCH > "$TMP"
#
#	Display
#
	if [ -s "$TMP" ]
	then	# Tempfile has content
		while read _ _  _ _ line
		do 	
			this_snapshot="${line}/.snapshot"
			if [ -d "$this_snapshot" ]
			then
				cd "$this_snapshot"
				if check4missing
				then
					echo "Today's backup is missing for $this_snapshot on $HOSTNAME"
				else
					echo "File system backup has been taken for $this_snapshot on $HOSTNAME"
				fi
			else
				echo "snapshot directory not found on \"$this_snapshot\""
			fi
		done < "$TMP"
	else	# Tempfile is empty
		echo "No netapp filesystems found"
		exit 1
	fi
	rm "$TMP"
	exit 0
1 Like

Thanks sea for rewriting the code. However when I execute your code on the server, I get the following output.

snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""
snapshot directory not found on ""

I verified that the temp file has some file system entries.

One _ too many.
4 fields go to _ and the 5th should go to line

while read _ _ _ _ line

ok, now I get a different output.

snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01
snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_02/.snapshot on sbldev01
snapshot directory not found on ""
Today's backup is missing for /sblmnt/sb_devm2_sbl_03/.snapshot on sbldev01

i verified all 3 file systems listed above have .snapshot directories in them and they have today's backup files.

Removed the _ too much.
Changed the variable name on the 'not found' line.

I'm confused... my DF prints the moutpoint on the 6th, not on the 5th.. (the 5th is usage, as you see)

0 ~ $ LC_ALL=C df -h
Filesystem             Size  Used Avail Use% Mounted on
dev                    3.9G     0  3.9G   0% /dev
run                    3.9G  820K  3.9G   1% /run
....

Please change your working dir to: /sblmnt/sb_devm2_sbl_03/
And print the output of:

ls -al
df -h
[ -w .snapshot ] ; echo $?

Or even better, print the output of this as well:

bash -xv ./fs_bkpchk.sh

Here's the output I got from the above command.

[greg@sbldev01 ~]$ bash -xv ./fs.sh
#!/bin/bash
#set -x
#
#       Variables
#
        TMP=$HOME/tmp/${0##*/}-fs.out
+ TMP=/home/greg/tmp/fs.sh-fs.out
        HOSTNAME="${HOSTNAME:-$(hostname)}"
+ HOSTNAME=sbldev01
        # This will print nothing on my system
        SEARCH="sdb" ## sbl sdb
+ SEARCH=sdb
#
#       Function
#
        check4missing() { # VALUE
        # Compares the current date versus the latest found backup dir
        # Returns false if today's date is not found
                fdate=$(date "+%Y-%m-%d")
                latest_bkp=$(ls -l |grep -v snapmirror | tail -1|awk '{print $9}')
                bkp_fldr_timestamp=$(echo "$latest_bkp"|cut -f3 -d .|cut -f1 -d _)
                if [ "${fdate}" != "${bkp_fldr_timestamp}" ]
                then    return 1
                else    return 0
                fi
        }
#
#       Action
#
        df -h | grep $SEARCH > "$TMP"
+ df -h
+ grep datastage
#
#       Display
#
        if [ -s "$TMP" ]
        then    # Tempfile has content
                while read _ _  _ _ line
                do
                        this_snapshot="${line}/.snapshot"
                        if [ -d "$this_snapshot" ]
                        then
                                cd "$this_snapshot"
                                if check4missing
                                then
                                        echo "Today's backup is missing for $this_snapshot on $HOSTNAME"
                                else
                                        echo "File system backup has been taken for $this_snapshot on $HOSTNAME"
                                fi
                        else
                                echo "snapshot directory not found on \"$this_snapshot\""
                        fi
                done < "$TMP"
        else    # Tempfile is empty
                echo "No netapp filesystems found"
                exit 1
        fi
+ '[' -s /home/greg/tmp/fs.sh-fs.out ']'
+ read _ _ _ _ line
+ this_snapshot=/.snapshot
+ '[' -d /.snapshot ']'
+ echo 'snapshot directory not found on "/.snapshot"'
snapshot directory not found on "/.snapshot"
+ read _ _ _ _ line
+ this_snapshot=/sblmnt/sb_devm2_sbl_01/.snapshot
+ '[' -d /sblmnt/sb_devm2_sbl_01/.snapshot ']'
+ cd /sblmnt/sb_devm2_sbl_01/.snapshot
+ check4missing
++ date +%Y-%m-%d
+ fdate=2016-02-01
++ ls -l
++ grep -v snapmirror
++ tail -1
++ awk '{print $9}'
+ latest_bkp=h.m20.2016-02-01_0820
++ echo h.m20.2016-02-01_0820
++ cut -f3 -d .
++ cut -f1 -d _
+ bkp_fldr_timestamp=2016-02-01
+ '[' 2016-02-01 '!=' 2016-02-01 ']'
+ return 0
+ echo 'Today'\''s backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01'
Today's backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01
+ read _ _ _ _ line
+ this_snapshot=/.snapshot
+ '[' -d /.snapshot ']'
+ echo 'snapshot directory not found on "/.snapshot"'
snapshot directory not found on "/.snapshot"
+ read _ _ _ _ line
+ this_snapshot=/sblmnt/sb_devm2_sbl_02/.snapshot
+ '[' -d /sblmnt/sb_devm2_sbl_02/.snapshot ']'
+ cd /sblmnt/sb_devm2_sbl_02/.snapshot
+ check4missing
++ date +%Y-%m-%d
+ fdate=2016-02-01
++ ls -l
++ grep -v snapmirror
++ tail -1
++ awk '{print $9}'
+ latest_bkp=h.m20.2016-02-01_0820
++ echo h.m20.2016-02-01_0820
++ cut -f3 -d .
++ cut -f1 -d _
+ bkp_fldr_timestamp=2016-02-01
+ '[' 2016-02-01 '!=' 2016-02-01 ']'
+ return 0
+ echo 'Today'\''s backup is missing for /sblmnt/sb_devm2_sbl_02/.snapshot on sbldev01'
Today's backup is missing for /sblmnt/sb_devm2_sbl_02/.snapshot on sbldev01
+ read _ _ _ _ line
+ this_snapshot=/.snapshot
+ '[' -d /.snapshot ']'
+ echo 'snapshot directory not found on "/.snapshot"'
snapshot directory not found on "/.snapshot"
+ read _ _ _ _ line
+ this_snapshot=/sblmnt/sb_devm2_sbl_03/.snapshot
+ '[' -d /sblmnt/sb_devm2_sbl_03/.snapshot ']'
+ cd /sblmnt/sb_devm2_sbl_03/.snapshot
+ check4missing
++ date +%Y-%m-%d
+ fdate=2016-02-01
++ ls -l
++ grep -v snapmirror
++ tail -1
++ awk '{print $9}'
+ latest_bkp=h.m20.2016-02-01_0820
++ echo h.m20.2016-02-01_0820
++ cut -f3 -d .
++ cut -f1 -d _
+ bkp_fldr_timestamp=2016-02-01
+ '[' 2016-02-01 '!=' 2016-02-01 ']'
+ return 0
+ echo 'Today'\''s backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01'
Today's backup is missing for /sblmnt/sb_devm2_sbl_01/.snapshot on sbldev01
+ read _ _ _ _ line
#       rm "$TMP"
        exit 0
+ exit 0

Remove the red marked exclamation mark:

if [ "${fdate}" != "${bkp_fldr_timestamp}" ]

As it claims that there is no snapshot, eventhough both dates are found (date + folder), the negation of the condtion can switch the meaning.

Also, you might want to try to add the green line:

			[ -z "$line" ] && continue
			this_snapshot="${line}/.snapshot"

That will skip 'this' line, if it does not contain any data.

hth

1 Like

It's working now, thanks Sea.

df has - per the POSIX-definition - the option "-P" for exactly this: formatting the output in a consistent way across all standard-adhering platforms.

I suggest you analyze the output of df -P and use this in scripts supposed to run platform-independently.

I hope this helps.

bakunin

3 Likes

The problem here is that GNU df can format an entry on one line or on two lines.
It does that even if the output is not a terminal (I consider it a bug - a Unix df only does that if it sees a terminal).
So the better fix is df -P , that forces one line!
And | awk '{print $6}' or while read _ _ _ _ _ line . And no [ -z "$line" ] && continue is needed.