LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2007, 01:31 AM   #1
nik1984
LQ Newbie
 
Registered: Sep 2007
Posts: 7

Rep: Reputation: 0
how to execute shell within shell


I am executing following shell script which internally gives call to another shell script.

sample code
=====

#!/bin/sh
cd /nik/cli/

./kcli.sh
show service
exit

======

./kcli.sh shell script open another command line interface as shown below. i want to execute some command on it. I am not able to achieve it.

[root@localhost ~]# ./sample.sh
Newpromt>
Newpromt>


expected

[root@localhost ~]# ./sample.sh
Newpromt> show service
mt [Running] - Uptime 0 (days) 23:11:25
mgr [Running] - Uptime 0 (days) 23:11:18
mn [Running] - Uptime 0 (days) 23:05:35
command executed
[root@localhost ~]#


i tried with ./kcli.sh " show service ; exit " without any success.

any suggestions.
 
Old 09-18-2007, 04:02 AM   #2
prajakta0shimpi
LQ Newbie
 
Registered: May 2007
Posts: 16

Rep: Reputation: 0
Cool Try this.........................

Quote:
Originally Posted by nik1984 View Post
I am executing following shell script which internally gives call to another shell script.

sample code
=====

#!/bin/sh
cd /nik/cli/

./kcli.sh
show service
exit

======

./kcli.sh shell script open another command line interface as shown below. i want to execute some command on it. I am not able to achieve it.

[root@localhost ~]# ./sample.sh
Newpromt>
Newpromt>


expected

[root@localhost ~]# ./sample.sh
Newpromt> show service
mt [Running] - Uptime 0 (days) 23:11:25
mgr [Running] - Uptime 0 (days) 23:11:18
mn [Running] - Uptime 0 (days) 23:05:35
command executed
[root@localhost ~]#


i tried with ./kcli.sh " show service ; exit " without any success.

any suggestions.
If you want the script to execute in same shell do . /Kcli.sh
 
Old 09-18-2007, 07:43 AM   #3
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
try this:
( ./kcli.sh show service ; exit)
 
Old 08-28-2008, 12:52 AM   #4
nik1984
LQ Newbie
 
Registered: Sep 2007
Posts: 7

Original Poster
Rep: Reputation: 0
No its not working...some more solutions....
 
Old 08-28-2008, 01:11 AM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
It might work this way:
Code:
./kcli.sh <<%
show service
exit
%
 
Old 08-28-2008, 02:09 AM   #6
rocket357
Member
 
Registered: Mar 2007
Location: 127.0.0.1
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 187

Rep: Reputation: 74
If the script you're calling invokes the secondary shell, you have to figure out how it goes about doing that...because if it simply exec's it, there's not much you can do without changing some things.

You could possibly pass the command in as an arg and then modify kcli.sh like such:

######### example.sh ################
#!/bin/sh

./kcli.sh "some command"




######### kcli.sh ###################
# a few thoughts on how to get it working...
# by piping to the shell...
echo "$1" |/bin/bash # or whatever shell...

# or you can "abuse" su...
su --command="$1" some_user # as long as the user's shell is the target shell

# "here" document (already covered by previous post)

# various shells have various ways of executing commands in a non-interactive way...
# ksh, for example:
/bin/ksh -c "$1"



That should get you started...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to execute shell through C program pawarsac Linux - Software 2 07-22-2006 04:23 AM
Shell won't execute commands.... frank320 Red Hat 6 03-17-2006 11:12 AM
cannot execute 'cd /' shell command from PL/SQL.... samyakm Linux - Newbie 1 01-03-2005 10:10 AM
shell script won't execute brandnewbie Linux - Newbie 7 08-10-2004 01:30 PM
shell script how to execute x2000koh Programming 6 07-29-2003 04:20 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration