LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [SOLVED] How do I run a command on a separate terminal in a bash script? (https://www.linuxquestions.org/questions/programming-9/%5Bsolved%5D-how-do-i-run-a-command-on-a-separate-terminal-in-a-bash-script-873338/)

Nathan.eth0 04-06-2011 08:47 AM

[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

vikas027 04-06-2011 10:07 AM

Quote:

Originally Posted by Nathan.eth0 (Post 4315821)
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.

theNbomr 04-06-2011 10:59 AM

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.

Nathan.eth0 04-06-2011 11:24 AM

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.

MTK358 04-06-2011 11:49 AM

Quote:

Originally Posted by Nathan.eth0 (Post 4316001)
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?

theNbomr 04-06-2011 12:24 PM

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.

Nathan.eth0 04-06-2011 12:24 PM

That's not the problem, That was just an example of what I wanted in a nutshell,
you add a

cd


up there;

vikas027 04-06-2011 12:42 PM

Quote:

Originally Posted by Nathan.eth0 (Post 4316057)
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 ?

Nathan.eth0 04-06-2011 01:29 PM

Yes I did. And No it didn't. (Thanks though :D)
Logically when I SSH to my local host, I'm using the VERY TERMINAL I'M USING to run my program.

MTK358 04-06-2011 01:37 PM

Quote:

Originally Posted by theNbomr (Post 4316056)
What exactly do you mean by 'a whole new shell console'?

Open a new terminal window.

theNbomr 04-06-2011 03:02 PM

Quote:

Originally Posted by MTK358 (Post 4316127)
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.

Ramurd 04-06-2011 03:13 PM

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. (&)

gnashley 04-07-2011 03:28 AM

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


MTK358 04-07-2011 07:52 AM

Quote:

Originally Posted by gnashley (Post 4316761)
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.

theNbomr 04-07-2011 10:40 AM

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.


All times are GMT -5. The time now is 06:11 PM.