Bash: How can a script create a script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: How can a script create a script?
# 1  
Old 03-06-2012
Question Bash: How can a script create a script?

I need to write a bash script that will generate another bash script. Furthermore, the child script is required to be hard-coded into the parent script. The parent script is not allowed to import the child script from an external file at run time. If you have a better solution than the following, please let me know.

I tried "cat" in the following manner.

cat > child.sh << EOF
The content of child script
...
...
EOF


However, if the child script contains variables, command substitutions and backtick pipes as shown below, then bash will expand them when the parent script is executed. Thus, the resultant child script will be corrupted and differ from the original child script.

Code:
cat > child.sh << EOF
#...
#...
OriginalDir=$(pwd)  #command substitution
cd $CloudDir        #variable
Array=`ls -1 *.txt` #backtick pipe
cd $OriginalDir     #variable
#...
#...
EOF


So, I have to somehow disable variable expansion, command substitution and backtick pipe. I thought about single quotes to disable expansion.

echo '.......' > child.sh
echo '.......' >> child.sh
echo '.......' >> child.sh

However, if the original child script contains single quotes, they will interfere with the outer single quotes, and hence the resultant child script will corrupt. Single quotes do not work.

In the above cat method, the expansion were interfering with $ and `. Now, my workaround is to temporarily remove $ and ` by applying percent-encoding to $ and `. That is, $ is replaced with %24, and ` is replaced with %60, (after % is replaced with %25 first) before the child script is embedded into the parent script. All the other characters are intact. Thus, the child script is prepared by sed like below.

sed "s/%/%25/g" | sed 's/\$/%24/g' | sed 's/`/%60/g'

At run time, the parent script will decode percents and restore the original version of the child script by piping the output from cat to sed in the following manner.

Code:
cat << EOF | sed 's/%60/`/g' | sed "s/%24/$/g" | sed "s/%25/%/g" > child.sh
#...
#...
OriginalDir=%24(pwd)    #command substitution
cd %24CloudDir          #variable
Array=%60ls -1 *.txt%60 #backtick pipe
cd %24OriginalDir       #variable
#...
#...
EOF


In this last method, the percent-encoding protects the variables, command substitutions and backtick pipes in the child script from expansion. However, the percent-encoding modifies the child script, even though the modification is temporary and the original version will be restored at the end. I prefer not to modify the child script at all.

In bash, how can a parent script generate a child script without the kind of preparation that modifies the child script? Does anyone know how to protect the variables, command substitutions and backtick pipes in the child script from expansion without encoding the child script? Note again that the child script is required to be hard-coded into the parent script. The parent script is not allowed to import the child script from an external file at run time. Any idea?

Many thanks in advance.
# 2  
Old 03-06-2012
Check the << bit of your bash man pages.
If you quote any part of the here document terminator it disables parameter substitution within the here document:
Code:
cat > child.sh << \EOF



The other approach is to escape any Shell special characters:
e.g.
Code:
echo \$PATH

Quite handy if you are embedding Oracle SQL.

Last edited by methyl; 03-06-2012 at 10:24 AM..
This User Gave Thanks to methyl For This Post:
# 3  
Old 03-06-2012
There's a neat little trick the shell allows you to use. Compare these 2 runs:
Code:
$ var=foo
$ cat << EOF
> echo $var
> EOF
echo foo
$ cat << 'EOF'
> echo $var
> EOF
echo $var

This User Gave Thanks to pludi For This Post:
# 4  
Old 03-06-2012
Any particular reason you want to generate a script in a script? Sometimes there's better ways, if you'll tell us the goal rather than the method you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to create an executable bash script for these commands?

I wish to create an executable bash script that will run the following commands as root, that is, using sudo su iptables-save | awk '/^ / { print $1 } /^:+ / { print $1 " ACCEPT" ; } /COMMIT/ { print $0; }' | iptables-restoreMy first attempt at bash... (9 Replies)
Discussion started by: thixeqi
9 Replies

2. Shell Programming and Scripting

Create excel file using bash script

i have written one script which generates text file which gives output like. 001.config: CR1.1 COMPILE_PASSED TEST_PASSED 002.config: CR1.1 COMPILE_FAILED TEST_FAILED . . .so on this text file will get filled one by one as its for loop for n number. i... (7 Replies)
Discussion started by: RamMore123
7 Replies

3. Homework & Coursework Questions

Create a simple bash backup script of a file

This is the problem: Write a script that will make a backup of a file giving it a ‘.bak’ extension & verify that it works. I have tried a number of different scripts that haven't worked and I haven't seen anything really concise and to the point via google. For brevity's sake this is one of the... (4 Replies)
Discussion started by: demet8
4 Replies

4. Programming

CGI Perl script to execute bash script- unable to create folder

Hi I have a bash script which takes parameters sh /tmp/gdg.sh -b BASE-NAME -n 1 -s /source/data -p /dest/data/archive -m ARC gdg.sh will scan the /source/data and will move the contents to /dest/data/archive after passing through some filters. Its working superb from bash I have... (0 Replies)
Discussion started by: rakeshkumar
0 Replies

5. Shell Programming and Scripting

Bash script to create rsa key pair

Hello all, I am setting up a cluster of Mac Pro's which need to be able to talk to a master computer, traffic between the nodes and the master needs to take place without a ssh key. I need a script that will create a security key, save it to the default place, enter the password as no password.... (2 Replies)
Discussion started by: sdl27789
2 Replies

6. Programming

Creating a bash script that create/open database

Hi. I have two text files(tables) which include some information and I want to make some query codes using them. First of all, I want to create bash script that read this two tables, create/open database and insert data from files into database. #!/bin/bash while read line; do ... (1 Reply)
Discussion started by: rlaxodus
1 Replies

7. Shell Programming and Scripting

bash script to create txt files based on timestamp

Hi , please guide me for a bash script that will create a txt files and the name of the txt files will be as of timestamp so that each file name will be different from other and these files will be get created say after every 10 minutes in a folder(/home/p2000/sxs137), please guide me how would... (1 Reply)
Discussion started by: nks342
1 Replies

8. Shell Programming and Scripting

Create mysql database with bash script - confused

Hi, i have the following: db="create database xxx;GRANT ALL PRIVILEGES ON xxx.* TO user@localhost IDENTIFIED BY 'password';FLUSH PRIVILEGES;quit;" mysql -u root -p$mysql_pass -e "$db" I don't understand why this is failing, it works fine when run from cmd but when is run in a bash script,... (1 Reply)
Discussion started by: ktm
1 Replies

9. Shell Programming and Scripting

help with a bash script to create a html table

Hi guys as the title says i need a little help i have partisally written a bash script to create a table in html so if i use ./test 3,3 i get the following output for the third arguement in the script i wish to include content that will be replace the A characters in the... (2 Replies)
Discussion started by: dunryc
2 Replies

10. Shell Programming and Scripting

I need to create a script in bash as follows:

List junk subdirectory in multi-column format Echo/Read : Ask for filename Test for existence of junk/filename True: Test for file size: Size=0: Display "filename exists, but size is zero" Size>0: Display a full listing... (3 Replies)
Discussion started by: POPOPO
3 Replies
Login or Register to Ask a Question