Mercurial > hg > config
annotate bin/example/self-writing.sh @ 325:bc2d0d504270
preserve mode (could, in theory, take from script)
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sat, 08 Jun 2013 08:42:21 -0700 |
parents | ae1346f776c3 |
children | dbb6ef0c9a26 |
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` |
325
bc2d0d504270
preserve mode (could, in theory, take from script)
Jeff Hammel <jhammel@mozilla.com>
parents:
324
diff
changeset
|
8 tmp=`tempfile --mode 0755` |
321 | 9 datestamp=`date` |
10 nonce="This script regenerated at " | |
11 | |
323 | 12 # sanity check |
13 if [[ ! -w "${path}" ]] | |
14 then | |
15 echo "You don't have write permission for script ${path}" | |
16 exit 1 | |
17 fi | |
18 | |
321 | 19 # avoiding -i for safety |
20 sed 's/\(echo \"'"${nonce}"'\).*\"/\1'"${datestamp}"'\"/' ${path} > ${tmp} | |
324
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
21 if [[ ! -e "${tmp}" ]] |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
22 then |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
23 echo "Temporary file creation not successful" |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
24 exit 1 |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
25 fi |
321 | 26 |
322 | 27 # echo last and current generation times for example |
28 echo "This script last generated at (None)" | |
323 | 29 echo "Now: ${datestamp}" |
30 | |
324
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
31 # move tmpfile -> script location via exec |
ae1346f776c3
hopefully finished....we shall see!
Jeff Hammel <jhammel@mozilla.com>
parents:
323
diff
changeset
|
32 exec mv ${tmp} ${path} |