How to create scripts that create another scripts
7,263
Solution 1
If you want to use echo with escaped characters like \n
you should add the -e
switch echo -e " ... "
. However it may be easier to use cat
with a here document instead
cat > stop.sh <<EOF
echo "STOPING GLASSFISH PLEASE WAIT..."
cd glassfish4/bin
chmod +x asadmin
./asadmin stop-domain
#In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict
EOF
chmod +x stop.sh
Solution 2
Removing the \n's from your code and simply doing this worked for me.
#!/bin/bash
echo "#!/bin/bash
echo 'Hello World'" > b.sh
bash b.sh
Results in output;
Hello World
Related videos on Youtube
Author by
sfrj
I am an enthusiastic young java developer, I like very much stackoverflow and i think it is a great community. Also i like blogging about software. Visit me at my blog Javing
Updated on September 18, 2022Comments
-
sfrj over 1 year
I am writing an script that needs to generate another script that will be used to shutdown an appserver...
This is how my code looks like:
echo "STEP 8: CREATE STOP SCRIPT" stopScriptContent="echo \"STOPING GLASSFISH PLEASE WAIT...\"\n cd glassfish4/bin\n chmod +x asadmin\n ./asadmin stop-domain\n #In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict\n" ${stopScriptContent} > stop.sh chmod +x stop.sh
But it is not being created correctly, this is how the output stop.sh looks like:
"STOPING GLASSFISH PLEASE WAIT..."\n cd glassfish4/bin\n chmod +x asadmin\n ./asadmin stop-domain\n #In order to work it is required that the original folder of glassfish don't contain already any #project, otherwise, there will be a conflict\n
As you see, lots of things are wrong:
- there is no echo command
- is taking the \n literaly so there is no new line
My doubts are:
- What is the correct way of making an
.sh
script create another.sh
script. - What do you thing I am doing wrong?
-
sfrj over 10 yearsI am unsure about this. Will I be able to have more stuff below?
-
Radu Rădeanu over 10 years@sfrj Yes, you will be. Just add your stuff before second
EOF
.