LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 04-06-2011, 08:47 AM   #1
Nathan.eth0
LQ Newbie
 
Registered: Apr 2011
Posts: 21

Rep: Reputation: 2
Smile [SOLVED] How do I run a command on a separate terminal in a bash script?


Hello World!

I'm trying to do something here::
I'm writing a bash script, I want to [open a new terminal and run a bash command in it] inside the script.

I tried to use this, but apparently I get syntax errors.
Quote:
#! /bin/bash
echo "echo Something" > ~/Second.sh
chmod +x ~/Second.sh
xterm -e sh -c ./Second.sh

Last edited by Nathan.eth0; 04-07-2011 at 12:03 PM.
 
Old 04-06-2011, 10:07 AM   #2
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Nathan.eth0 View Post
Hello World!

I'm trying to do something here::
I'm writing a bash script, I want to [open a new terminal and run a bash command in it] inside the script.

I tried to use this, but apparently I get syntax errors.
I am unable to figure out why do you want to open a new terminal of the same machine you are working on.

Still if you are trying to do so, an alternative is to run your script "Second.sh" through ssh.

You can do something like:
Code:
#! /bin/bash
echo "echo Something" > ~/Second.sh
chmod +x ~/Second.sh
dir=`pwd`
ssh localhost "cd $dir; ./Second.sh"
Athough this would ask for password which can be automated through expect.
 
Old 04-06-2011, 10:59 AM   #3
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
The script gets created as '~/Something.sh' but executed as './Something.sh', which would only be correct if the current working directory is '~/' I see no syntax error, however the script is so short that it closes the window almost immediately. If you do something more like
Code:
echo "echo Something; sleep 3" > ~/Second.sh
it seems to do what would I expect.

--- rod.
 
1 members found this post helpful.
Old 04-06-2011, 11:24 AM   #4
Nathan.eth0
LQ Newbie
 
Registered: Apr 2011
Posts: 21

Original Poster
Rep: Reputation: 2
That was not what I wanted to do, I want my script to open a whole new shell console and start running my second script on that console. Like the main script is a root which triggers several sub-scripts to start running, each on a new shell console, Separately, and my main all my main terminal does would be creating the sub-scripts not running them.
 
Old 04-06-2011, 11:49 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by Nathan.eth0 View Post
That was not what I wanted to do, I want my script to open a whole new shell console and start running my second script on that console. Like the main script is a root which triggers several sub-scripts to start running, each on a new shell console, Separately, and my main all my main terminal does would be creating the sub-scripts not running them.
Did you read theNbomr's post?

You're creating ~/Second.sh but executing ./Second.sh. What if the working directory is not $HOME?
 
Old 04-06-2011, 12:24 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
What exactly do you mean by 'a whole new shell console'? When you make the changes I previously suggested, what does it do or not do that you see as incorrect?

--- rod.
 
Old 04-06-2011, 12:24 PM   #7
Nathan.eth0
LQ Newbie
 
Registered: Apr 2011
Posts: 21

Original Poster
Rep: Reputation: 2
That's not the problem, That was just an example of what I wanted in a nutshell,
you add a

cd


up there;
 
Old 04-06-2011, 12:42 PM   #8
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Ubuntu, Debian, OS X
Posts: 1,305

Rep: Reputation: 107Reputation: 107
Quote:
Originally Posted by Nathan.eth0 View Post
That's not the problem, That was just an example of what I wanted in a nutshell,
you add a

cd


up there;
Did you tried my code ?
Does it solve your problem ?
 
Old 04-06-2011, 01:29 PM   #9
Nathan.eth0
LQ Newbie
 
Registered: Apr 2011
Posts: 21

Original Poster
Rep: Reputation: 2
Yes I did. And No it didn't. (Thanks though )
Logically when I SSH to my local host, I'm using the VERY TERMINAL I'M USING to run my program.
 
Old 04-06-2011, 01:37 PM   #10
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by theNbomr View Post
What exactly do you mean by 'a whole new shell console'?
Open a new terminal window.
 
Old 04-06-2011, 03:02 PM   #11
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Quote:
Originally Posted by MTK358 View Post
Open a new terminal window.
And that's what I would have thought, except that he says that using an xterm isn't getting him there.

--- rod.
 
Old 04-06-2011, 03:13 PM   #12
Ramurd
Member
 
Registered: Mar 2009
Location: Rotterdam, the Netherlands
Distribution: Slackwarelinux
Posts: 703

Rep: Reputation: 111Reputation: 111
Code:
echo 'echo "test";sleep 3' > testscript.sh; chmod u+x testscript.sh; xterm -e sh -c ./testscript.sh
just confirming what others said before: above code works and opens an xterm nicely; If you want to run more xterms concurrently from the same script, you should run them in the background of course. (&)
 
1 members found this post helpful.
Old 04-07-2011, 03:28 AM   #13
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
Since your script is writing a script, you also need to write the shebang to the script:
Code:
#! /bin/bash
echo '#!/bin/bash' > ~/Second.sh
echo "echo Something" >> ~/Second.sh
chmod +x ~/Second.sh
xterm -e sh -c ~/Second.sh
 
Old 04-07-2011, 07:52 AM   #14
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Angry

Quote:
Originally Posted by gnashley View Post
Since your script is writing a script, you also need to write the shebang to the script:
No.

xterm is told what to interpret it with in the -e option. If you don't specify it, it assumes the shell.

EDIT: looking at the xterm man page, I found a problem! it says:

Quote:
-e program [ arguments ... ]
This option specifies the program (and its command line arguments) to be run in
the xterm window. It also sets the window title and icon name to be the base‐
name of the program being executed if neither -T nor -n are given on the command
line. This must be the last option on the command line.

Last edited by MTK358; 04-07-2011 at 07:55 AM.
 
Old 04-07-2011, 10:40 AM   #15
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
It is the last argument. Everything following the '-e' is considered part of that argument (at least in the examples here). Although there are elements within it that look like option arguments, they are for the command which will be launched by xterm, and are considered to be lumped together as part of the '-e' argument to xterm. I usually enclose the whole thing in quotes to enforce this, although evidently, this is not required.

--- rod.
 
  


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
sshfs in bash script - won't run in called terminal? basd Linux - General 3 03-17-2012 05:10 PM
[SOLVED] Partial list with ls-l in bash script run in cron but full list run from command line redgshost Linux - General 29 01-16-2011 12:14 PM
Bash script run via cron not executing MYSQL command mackstar Linux - Server 4 04-23-2009 05:01 AM
Bash script - to save and run a command at the end satimis Programming 15 11-02-2004 08:53 AM
How to run a bash command in the background from perl script professorfrink Programming 3 11-13-2003 03:02 PM

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

All times are GMT -5. The time now is 09:26 AM.

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