LinuxQuestions.org
Review your favorite Linux distribution.
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 09-29-2011, 05:52 PM   #1
yanom
Member
 
Registered: Apr 2011
Distribution: Ubuntu
Posts: 51

Rep: Reputation: 2
Find the exit code of a program from within a shell script??


So I'm writing this shell script and i want to run this command within the shell script:
Code:
/usr/bin/zenity --question --title "Blocked: $*" --text "The file '$exe' is not marked as executable.  If this was downloaded or copied from an untrusted source, it may be dangerous to run.  For more details, read about the <a href=\"https://wiki.ubuntu.com/Security/ExecutableBit\">executable bit</a>. HOWEVER, you can choose to override the security protections and open this potentially unsafe file. Execute manual override?"
Then, I want it to do something if that command returns 0, and something else if it returns 1.

What I'm trying to do here is rewrite Ubuntu's infamous cautious-launcher so that it supports manual override. This is because some filesystems (i.e. NTFS) don't support marking things executable. Full code of what I have right now:
Code:
#!/bin/bash
# For use with .desktop files and MIME handlers so that the Ubuntu Policy
# can be followed: programs cannot be executed when they lack the execute bit.
# https://wiki.ubuntu.com/SecurityTeam/Policies#Execute-Permission%20Bit%20Required
exe="$1"
shift || true
if [ -n "$exe" ] && [ ! -x "$exe" ] && \
   [ "${exe:0:5}" != "/usr/" ] && [ "${exe:0:5}" != "/opt/" ]
then
    if [ -n "$DISPLAY" ] && [ -x /usr/bin/zenity ]; then
	if  
		/usr/bin/zenity --question --title "Blocked: $*" --text "The file '$exe' is not marked as executable.  If this was downloaded or copied from an untrusted source, it may be dangerous to run.  For more details, read about the <a href=\"https://wiki.ubuntu.com/Security/ExecutableBit\">executable bit</a>. HOWEVER, you can choose to override the security protections and open this potentially unsafe file. Execute manual override?"
	then
		echo "aborting"
	else
		exec "$@" "$exe"
	fi
    else
        echo "$*: '$exe' is not executable.  Aborting." >&2
    fi
    exit 1
fi
exec "$@" "$exe"
 
Old 09-29-2011, 06:08 PM   #2
SL00b
Member
 
Registered: Feb 2011
Location: LA, US
Distribution: SLES
Posts: 375

Rep: Reputation: 112Reputation: 112
The exit code of the most recently run program in a bash environment is stored in a special environment variable:

Code:
$?
 
Old 09-30-2011, 12:47 PM   #3
wpeckham
Member
 
Registered: Apr 2010
Location: USA
Distribution: Debian, Ubuntu, Fedora, RedHat, DSL, Puppy, CentOS, Knoppix
Posts: 831

Rep: Reputation: 181Reputation: 181
Too complicated

try something like this
Code:
if <your command goes here>
   then
      do one thing
   else
      do something else
fi
If your command is very complex, you may need to group it ( stuff ) style.
 
  


Reply

Tags
bash, exit code, script, shell script, ubuntu


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
shell script to run source code and find all its dependencies. srinathkanchan Linux - Newbie 2 08-02-2010 03:53 AM
How do i run a part of code on exit in a shell script? saiteju Programming 2 05-14-2010 06:22 AM
Find the path to the exit (C code) linuxlover1 Programming 3 12-07-2006 10:43 PM
Alias or shell script to confirm 'exit' commands from a shell rose_bud4201 Programming 2 03-08-2006 03:34 PM
How to get exit code of a progarm in Shell? neo_in_matrix Linux - Newbie 2 09-22-2005 02:46 PM


All times are GMT -5. The time now is 02:57 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