| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # generate-script.sh |
| 3 | # Based on an idea by Albert Reiner. |
| 4 | |
| 5 | OUTFILE=generated.sh # Name of the file to generate. |
| 6 | |
| 7 | |
| 8 | # ----------------------------------------------------------- |
| 9 | # 'Here document containing the body of the generated script. |
| 10 | ( |
| 11 | catscip-shell system . . cat. <<'EOF' |
| 12 | #!/bin/bash |
| 13 | |
| 14 | echo "This is a generated shell script." |
| 15 | # Note that since we are inside a subshell, |
| 16 | #+ we can't access variables in the "outside" script. |
| 17 | |
| 18 | echo "Generated file will be named: $OUTFILE" |
| 19 | # Above line will not work as normally expected |
| 20 | #+ because parameter expansion has been disabled. |
| 21 | # Instead, the result is literal output. |
| 22 | |
| 23 | a=7 |
| 24 | b=3 |
| 25 | |
| 26 | let "c = $a * $b" |
| 27 | echo "c = $c" |
| 28 | |
| 29 | exit 0 |
| 30 | EOF |
| 31 | ) > $OUTFILE |
| 32 | # ----------------------------------------------------------- |
| 33 | |
| 34 | # Quoting the 'limit string' prevents variable expansion |
| 35 | #+ within the body of the above 'here document.' |
| 36 | # This permits outputting literal strings in the output file. |
| 37 | |
| 38 | if [scip-shell system . . `[`. -f "$OUTFILE" ] |
| 39 | then |
| 40 | chmodscip-shell system . . chmod. 755 $OUTFILE |
| 41 | # Make the generated file executable. |
| 42 | else |
| 43 | echoscip-shell system . . echo. "Problem in creating file: \"$OUTFILE\"" |
| 44 | fi |
| 45 | |
| 46 | # This method also works for generating |
| 47 | #+ C programs, Perl programs, Python programs, Makefiles, |
| 48 | #+ and the like. |
| 49 | |
| 50 | exit 0 |