0

I have output that I need to export to a file. Then I need to call this file to run the script in it. Another condition would be to run the last query or instruction after 15mins. See code below:

#SORT RESULT IN DESCENDING ORDER. 
#FORMAT SYNTAX FOR BACKUP DELETION
sort -r ${TEMPRESULT2} | sed -e "s/^/DELETE BACKUP /g" | sed -e "s/$/ NOWARNING/g"

output:

DELETE BACKUP (backupid) NOWARNING

DELETE BACKUP (backupid) NOWARNING

[pause then run after 15mins]

DELETE BACKUP (finalbackupid) NOWARNING

the final backup id can only run once all other backupids are done

  • Your output files do not contain valid bash syntax, but something like SQL statements. I guess with 'running' you mean something like mysql < outputfile.sql or something? Could you add these details? – Benjamin B. Jun 5 '15 at 10:29
2

You have two different sequence of command to be executed at two different times. The most logical solution would therefore be to put these command in two different files:

TEMPRESULT3=$(sort -r ${TEMPRESULT2} | sed -e "s/^/DELETE BACKUP /g" | sed -e "s/$/ NOWARNING/g")

# Put all lines except the last one in a first script:
echo "$TEMPRESULT3" | head -n -1 >"path/to/first-script"

# Put the last line in a second script:
echo "$TEMPRESULT3" | tail -n 1 >"path/to/second-script"

As far as I know negative line number is an extension of GNU head, if it is not available you must calculate the number of lines before executing it (total=$(echo "$TEMPRESULT3" | wc -l); all_but_least=$(expr $total - 1), then $all_but_least cna be passed as parameter to the first head command instead of -1).

After that, all you have to do is call these script the way you want. For instance, if I take the mysql syntax proposed by Benjamin B. it would be:

mysql < "path/to/first-script"
# 800 s. = 15 m.
sleep 800
mysql < "path/to/second-script"
  • This won't work because $TEMPRESULT3 holds a single line, all lines joined with a space. You'll need temporary IFS modification. – yaegashi Jun 7 '15 at 4:32
  • @yaegashi: Did you tried this? What you say is true for echo $TEMPRESULT3 (without quotes, where each word composing $TEMPRESULT3 are considered as separate arguments for echo so spaces and line feeds are not preserved), not for echo "$TEMPRESULT3" (with double-quotes, where echo receives a single argument which is $TEMPRESULT3 actual value, multiple spaces and line feeds being preserved). – WhiteWinterWolf Jun 7 '15 at 10:22
  • Confirmed, you're right. In my tests I've dropped quotes and seen the result of IFS= && echo $TEMPRESULT3. Sorry for the noise... – yaegashi Jun 7 '15 at 10:39
  • @yaegashi: No problem, it is useful to highlight such details which may be easily be missed by other readers :) – WhiteWinterWolf Jun 7 '15 at 10:41
1

You can write the output into a file an execute it as shell program like below:

sort -r ${TEMPRESULT2} | sed -e "s/^/DELETE BACKUP /g" | sed -e "s/$/ NOWARNING/g" > file.sh && chmod +x file.sh && sleep 15m && ./file.sh
  • How do you pause for 15 min. before running the last line? – yaegashi Jun 5 '15 at 13:32
  • If you want to pause the script, you should use the sleep unix command, I just updated the script – Junior Joanis Jun 6 '15 at 16:47
  • If you call two times the same script, it will just execute two times all the commands therein. – WhiteWinterWolf Jun 6 '15 at 21:26
  • thanks for the responses! i separated the 1st set of backups then the last full backup so there's no conflict with the pause... ^_^ – willow Jun 8 '15 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.