Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Call one script from another

Posted on 2003-03-05
15
Medium Priority
?
262 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
  • 3
  • 3
  • 2
  • +3
13 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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…
This tech tip describes how to install the Solaris Operating System from a tape backup that was created using the Solaris flash archive utility. I have used this procedure on the Solaris 8 and 9 OS, and it shoudl also work well on the Solaris 10 rel…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses

577 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