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`
|
|
8 tmp=`tempfile`
|
|
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}
|
|
21
|
322
|
22 # echo last and current generation times for example
|
|
23 echo "This script last generated at (None)"
|
323
|
24 echo "Now: ${datestamp}"
|
|
25
|
|
26 # move tmpfile -> script location via exec |