annotate bin/example/self-writing.sh @ 323:b2924a3ae4f3

fleshing
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 08 Jun 2013 08:30:37 -0700
parents 19b5bf66ba4e
children ae1346f776c3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
320
5a0009051b13 stub for self-writing script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
1 #!/bin/bash
5a0009051b13 stub for self-writing script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
2
5a0009051b13 stub for self-writing script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 # illustrate self-writing script (example)
321
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
4 # This one does something hard and replaces dynamic data with sed.
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
5 # Other solutions (magic markers, etc) are possible
320
5a0009051b13 stub for self-writing script
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
321
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
7 path=`readlink -f $0`
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
8 tmp=`tempfile`
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
9 datestamp=`date`
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
10 nonce="This script regenerated at "
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
11
323
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
12 # sanity check
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
13 if [[ ! -w "${path}" ]]
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
14 then
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
15 echo "You don't have write permission for script ${path}"
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
16 exit 1
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
17 fi
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
18
321
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
19 # avoiding -i for safety
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
20 sed 's/\(echo \"'"${nonce}"'\).*\"/\1'"${datestamp}"'\"/' ${path} > ${tmp}
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
21
322
19b5bf66ba4e example
Jeff Hammel <jhammel@mozilla.com>
parents: 321
diff changeset
22 # echo last and current generation times for example
19b5bf66ba4e example
Jeff Hammel <jhammel@mozilla.com>
parents: 321
diff changeset
23 echo "This script last generated at (None)"
323
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
24 echo "Now: ${datestamp}"
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
25
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
26 # move tmpfile -> script location via exec