[SOLVED] keep the bash-sript running if "exec command" fails
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
#!/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
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.
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).
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.