SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2015-09-23 04:29:27

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

[SOLVED] Any way to get return value from x-terminal-emulator window?

I've googled this and think it might be impossible, but just in case anyone here has an idea:

There's this script (dropbox pipemenu) from which I'd like to launch a sub-process in a terminal window. Like:

x-terminal-emulator -e sudo apt-get install package
# or
x-terminal-emulator -e bash -c 'some commands'

The thing is, the script wants to know if the commands succeeded or not but, after the window closes, x-terminal-emulator always seems to return 0.

Any thoughts?

Last edited by johnraff (2015-09-26 07:46:23)

Offline

Help fund CrunchBang, donate to the project!

#2 2015-09-23 08:28:03

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2012-03-22
Posts: 1,026
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

x-terminal-emulator -e sudo apt-get install package
# or
x-terminal-emulator -e bash -c 'some commands'

Offline

#3 2015-09-23 09:22:20

damo
#! gimpbanger
From: N51.5 W002.8 (mostly)
Registered: 2011-11-24
Posts: 5,434

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

After the child shell finishes, could the parent script check if the package is now present?

Offline

#4 2015-09-23 19:02:14

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Last edited by brontosaurusrex (2015-09-23 19:03:33)

Offline

#5 2015-09-23 19:38:26

twoion
Emerald Caffeine
From: 星界
Registered: 2012-05-11
Posts: 1,648

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

#!/bin/bash
export CATCH=/dev/shm/catch # POSIX shared memory is very useful for things like this…
x-terminal-emulator -e bash -c 'sudo apt-get install package; echo $? > $CATCH'
if [[ $(<$CATCH) -ne 0 ]]; then
  # error handling
fi

Offline

#6 2015-09-23 22:14:19

iMBeCil
WAAAT?
From: Edrychwch o'ch cwmpas
Registered: 2012-03-22
Posts: 1,026
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

^Nice thing this '/dev/shm/' aka shared memory. Never heard of this before. Excellent info twoion, thanks a lot! smile

Offline

#7 2015-09-25 08:27:05

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Thanks everybody - it looks as if an external temporary file is the only way out of this.
A lot of googling was triggered from these answers...

About /dev/shm, interesting to find that on Wheezy it's a symlink to /run/shm but on Jessie it's a self-standing directory. Web opinion seems divided 50/50 on whether it's Politically Correct to put random stuff in there, and/or whether memory caching makes the difference between /dev/shm and /tmp irrelevant in disk-write terms or not.

Exporting doesn't work for arrays. sad
But  it does work for functions. smile

Finally I might go with something like this (I might want to send some data in an array, and there is a workaround for exporting, since we're creating a temp file anyway):

john@bunsen:~$ ourfile=$(mktemp)
john@bunsen:~$ declare -A array=([one]="first element" [two]="second element")
john@bunsen:~$ declare -p array >"$ourfile"
john@bunsen:~$ export ourfile
john@bunsen:~$ stuff(){
. $ourfile
echo "${array[one]}"
echo "press a key"
read -srn1
[[ $REPLY ]] && return 1
return 0
}
john@bunsen:~$ export -f stuff
john@bunsen:~$ x-terminal-emulator -e bash -c 'stuff;echo $? > $ourfile'

** (x-terminal-emulator:18393): WARNING **: Binding '<Shift><Control>a' failed!
Unable to bind hide_window key, another instance/window has it.
# stuff from terminator - seems harmless

## another terminal opens with this:
first element
press a key
## press anything except Enter to get:

## back in the first terminal
john@bunsen:~$ cat $ourfile
1
## enter returns 0 instead

I used the same temp file to send in the array and get out the return value.
For giving the new terminal a lot of commands an exported function looks more reliable that putting the commands in a string variable.

Last edited by johnraff (2015-09-26 02:25:04)

Offline

#8 2015-09-25 08:54:56

brontosaurusrex
#! Red Menace
Registered: 2012-06-15
Posts: 1,643

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Offline

#9 2015-09-26 07:45:55

johnraff
nullglob
From: Nagoya, Japan
Registered: 2009-01-07
Posts: 4,148
Website

Re: [SOLVED] Any way to get return value from x-terminal-emulator window?

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.
Server: bleh

Debian Logo