LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-13-2010, 11:14 PM   #1
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Rep: Reputation: Disabled
keep the bash-sript running if "exec command" fails


Is there a way to use exec, but if exec fails to go on with the script?
Example:
Code:
#!/usr/bin/env bash

exec startx
echo "Starting of X failed"
If startx fails, the echo will be seen on the screen.
I tried all kind of stuff, but guess it ain't of much use to post it here. I searched the web, but searching for "exec and bash" in one sentence does give results which are not what i am looking for.

Thanks
 
Old 08-14-2010, 12:13 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
exec only fails if startx cannot be executed. startx takes over bash once it already happens so even the statement after exec will no longer be there. I think you don't have a choice but put it in a condition block.
Code:
if ! startx; then
    echo "Starting of X failed"
fi
Code:
startx || echo "Starting of X failed"
Code:
startx || {
    echo "Starting of X failed"
    ....
}
 
Old 08-14-2010, 12:52 AM   #3
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
That is my problem.
If i do:
Code:
#!/usr/bin/env bash

if ! startx; then
    echo "startx did fail"
fi
startx fails and "startx did fail" is echoed on the screen.

But if i do.
Code:
#!/usr/bin/env bash

if ! exec startx; then
    echo "startx did fail"
fi
then nothing is echoed on the screen.

Is it possible to make the code go on in case "exec command" does fail.
If it should work out of box, what might i be doing wrong ( i tried:
#!/bin/bash
#!/usr/bin/env bash
#!/bin/sh
and i tried with ./call_startx with "bash call_startx" and with "sh call_startx"
thanks
 
Old 08-14-2010, 12:56 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
As i've said the shell (its process) that's parsing the commands is already taken over by startx so there's no way you can do that.

For you to understand more, see this: http://linux.die.net/man/3/exec. The shell's exec command is just based on that.

May I ask... Why do you really have to use exec? Why not just use a normal call? Or do you want the script to immediately continue after calling startx? Maybe you need

Code:
startx &
?

Last edited by konsolebox; 08-14-2010 at 12:59 AM.
 
1 members found this post helpful.
Old 08-14-2010, 01:15 AM   #5
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
Ok. My main question was if it is possible at all. In a thread i have read it would be, i tried for hours and hours and didn't get it running. If it doesn't work then its clear why it didn't work.

The only drawback of using a simple "startx" i can see is that the script would be running during the whole X-session.

The problem behind it is kinda like this:
On a live-CD if startx fails use Xorg -configure and try to use that xorg.conf . If startx fails again, use xdebconfigurator and use that one. If startx fails again use a stock xorg-vesa file. Kinda like that. If i could explain it better i would. Sorry for that.
I took my chance to try to figure out about exec (and do a bit of if-else-training).

Thanks a lot.
 
Old 08-14-2010, 01:26 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Don't you mean something like this?
Code:
if ! startx; then
    <use xdebconfiguartor>

    if ! startx; then
        <do something to use a stock xorg-vesa file>

        if ! startx; then
            ... and so on
        fi
    fi
fi
 
Old 08-14-2010, 01:33 AM   #7
j1alu
Member
 
Registered: Apr 2009
Distribution: debian gnu/linux
Posts: 798

Original Poster
Rep: Reputation: Disabled
Yes, something like that.
( I have thought about it for a while. Somehow i thought a good solution might be to use either a while-loop or (later) a for loop. )
But, to make it short: yes, something like that. Hammer on it until i am able to run X.


But my main question for this thread was how exactly "exec" does work (in case it suceeds and in case it fails). That made me lots of problems. Thanks again for making it clear.
With startx (without exec) solving the problem works (with the methods i am able to use, which are only a few).
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash "exec" command won't Skaperen Linux - Software 4 03-17-2010 03:08 PM
1st script attempt; cygwin; fails "bash: myscript : command not found" engineerd1 Linux - Newbie 8 11-08-2009 04:03 PM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 10:18 PM
PHP exec() fails "cannot open display" pppp Linux - Software 1 12-04-2006 02:46 PM
Bash Script: Problem running variable command containing "" Paasan Programming 2 01-21-2004 01:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration