Link to home
Start Free TrialLog in
Avatar of MRA
MRA

asked on

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

Avatar of tfewster
tfewster
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of MRA
MRA

ASKER

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
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
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


HamdyHassan - good point! maybe we don't really understand MRA wants.
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.
Avatar of Tintin
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
Avatar of MRA

ASKER

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
hey,

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


hey,

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


hey,

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


ASKER CERTIFIED SOLUTION
Avatar of Tintin
Tintin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of MRA

ASKER

Hi,

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

exec >&2

Rgds
MRA