?
Solved

Call one script from another

Posted on 2003-03-05
15
Medium Priority
?
261 Views
Last Modified: 2010-04-21
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

0
Comment
Question by:MRA
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
15 Comments
 
LVL 21

Expert Comment

by:tfewster
ID: 8071742
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
 

Author Comment

by:MRA
ID: 8071775
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
 
LVL 18

Expert Comment

by:liddler
ID: 8072215
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 9

Expert Comment

by:HamdyHassan
ID: 8073108
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
 
LVL 18

Expert Comment

by:liddler
ID: 8073284
HamdyHassan - good point! maybe we don't really understand MRA wants.
0
 
LVL 21

Expert Comment

by:tfewster
ID: 8074045
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
 
LVL 48

Expert Comment

by:Tintin
ID: 8075114
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
 

Author Comment

by:MRA
ID: 8078905
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
 
LVL 1

Expert Comment

by:sosatan
ID: 8082817
hey,

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


0
 
LVL 1

Expert Comment

by:sosatan
ID: 8082949
hey,

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


0
 
LVL 1

Expert Comment

by:sosatan
ID: 8082950
hey,

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


0
 
LVL 48

Accepted Solution

by:
Tintin earned 100 total points
ID: 8084881
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
 

Author Comment

by:MRA
ID: 8126532
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

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question