LinuxQuestions.org
Help answer threads with 0 replies.
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-03-2007, 03:16 PM   #1
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 49
Bash: execute command


I am writing a script that will run a gcc command. I want the script to stop executing if the gcc command fails and output something like "script failed" and then just stop.

Is there a way in bash to execute a command and catch errors from it in an if-then-else or something?

Thanks!
 
Old 08-03-2007, 03:27 PM   #2
jailbait
Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Debian Wheezy, Debian Jessie
Posts: 7,509

Rep: Reputation: 176Reputation: 176
"Is there a way in bash to execute a command and catch errors from it in an if-then-else or something?"

Yes. Check the gcc return code within an if statement. Normal completion gives a return code of zero. An error gives a non-zero return code. You can even get fancy and take different actions based on which error code is returned.

-----------------
Steve Stites
 
Old 08-03-2007, 03:28 PM   #3
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Basically you need to check the value of exit code of gcc, using $? variable. Non-zero result means error.
 
Old 08-03-2007, 04:26 PM   #4
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Thanks! The $? was exactly what I was looking for. I googled a lot for "command execution return" and stuff but couldn't find a place that said "the return code of the command is in $?" :P

Thanks! That helped a lot.
 
Old 08-03-2007, 04:45 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,493

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
Or this simpler solution:
Code:
...
gcc ... || { echo gcc failed ; exit ; }
...

Last edited by jlliagre; 09-02-2007 at 03:02 AM.
 
Old 08-03-2007, 06:20 PM   #6
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,395
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
or
Code:
make
--- rod.
 
Old 09-01-2007, 07:54 AM   #7
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
make would require me to create a makefile. This script is supposed to compile, create an xml file, pack into a tar and move for installation. So..
 
Old 09-02-2007, 10:04 PM   #8
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
So, like - what's wrong with having the *makefile* create the XML, pack it into a tar file, and move it for installation?

I would argue that a makefile (or, in some environments, Maven or Ant) would be the *better* (simpler, more robust, more flexible) solution!

IMHO .. PSM
 
Old 10-07-2007, 08:23 AM   #9
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Original Poster
Rep: Reputation: 49
Now you have really triggered my interest. Can Makefile do all that? I am currently using CMake as the build system for my project. Is there a way to create such wonderful Makefiles via CMake?

Please enlighten me.
 
Old 10-07-2007, 10:58 AM   #10
Tischbein
Member
 
Registered: Oct 2006
Distribution: debian
Posts: 124

Rep: Reputation: 15
Ah, that signature just got me going! Here's a third way of doing it:

Code:
#!/bin/bash

cry_murder()
{
  if (($? != 0))
  then
      printf "%s\n" \
             "Would you like to take a seat and have a cup of nice" \
             "relaxing green tea.  I have some bad news for you..." \
            >&2
  fi
}

trap cry_murder 0   # Execute cry_murder on exit
set -e              # If any command returns an error value, exit

echo beg to differ | grep stuff  # No stuff, so grep returns 1.

echo That went just swell.       # Never executed

Last edited by Tischbein; 10-07-2007 at 11:00 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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] execute string as command michael_hk Linux - Newbie 2 06-26-2006 03:34 AM
remote command execute telnet with 1 command MPowers Linux - Networking 2 06-30-2005 06:31 AM
how to execute bash scripts from C????? :( cho Programming 2 06-19-2005 07:15 PM
Bash command $? failed to execute. Linh Programming 7 05-14-2004 11:11 AM
execute bash scripts vapor Programming 9 07-14-2003 02:54 PM


All times are GMT -5. The time now is 05:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration