Re-write commands to make a bash script

Hi Guys,
Can someone show me how to re-write the following commands for a bash script to execute? Please take a look at the attached code snippet. I will award 500.00 points for the best answer.
Thanks

cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys
#!/bin/bash
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''
rm -f \$0
EOF

Open in new window

vePortalAsked:
Who is Participating?
 
tty2Connect With a Mentor Commented:
"cat" is not a proper command for this. "cat" outputs the content of the file, mentioned as argument.
What do you need?
If you want to write some text to the file, use "echo".

/bin/echo "#!/bin/bash" > /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''" >> /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''" >> /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "rm -f \$0" >> /etc/rc2.d/S15ssh_gen_host_keys

Pay your attention, that ">" overwrites the file, ">>" appends to the end of the file.
But your attached code snippet does the same, but looks better.
If you want to run the newly created script "/etc/rc2.d/S15ssh_gen_host_keys", you need to make it executable

chmod +x /etc/rc2.d/S15ssh_gen_host_keys

or use the command "."

. /etc/rc2.d/S15ssh_gen_host_keys
0
 
vePortalAuthor Commented:
I've come up with the following. What do you guys think?
/bin/cat "#!/bin/bash" > /etc/rc2.d/S15ssh_gen_host_keys
/bin/cat "ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''" > /etc/rc2.d/S15ssh_gen_host_keys
/bin/cat "ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''" > /etc/rc2.d/S15ssh_gen_host_keys
/bin/cat "rm -f \$0" > /etc/rc2.d/S15ssh_gen_host_keys

Open in new window

0
 
vePortalAuthor Commented:
Sorry, i miss posted. I meant echo, not cat.
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
TintinCommented:
What are you actually trying to do?

/bin/echo "#!/bin/bash" > /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''" >> /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''" >> /etc/rc2.d/S15ssh_gen_host_keys
/bin/echo "rm -f \$0" > >/etc/rc2.d/S15ssh_gen_host_keys

is the same as the original script (only more typing)

0
 
hemmiCommented:
What is actually your problem?
It is already a perfectly working script!
Rewriting may do the same job, but deteriorate the script.
0
 
vePortalAuthor Commented:
With the initial script, the commands only execute if i manually input them. I can't make a bash script out of them. With the latter commands, they actually execute in my bash script.
0
 
hemmiConnect With a Mentor Commented:
Put
#!/bin/bash
at the beginning.
Thus the script should be

#!/bin/bash
cat << EOF > /etc/rc2.d/S15ssh_gen_host_keys
#!/bin/bash
ssh-keygen -f /etc/ssh/ssh_host_rsa_key -t rsa -N ''
ssh-keygen -f /etc/ssh/ssh_host_dsa_key -t dsa -N ''
rm -f \$0
EOF

Next execute the command
chmod +x filename_of the_script_file
0
All Courses

From novice to tech pro — start learning today.