You are not logged in.

#1 2012-01-05 23:44:31

buntolo
Member
Registered: 2011-10-11
Posts: 202

Bash script "open terminal and launch command

I need a script to do this:
launch a terminal, from this change working directory and then launch a command; the terminal have to remain opened after the command execution.
Something like:

$terminal cd $dir && $command

If I open a terminal and I do:

cd $dir && $command

it works


If I do a .sh script with this:

xterm -hold -e cd $dir && $command

or

terminal -H -x cd $dir && $command

when I launch it (the script) it doesn't work.

Last edited by buntolo (2012-01-05 23:44:52)

Offline

#2 2012-01-05 23:53:19

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash script "open terminal and launch command

You can run

xterm -hold -e /path/to/app

the $app being a script

[karol@black ~]$ cat app
#!/bin/bash
cd ~/test/ && ls

Last edited by karol (2012-01-05 23:55:42)

Offline

#3 2012-01-05 23:58:42

buntolo
Member
Registered: 2011-10-11
Posts: 202

Re: Bash script "open terminal and launch command

karol wrote:

You can run

xterm -hold -e /path/to/app

the $app being a script.


It doesn't work.
That application check if it has already been installed in her actual directory.
In that way the working directory (pwd) will remain the one where the script is and so the application will think it has never been installed and start to install herself again in the directory where the script resides.

EDIT:

karol wrote:

You can run

xterm -hold -e /path/to/app

the $app being a script

[karol@black ~]$ cat app
#!/bin/bash
cd ~/test/ && ls

What ls stand for?
Not the list command?

Anyway you've got me this idea:
script: it will launch another script which already is in the right directory and this last one will launch terminal with command in.

Last edited by buntolo (2012-01-06 00:02:50)

Offline

#4 2012-01-06 00:01:11

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash script "open terminal and launch command

xterm -hold -e "cd ~/test/ && ls"

works too.

I don't know what $command do you mean, but this will list th ~/test directory.

Offline

#5 2012-01-06 02:41:11

rockin turtle
Member
From: Montana, USA
Registered: 2009-10-22
Posts: 227

Re: Bash script "open terminal and launch command

Why can't you just have

cd $dir

as the first line of your script?

Offline

#6 2012-01-06 12:53:57

buntolo
Member
Registered: 2011-10-11
Posts: 202

Re: Bash script "open terminal and launch command

rockin turtle wrote:

Why can't you just have

cd $dir

as the first line of your script?

It doesn't work.
That application check if it has already been installed in her actual directory.
In that way the working directory (pwd) will change only for "cd $dir", then the terminal with the $command will open in the script directory and NOT in "cd $dir"; in this way the application will fail the check and proceed to install in the script-dir.

Offline

#7 2012-01-06 13:09:52

buntolo
Member
Registered: 2011-10-11
Posts: 202

Re: Bash script "open terminal and launch command

karol wrote:
xterm -hold -e "cd ~/test/ && ls"

works too.

I don't know what $command do you mean, but this will list th ~/test directory.


It works also for me.
With urxvt and xfce4-terminal don't work.
But xterm is too minimalist, is there an advanced terminal based on it?
And I don't unerstand why it doesn't work for xfce4-terminal but for it does for xterm...

man xfce4-terminal:

-H, --hold
Causes the terminal to be kept around after the child command has terminated

-e, --command=command
Execute command inside the terminal

man xterm:

-hold 
Turn on the hold resource, i.e., xterm will not immediately destroy its window when the shell command completes. It will wait until you use the window manager to destroy/kill the window, or if you use the menu entries that send a signal, e.g., HUP or KILL.

-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 basename 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.

What the ...?
Is only my opinion that there aren't differences between:
terminal -H -e "cd $dir && $command"
and
xterm -hold -e "cd $dir && $command"
???

Last edited by buntolo (2012-01-06 13:11:58)

Offline

#8 2012-01-06 18:36:44

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Bash script "open terminal and launch command

The man page for urxvt explains what to do for execute. hold will work with this:

-e command [arguments]
           Run the command with its command-line arguments in the urxvt window; also sets the window title and icon name to be the basename
           of the program being executed if neither -title (-T) nor -n are given on the command line. If this option is used, it must be
           the last on the command-line. If there is no -e option then the default is to run the program specified by the SHELL environment
           variable or, failing that, sh(1).

           Please note that you must specify a program with arguments. If you want to run shell commands, you have to specify the shell,
           like this:

             urxvt -e sh -c "shell commands"

Offline

#9 2012-01-06 23:05:00

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash script "open terminal and launch command

If urxvt is run w/o 'sh -c', it throws 'urxvt: unable to exec child.' error. Is Terminal printing any errors?

Offline

#10 2012-01-07 15:52:59

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Bash script "open terminal and launch command

I just tried this in XFCE's terminal and it works fine:

terminal -e ls --hold

Offline

#11 2012-01-07 15:56:43

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Bash script "open terminal and launch command

skottish wrote:

I just tried this in XFCE's terminal and it works fine:

terminal -e ls --hold

Just out of curiosity, what does the '-x' switch do?

Offline

Board footer

Powered by FluxBB