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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
tty2Commented:
"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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vePortalAuthor Commented:
Sorry, i miss posted. I meant echo, not cat.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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
hemmiCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Shell Scripting

From novice to tech pro — start learning today.