annotate bin/example/self-writing.sh @ 848:a3ee050ae568

[documentation] reference links
author Jeff Hammel <k0scist@gmail.com>
date Tue, 17 Oct 2017 12:05:19 -0700
parents aae9b0776e60
children
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`
327
59f402cd9d98 get mode dynamically
Jeff Hammel <jhammel@mozilla.com>
parents: 326
diff changeset
8 mode=`stat --format '%a' ${path}`
59f402cd9d98 get mode dynamically
Jeff Hammel <jhammel@mozilla.com>
parents: 326
diff changeset
9 tmp=`tempfile --mode ${mode}`
321
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
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
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
12
323
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
13 # sanity check
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
14 if [[ ! -w "${path}" ]]
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
15 then
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
16 echo "You don't have write permission for script ${path}"
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
17 exit 1
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
18 fi
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
19
321
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
20 # avoiding -i for safety
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
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
c64406cdcee1 nearly works
Jeff Hammel <jhammel@mozilla.com>
parents: 320
diff changeset
27
322
19b5bf66ba4e example
Jeff Hammel <jhammel@mozilla.com>
parents: 321
diff changeset
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
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
30 echo "Now: ${datestamp}"
b2924a3ae4f3 fleshing
Jeff Hammel <jhammel@mozilla.com>
parents: 322
diff changeset
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}