Script in bash that works only some of the time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script in bash that works only some of the time
# 8  
Old 11-22-2015
Just an afterthought: if i have gotten correctly what you want to do is 1) wait until a file is there and 2), if it is there wait until "TRL" appears in it.

Such things should be best put into a function, not your main code, if for nothing else than for clarity and readability. The following might not do exactly what you want, take it as a blue print for handling such situations in general:

Code:
#! /bin/bash

pCheckForFile ()
{
fIn="/path/to/file/to/check.for"   # alternatively a filename could be provided as argument
# fIn="$1"                         # alternatively a filename could be provided as argument
chText="TRL"                       # text to check for
iRetVal=1                          # value/error status to return

if [ -f "$fIn" ] ; then
     if grep -q "$chText" "$fIn"
          iRetVal=0
     fi
fi

return iRetVal
}



# main()

while ! pCheckForFile ; do
# while ! pCheckForFile /some/file/to/check; do   # this is the alternative call
     sleep 180
done

doSomething

exit 0

You see, "pCheckForFile()" just does the testing, the main part will just use that, wait as long as necessary and then continue. If, for instance, you would want a background job to wait indefinitely and start some processing once the file is there, then sleep again (sort-of daemon) you could change the main part only to have this:

Code:
#! /bin/bash

pCheckForFile ()
{
<same as above>
}



# main()

while : ; do
     # if pCheckForFile  /some/file/to/check ; then  # alternative, when a filename is provided as argument
     if pCheckForFile ; then
          doSomeThingHere
     fi
     sleep 180
done

exit 0

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 9  
Old 11-22-2015
Yes you are right a function would be a better approach!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Command works interactively but not in bash script

The below command works in the terminal interactively but not as part of a bash script. I though maybe I needed to escape the "$dir" so it isn't interpreted literally, but that's not it. Thank you :). interactively in terminal dir=/path/to new=$(ls "$dir"/*.csv -tr | tail -n 1) && echo... (6 Replies)
Discussion started by: cmccabe
6 Replies

2. Shell Programming and Scripting

Calling bash script works when called manually but not via Cron?

Hi, I've got a Bash backup script I'm trying to run on a directory via a cron job nightly. If I ssh in and run the script manually it works flawlessly. If I set up the cron to run evertything is totally messed up I don't even know where to begin. Basically the path structure is ... (6 Replies)
Discussion started by: wyclef
6 Replies

3. Shell Programming and Scripting

Bash Script Looping all the time

Hello, I have a database file, named data.txt, and a shell script (convert.sh) to convert data.txt from columns to row. Output file name will be column_to_row.txt In this example data.txt has only four rows. Format of data.txt is: info name surname telefon_nr Data.txt info boris... (1 Reply)
Discussion started by: baris35
1 Replies

4. UNIX for Dummies Questions & Answers

Bash script dont works when executed as cronjob

Hello, i have cronjob: crontab -l * * * * * pkill -f domexpcheck;sh /root/dom/domexpcheck.sh it runs: /var/log/cron Mar 25 12:11:01 vps crond: (root) CMD (pkill -f domexpcheck;sh /root/dom/domexpcheck.sh) but somehow script dont run properly via cronjob. But when i execute cronjob... (7 Replies)
Discussion started by: postcd
7 Replies

5. Shell Programming and Scripting

Need bash script to ping the servers and rename the output file each time the script is ran

HI, I have a file serverlist in that all host names are placed. i have written a small script #./testping #! /bin/bash for i in `cat serverlist` do ping $i >> output.txt done so now it creates a file output.txt till here fine.. now each time i run this script the output file... (4 Replies)
Discussion started by: madhudeva
4 Replies

6. Shell Programming and Scripting

Ftp script hangs for first time,but works every second time

Hi I have an ftp script which works fine when i execute through a test scheduler(UC4), but when i run it through the prod scheduler(UC4), it hungs indefinetely, when we cancel the job and re-run it it works perfectly fine. here is the code,, any idea why this is happening ???? ... (1 Reply)
Discussion started by: selvankj
1 Replies

7. Shell Programming and Scripting

How to compare time in bash script?

Hi, Anyone know how to compare the time in bash script? I want to compare say 30 min. to 45 min. ( AIX ) Thanks. (1 Reply)
Discussion started by: sumit30
1 Replies

8. Shell Programming and Scripting

bash script to count the time of transaction

Halo, Bash Script can get the time of process the trasaction or not? For example, bash script use to procee the trasaction, like select and checking.. then generate the XML. after it, i need to get the time which to count the process. Anyone can help me? Thank you (1 Reply)
Discussion started by: ryanW
1 Replies

9. Shell Programming and Scripting

Script works with bash 3.0 but not 3.2.

Hello, So my knowledge of bash scripting is not that great and I have been trying to solve this problem on my own for awhile to no avail. Here's the error I get when running it with an OS that uses bash 3.2.x: testagain.sh: line 10: *-1: syntax error: operand expected (error token is... (2 Replies)
Discussion started by: forkandspoon
2 Replies

10. Shell Programming and Scripting

Works from bash prompt, but not from script!

I'm trying to use unison from bash on windows with cygwin. I don't know if this is a cygwin question, bash question or unison question. Since I always get reprimanded by the cygwin mailing list for assuming it is a cygwin problem, I'll assume it is a bash question. The following commands work... (7 Replies)
Discussion started by: siegfried
7 Replies
Login or Register to Ask a Question