Fedora Linux Support Community & Resources Center
  #1  
Old 3rd May 2007, 09:35 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
Script writing question...

I am creating a script that uses kdialog for user interaction from the GUI. Its a simple script that will backup a directory for a user to dvd. I have it doing everything i need.. However, I have one question.

Inside the script, I call xterm -e 'growisofs'

This works fine, pops an xterm, and displays the growisofs progress., then returns to the rest of my script. However, I want to be able to capture the return value of growisofs (i.e. if it errors out, I want my script to catch it and display a popup)

Problem is: when i grab the output, I am just getting the return value of the xterm, not growisofs. Anyone with ideas?
Reply With Quote
  #2  
Old 3rd May 2007, 10:14 PM
RupertPupkin Offline
Registered User
 
Join Date: Nov 2006
Location: Detroit
Posts: 5,620
You could do this:

xterm -e /bin/bash -l -c "growisofs 2>/tmp/errors"

If there were errors with growisofs, then the file /tmp/errors would have a size greater than 0 bytes (and would contain the error messages), otherwise it's size would equal 0 bytes. You could test for that like this:
Code:
if [ -s /tmp/errors ]; then
   echo "There were errors"
   # do some other stuff
fi
Reply With Quote
  #3  
Old 3rd May 2007, 10:44 PM
i2ambler Offline
Registered User
 
Join Date: Aug 2006
Posts: 20
I gave that a shot, but I really need the numeric return values of growisofs.. each number represents different types of failures.. and the point of executing the xterm is so that the user can see the 'progress meter' of growisofs. I wish I knew of a better way that using xterm.. i.e. something that could look at growisofs in the background and see its progress..
Reply With Quote
  #4  
Old 3rd May 2007, 11:11 PM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,046
Maybe:
xterm -e 'growisofs || do_your_popup'

man bash says:
? Expands to the status of the most recently executed foreground
pipeline.
So maybe somehow use $? in popup?

Last edited by lmo; 3rd May 2007 at 11:13 PM.
Reply With Quote
  #5  
Old 3rd May 2007, 11:32 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,765
Quote:
Originally Posted by lmo
Maybe:
xterm -e 'growisofs || do_your_popup'

man bash says:
? Expands to the status of the most recently executed foreground
pipeline.
So maybe somehow use $? in popup?

I believe tha will give him the return value of xterm.
No - you are going to have to exute some complex command in the xterm and pass the value back outside like (quick guess - gotta run) ...

xterm -c "bash "growisofs ; echo $? >/tmp/errcode"

Of maybe you'll have to put the compound statement "gro...code" into a shell script. The point is that xterm just drops the return value when growisofs exits. You need bash or something else above it to capture the child processess exit value & return it.
Reply With Quote
  #6  
Old 4th May 2007, 12:20 AM
lmo Offline
Registered User
 
Join Date: Mar 2007
Posts: 1,046
true && echo $?
false || echo $?

prints 0 and 1 respectively.

growisofs || do_something
will not execute do_something if growisofs succeeds (return value $? is 0)
if growisofs fails, (return value $? will be non-zero) and procession will go (|| or) to do_something

Just guessing

see what this does:
xterm -e 'fake || err=$? && read -p "$err" foo'

Last edited by lmo; 4th May 2007 at 12:50 AM.
Reply With Quote
  #7  
Old 7th May 2007, 02:38 PM
stevea Offline
Registered User
 
Join Date: Apr 2006
Location: Ohio, USA
Posts: 8,765
Quote:
Originally Posted by lmo
true && echo $?
false || echo $?

prints 0 and 1 respectively.

growisofs || do_something
will not execute do_something if growisofs succeeds (return value $? is 0)
if growisofs fails, (return value $? will be non-zero) and procession will go (|| or) to do_something
Quite true - but that's a given. Basic shell eval stuff. What's the point ?



Quote:
Originally Posted by lmo
Just guessing

see what this does:
xterm -e 'fake || err=$? && read -p "$err" foo'
It doesn't do anything good, but it is quite convoluted.

A/ fake is undefined - I assume you meant somethingt else here.
B/ If fake 'fails' (returns zero) theh the second claus is executed
typically identical to "err=1". It's pointless IMO.
C/ If the second cause executed then the third must also. So we have
read -p $err foo
which is nonsense. This produces a prompt string of "1" and reads stdin
for the value of "foo" - A complete waste IMO. There is no way to
access $foo in the context of the original shell (the one that started 'xterm').

I think a lot of the folks here must really misunderstand that a shell variable
is and what context is spans.

If you create a little script like:
Quote:
[sja]# cd /tmp
[sja]# cat >mytest
#!/bin/bash
echo "FOO = $foo"
^D
[sja]# chmod +x mytest
Note that whenever I exectute the script "./mytest" that it creates another
bash shell as a child process.

If foo is undefined, then it is undefined in the child process;
Quote:
[sja]# echo $foo

[sja]# ./mytest
FOO =
If you give foo some value locally it only has that value in the current shell process, no other provcess sees it al all.
Quote:
[sja]# foo="bar"
[sja]# echo $foo
bar
[sja]# ./mytest
FOO =
If I export the shell valiable foo, then it is passed as an argument to child processes but it is NOT seen by any processes except child processes and their children etc ... as follows:
Quote:
[sja]# export foo="baz"
[sja]# echo $foo
baz
[sja]# ./mytest
FOO = baz
====
Note too that child processes CANNOT impact the shell valiables in their parent processes at all in any way; not no way, not no how - can't be done.
Quote:
[sja]# cat >mytest2
#!/bin/bash
export BLETCH="yoyoma"
echo "BLETCH = $BLETCH"
^D[sja]# chmod +x ./mytest2
[sja]# ./mytest2
BLETCH = yoyoma

Now try
Quote:
[sja]# BLETCH="my bletch"
[sja]# ./mytest2
BLETCH = yoyoma
[sja]# echo $BLETCH
my bletch

Or:
Quote:
[sja]# export BLETCH="my bletch2"
[sja]# ./mytest2
BLETCH = yoyoma
[sja]# echo $BLETCH
my bletch2

Sorry - but a child process (like the xterm, or a child shell) cannot pass anything to it's parents via shell or envp variables. It's one-way communication of exported shell vars(and the envp) to child processes from parents.

You need to use a file or some other IPC mechanism to pass such information since the xterm will return it's own exit code, and not that of it's child back to the original parent bash script.
Reply With Quote
Reply

Tags
question, script, writing

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Needed Writing a Display Script Tekno_Cowboy Using Fedora 2 29th July 2009 09:45 AM
Help writing shell script. Tim M Using Fedora 7 23rd September 2007 05:16 PM
Shell Script writing radocool Using Fedora 7 11th December 2006 12:18 PM
Writing a script... six-eleven Using Fedora 3 23rd September 2006 03:40 AM


Current GMT-time: 14:37 (Wednesday, 20-08-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat