Call one script from another

Hi ,

I need to write a Unix script A which echoes commands into another script B so that we can execute B from the shell .

Please let me know how to do this

Rgds
MRA

MRAAsked:
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.

tfewsterCommented:
e.g.
#start of scriptA
various commands
.
echo "#!/usr/bin/ksh" > /tmp/scriptB
echo "first_command" >> /tmp/scriptB
.
Other commands
chmod 755 /tmp/scriptB
# end of scriptA
0
MRAAuthor Commented:
Hi tfewster,

Thanks for the quick reply.
But I need to write 100+ lines of code into 2nd script

Is there any easier way of doing it, please let me know.

Rgds
MRA
0
liddlerCommented:
Do you mean that you already have you commands in a file and you just don't want to do all of the typing?
If so, assuming your commands are in a file called comm, change tfewster's script to:
#start of scriptA
various commands
.
echo "#!/usr/bin/ksh" > /tmp/scriptB
(while read line
do
echo $line >> /tmp/scriptB
done ) < comm
.
Other commands
chmod 755 /tmp/scriptB
# end of scriptA

If that's not what you want, give us a few more details
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

HamdyHassanCommented:
liddler,
if true what you assume, it will be better to do

cp comm scriptB
vi scriptB   and add "#!/usr/bin/ksh" at the top
chmod 755 scriptB


0
liddlerCommented:
HamdyHassan - good point! maybe we don't really understand MRA wants.
0
tfewsterCommented:
Possibly scriptB will need to be "customised" by inserting variables, like writing a .netrc file for ftp to use...

MRA, if you can give us more background on what you are trying to do, we may be able to suggest an easier way.
0
TintinCommented:
Just to illustrate a point, here's a easier way to write 100+ lines of code into a 2nd script

#!/bin/sh
exec 1>newscript.sh

cat <<EOF
#!/bin/sh
#
# This is an automatically generated script
EOF

a=1
while [ $a -lt 100 ]
do
  echo "echo \"a=$a\""
  a=`expr $a + 1`
done
0
MRAAuthor Commented:
Hi All,

Thanks a lot for the response.
Sorry for the inadequate data.

The actual scenario is,
I have a set of commands in script A.
At runtime I need to create script B from A so that finally user can run script B at unix prompt after running script A.
A portion of commands from script A needs to be redirected to script B.

I used the method indicated by Tintin and it solved my problem .

Now the problem is , after

 #!/bin/sh
exec 1>newscript.sh

cat <<EOF
#!/bin/sh
#
# This is an automatically generated script
EOF

I have ,

echo " Now execute newscript.sh"

Even this echo statement is going into newscript.sh

How do I stop this redirection now???
Please let me know

Thanks again.
MRA
0
sosatanCommented:
hey,

you could simply put the 'echo "Now execute.." ' statement before you do the exec. it might work.


0
sosatanCommented:
hey,

you could simply put the 'echo "Now execute.." ' statement before you do the exec. it might work.


0
sosatanCommented:
hey,

you could simply put the 'echo "Now execute.." ' statement before you do the exec. it might work.


0
TintinCommented:
The redirection of STDOUT to a file is useful when you want all output to be redirected.

An alternative is

#!/bin/sh

cat >newscript.sh <<EOF
#!/bin/sh
#
# This is an automatically generated script
EOF

echo " Now execute newscript.sh"
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
MRAAuthor Commented:
Hi,

Thanks for all the help, I am done with the script now.
To stop redirection I used the following

exec >&2

Rgds
MRA
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
Unix OS

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.