LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   using subshell in another xterm (https://www.linuxquestions.org/questions/programming-9/using-subshell-in-another-xterm-831092/)

patolfo 09-08-2010 08:23 PM

using subshell in another xterm
 
Hi there guys, problem is simple, i want to run some parts of a script in different terminals. I already found about subshell, () using parenthesis to group commands and using xterm -e (function), problem is i can not get pid for the function running in the other terminal. And if I use & to set the job in background the terminal goes away as soon as it ends the job.
I am using log files, and then send it them using built in mail.

After all the parts finish i have to run the "main" script which needs the data from all the pieces.

Also other question, i usually use bash, but to load a tool i need csh, the tool is not mine so i can not change the csh usage. There is anyway of passing bash scripts/commands at the moment of changing/invoking to csh shell in the same console?

gnashley 09-09-2010 02:52 AM

(csh commands-or-file-name) or simply having bash run a csh script which has the /bin/csh shebang.

konsolebox 09-09-2010 04:41 AM

Please post the commands you already have used.

patolfo 09-10-2010 10:55 AM

trying to run commands in xterm
 
Code:

#!/bin/bash
# subshell-test.sh

function sube(){
while [ 1 ]  # Endless loop.
do
  echo "Subshell running . . ."
done
}

# Use Ctrl-C.
xterm -e sube

exit $?

If you only use: sube()
then it will start printing in the console, but if you try as I posted the xterm opens and closes, with out printing anything.

theNbomr 09-10-2010 11:38 AM

I think you've prompted me to learn something...

If you run your script in a shell, it works as expected; an endless stream of messages. However, if you run it in a subshell:
Code:

bash sube
it does not exist in the subshell. This surprises me, as I thought it would be inherited as part of the parent shell's environment. However, it does explain why it is not inherited by xterm.

--- rod.

patolfo 09-11-2010 09:37 AM

Yep kind of complicated, and i still have not figure out, how to make it run :(, any ideas ?


Quote:

Originally Posted by theNbomr (Post 4093644)
I think you've prompted me to learn something...

If you run your script in a shell, it works as expected; an endless stream of messages. However, if you run it in a subshell:
Code:

bash sube
it does not exist in the subshell. This surprises me, as I thought it would be inherited as part of the parent shell's environment. However, it does explain why it is not inherited by xterm.

--- rod.


konsolebox 09-11-2010 10:51 AM

Functions can't be used as external binaries and they are also rarely inherited. Why not just create another script named sube?

gnashley 09-11-2010 01:14 PM

Actually, you can export a function so that it is available to scripts which your script executes.
Code:

#!/bin/bash
# subshell-test.sh

function sube(){
while [ 1 ]  # Endless loop.
do
  echo "Subshell running . . ."
done
}

# Use Ctrl-C.
# xterm -e sube
# add -hold so you can see what is happening:
xterm -hold -e sube

# since we used '-hold' above, you'll have to close the above window
# before execution proceeds below

# now export the function
export -f sube
# Now you really will need CTL-C
xterm -e sube

exit $?


konsolebox 09-12-2010 02:24 AM

@gnashley so some shells also export functions in the same delivery method just like like environmental variables.. i never thought about that. I always think functions can only be transported inside the same shellspace, for example, subshells called using ().

patolfo 09-13-2010 10:04 AM

did not know about that either
 
Thanks a bunch @gnashley
I'll try it right away :)

patolfo 09-13-2010 03:37 PM

it aborts...
 
Did you run this script, i get an error when i try to run it...


Quote:

Originally Posted by gnashley (Post 4094548)
Actually, you can export a function so that it is available to scripts which your script executes.
Code:

#!/bin/bash
# subshell-test.sh

function sube(){
while [ 1 ]  # Endless loop.
do
  echo "Subshell running . . ."
done
}

# Use Ctrl-C.
# xterm -e sube
# add -hold so you can see what is happening:
xterm -hold -e sube

# since we used '-hold' above, you'll have to close the above window
# before execution proceeds below

# now export the function
export -f sube
# Now you really will need CTL-C
xterm -e sube

exit $?



gnashley 09-14-2010 01:59 AM

Yes, I did run it and it works fine here. What is the exact error you are getting?

patolfo 09-14-2010 10:22 AM

Quote:

Originally Posted by gnashley (Post 4096797)
Yes, I did run it and it works fine here. What is the exact error you are getting?

sube: Command not found.

Look like it does not import the function.

I just copy 'n' paste the code, but i still got the error above.

gnashley 09-14-2010 01:08 PM

"Distribution: Debian-Sarge" What is your shell? dash, maybe?

David the H. 09-14-2010 01:51 PM

It's my understanding that, depending on the shell, functions should be declared either by the syntax name() {..}, or by function name {..}. Mixing the two, function name() {..}, is not good syntax, although bash at least allows it.

My system is set so that dash is used to run posix scripts, and when I used #!/bin/sh in a quick test script just now, it would only accept the name() {..} form.


All times are GMT -5. The time now is 05:55 AM.