Issue with while loop?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue with while loop?
# 8  
Old 02-01-2016
Quote:
Or even better, print the output of this as well:
Code:
bash -xv ./fs_bkpchk.sh

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

Code:
[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

# 9  
Old 02-01-2016
Remove the red marked exclamation mark:
Code:
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:
Code:
			[ -z "$line" ] && continue
			this_snapshot="${line}/.snapshot"

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

hth

Last edited by sea; 02-03-2016 at 10:37 AM..
This User Gave Thanks to sea For This Post:
# 10  
Old 02-02-2016
It's working now, thanks Sea.
# 11  
Old 02-02-2016
Quote:
Originally Posted by sea
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)
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
These 3 Users Gave Thanks to bakunin For This Post:
# 12  
Old 02-03-2016
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Issue when doing a loop

Hi, I just have started learning shell scripting (sh). Why do i only get the date? while read dt do echo "Date : ${dt} sed -n '/${dt}/,/${dt}/p' file1.log | grep -w ERROR done < date1.dat INPUT - date1.dat 2019-04-05 04:58:25 2019-04-05 04:58:26 2019-04-05 05:00:56... (3 Replies)
Discussion started by: margel
3 Replies

2. Shell Programming and Scripting

Issue with for loop

Hi Team, I have for loop in my shell script. Which basically loop through all files in the directory, When some files are in the directory it works just fine. But if there are no files at all..still the for loop try to execute. Please help. Below is the code. #!/bin/ksh echo "Program... (5 Replies)
Discussion started by: bharath561989
5 Replies

3. Shell Programming and Scripting

While Loop issue

Hi, i=0 t5=6000001 while do i=`expr $i + 1` t5=`expr $t5 + 1` echo $t5 done I am able to increment "col3" value but unable to get col1,col2 value. Input: t1=10001 t2=abc t3=ghkc (5 Replies)
Discussion started by: onesuri
5 Replies

4. Shell Programming and Scripting

Issue with using While loop

Hi, I am trying to move a file from remote server to local server and when the transfer completes successfully i call a script in remote server to remove the file which was successfully transferred. I do this by first getting the list of file in remote server and move the text file to local... (8 Replies)
Discussion started by: funonnet
8 Replies

5. Shell Programming and Scripting

for-while loop issue

sup experts..i had a script which was bugging me..was hoping someone could point out the issue here Input file: space separated 2 columns I wanted to print out the 2 columns after assigning them to variables ( bascially the same output but iterate through line by line ). The code worked... (7 Replies)
Discussion started by: foal_newbie
7 Replies

6. Shell Programming and Scripting

until loop issue.

Hi, my script is waiting for 3 files to come to a folder for 30 min but even when all the files arrive in the folder it's still waiting for these three files. Files can come with in 2 min and I want it to start processing them immediately after all the files arrive in the folder. until ; do... (3 Replies)
Discussion started by: gurpartap
3 Replies

7. Shell Programming and Scripting

loop issue

function ext { echo "THANKS & WELCOME BACK" } function upc { echo "TO EXPORT UPROC GIVE UPROC NAME PER LINE IN THE input.txt and PRESS Y" echo "TO GO BACK PRESS 99" read parm0 if ; then start elif ; then for i in `cat input.txt` ; do echo $i $UXEXE/uxext upr upr=$i... (0 Replies)
Discussion started by: kojo
0 Replies

8. Shell Programming and Scripting

loop issue

I have 2 files one of them has all the all mac addresses and the other one has all the ip addresses. Basically, I want to loop thru those 2 files and generate a configuration like below: host www184.domain.com { hardware ethernet 00:13:72:3B:B4:3A; fixed-address 192.168.0.184; }... (4 Replies)
Discussion started by: kkkk
4 Replies

9. Shell Programming and Scripting

while loop issue

Hi, Following is my code and the file FILE_LIST_EXCESS.txt has 40 file names in it while read LineIn do echo ${LineIn} `ftp -vin << END_INPUT >> ${PID}_DS_GET_Log.log 2>&1 open servername user userid password cd FileDir get ${LineIn} END_INPUT`... (4 Replies)
Discussion started by: mgirinath
4 Replies

10. Shell Programming and Scripting

Help With A For Loop Issue

I was wondering how I can modify this for loop, so it only loops through the filenames that do not have an ".old" extension. for filename in $(ls "$1") do echo $filename | grep '\.old$' > /dev/null if then mv $1/$filename $1/$filename.old fi done (5 Replies)
Discussion started by: ralts01
5 Replies
Login or Register to Ask a Question