1

I usually download lots of images and often clean up with a cleanup.sh file, that only contains a rm -rf * command.

My query is, I want to know how many times a do it. Like say, in another file I wanna append the info that file has been executed. Is their any way?

mb47
  • 193
  • 1
  • 1
  • 11

3 Answers3

2
echo $(($(cat other.file)+1)) > other.file;
  • $(cat other.file) reads the contents of other.file
  • $((...+1)) increments it by one.
  • echo ... > other.file writes the result back to other.file

Of course, that requires you to create an initial other.file with content 0.

Siguza
  • 21,155
  • 6
  • 52
  • 89
  • its not safe to read from other.file if you redirect the output this same file. see this question: http://stackoverflow.com/questions/6696842/bash-redirect-input-from-file-back-into-same-file/6697219#6697219 – Lynch Oct 17 '15 at 14:22
  • In this case, it's OK, because the command substitution is evaluated prior t the input redirection. The problem comes with something like `cat foo.txt > foo.txt` – chepner Oct 17 '15 at 14:29
  • @Lynch That sort of problem only occurs when writing begins before reading has ended. This is not the case with `cat`. – Siguza Oct 17 '15 at 14:36
  • Thanks @Siguza that works perfectly fine. Thanks very much. – mb47 Oct 17 '15 at 14:37
  • 1
    @chepner you are correct. I tested it usin strace and its clearly show that the result of subprocess is evaluated before echo is called: `$ strace echo $(($(cat other.file)+1)) > other.file execve("/bin/echo", ["echo", "6"], [/* 74 vars */]) = 0`. my bad. – Lynch Oct 17 '15 at 15:05
1

In your cleanup.sh, you can do rm -rf * && date >> cleanup.runs.log. That way whenever you run it, the date/time is written to the log file, which you can use to count the number of times the script was executed and when.

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
1

This may work:

echo "Script was executed" >> logFile

Then you can count the lines using the wc -l

Roman Pustylnikov
  • 1,937
  • 1
  • 10
  • 18