Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

For example I want to check does py-sqlite3 available..? One of the methods would to call that command with some minimum python script and catch error?

I want to make check for any linux distro and unix system (at least bsd)

What could be the best way to achieve this?

P.s. Please provide example of shell scripting error catching, because I'm not so advanced in shell scripting

share|improve this question
    
probably better to ask stack overflow –  pablo Feb 17 '11 at 22:10

2 Answers 2

up vote 1 down vote accepted

This would be easy to do with Python.

import sys

try:
    import sqlite3
except ImportError:
    sys.exit(1)
else:
    sys.exit(0)

Exits with return code 0 if it can import it, or return code 1 if it cannot.

Andrew

share|improve this answer
    
if this would be launched in another shell script, how can I detect return code with bash script? –  holms Feb 17 '11 at 22:38
1  
bash has special variable that stores return code; try echo $? after command, and if it's 0 it was ok, if it was greater than 0 something went wrong. –  Hrvoje Špoljar Feb 17 '11 at 22:48
    
Thanks for adding that, Hrvoje. :) –  Andrew M. Feb 19 '11 at 15:02

In bash:

$ python -c 'import sqlite3' 2>/dev/null && echo "python sqlite3 modules install" || echo "python sqlite3 modules not install"
python sqlite3 modules install

$ python -c 'import sqlite3' 2>/dev/null && echo "python sqlite3 modules install" || echo "python sqlite3 modules not install"
python sqlite3 modules not install
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.