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.
So, I have to somehow disable variable expansion, command substitution and backtick pipe. I thought about single quotes to disable expansion.
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.
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?
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:
The other approach is to escape any Shell special characters:
e.g.
Quite handy if you are embedding Oracle SQL.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)