Mercurial > hg > config
annotate bin/example/self-writing.sh @ 738:6833137f039c
typo
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 21 Apr 2015 18:46:50 -0700 |
parents | aae9b0776e60 |
children |
rev | line source |
---|---|
320 | 1 #!/bin/bash |
2 | |
3 # illustrate self-writing script (example) | |
321 | 4 # This one does something hard and replaces dynamic data with sed. |
5 # Other solutions (magic markers, etc) are possible | |
320 | 6 |
321 | 7 path=`readlink -f $0` |
327 | 8 mode=`stat --format '%a' ${path}` |
9 tmp=`tempfile --mode ${mode}` | |
321 | 10 datestamp=`date` |
326
dbb6ef0c9a26
ya gotta update your nonce
Jeff Hammel <jhammel@mozilla.com>
parents:
325
diff
changeset
|
11 nonce="This script last generated at " |
321 | 12 |
323 | 13 # sanity check |
14 if [[ ! -w "${path}" ]] | |
15 then | |
16 echo "You don't have write permission for script ${path}" | |
17 exit 1 | |
18 fi | |
19 | |
321 | 20 # avoiding -i for safety |
21 sed 's/\(echo \"'"${nonce}"'\).*\"/\1'"${datestamp}"'\"/' ${path} > ${tmp} | |
324
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
22 if [[ ! -e "${tmp}" ]] |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
23 then |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
24 echo "Temporary file creation not successful" |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
25 exit 1 |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
26 fi |
321 | 27 |
322 | 28 # echo last and current generation times for example |
329
aae9b0776e60
another commit just to avoid a pointless traversal of non-tracked items
Jeff Hammel <jhammel@mozilla.com>
parents:
328
diff
changeset
|
29 echo "This script last generated at Sat Jun 8 08:52:35 PDT 2013" |
323 | 30 echo "Now: ${datestamp}" |
31 | |
324
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
32 # move tmpfile -> script location via exec |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
33 exec mv ${tmp} ${path} |