LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-09-2020, 11:50 PM   #1
subir
LQ Newbie
 
Registered: Nov 2019
Posts: 22

Rep: Reputation: Disabled
shell script to check availability of a file for 1 hour , after one hour if file not there , it will echo "Time Out"


i wrote this code. but the problem is after every 300s it prints "Time over", when $fname present is got out from the loop. I want "time over" after 1hr if file not arrived, but after 300s it starts to print "time over"

#!/bin/bash
echo "enter file name"
read fname
START=`date +%s`
while [ $(( $(date +%s) - 3600 )) -lt $START ]; do
if [ -e $fname ]
then
echo "$fname present"
break
sleep 300
else
echo "Time Over"
fi
done

Last edited by subir; 03-09-2020 at 11:54 PM.
 
Old 03-10-2020, 02:00 AM   #2
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 17,782

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
This basically does what you want
Code:
file=time.t

now=$(date +%s)
end=$((now + 10))

found=0
while [[ $now -le $end ]]
do
	if [[ -e $file ]]
	then
		found=1
		echo "file found"
		break
	else
		sleep 3
	fi
	now=$(date +%s)
done

if [[ $found -eq 0 ]]
then
	echo "Not found"
fi
although you'll need to customise it a bit.
 
Old 03-10-2020, 02:01 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,853

Rep: Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880Reputation: 4880
would be nice to use code tags, otherwise hard to read and understand your script. This looks much better:
Code:
#!/bin/bash
echo "enter file name"
read fname
START=`date +%s`
while [ $(( $(date +%s) - 3600 )) -lt $START ]; do
    if [ -e $fname ]
    then
        echo "$fname present"
        break
        sleep 300
    else
        echo "Time Over"
    fi
done
I hope you will now better understand what's happening.
 
Old 03-10-2020, 02:22 AM   #4
subir
LQ Newbie
 
Registered: Nov 2019
Posts: 22

Original Poster
Rep: Reputation: Disabled
OMG. Thank you very much ...
 
Old 03-10-2020, 07:05 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,420

Rep: Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635Reputation: 635
If you really must have the echo in the loop, then a break must follow it:
Code:
#!/bin/bash
echo "enter file name"
read fname
END=$(( $(date +%s) + 3600 ))
while :
do
  if [ $(date +%s) -ge $END ]
  then
    echo "Time Over"
    break
  fi
  if [ -e "$fname" ]
  then
    echo "$fname present"
    break
  fi
  sleep 300
done
Unless the while loop is forced into a sub shell (e.g. by a pipe into it or from it),
it is better to query a variable after the loop:
Code:
#!/bin/bash
echo "enter file name"
read fname
END=$(( $(date +%s) + 3600 ))
found=""
while [ $(date +%s) -lt $END ]; do
  if [ -e "$fname" ]
  then
    echo "$fname present"
    found=1
    break
  fi
  sleep 300
done
if [ -z "$found" ]
then
  echo "Time Over"
fi
 
  


Reply

Tags
scripting, shell script, while loop


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Zorin OS 12: change 24 hour time to 12 hour time display ghoracek Zorin OS 5 05-04-2019 12:59 AM
[SOLVED] Ping fails to a particular IP-address: "Echo Request" received but no "Echo Reply" sundialsvcs Linux - Networking 0 01-19-2017 06:44 PM
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
BASH: How to NOT echo to screen with "if echo $x | grep ".*"; then" eur0dad Programming 9 07-27-2006 02:14 PM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:27 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration