Python Conditional Statements Based on Success of Last Command


 
Thread Tools Search this Thread
Top Forums Programming Python Conditional Statements Based on Success of Last Command
# 1  
Old 10-24-2013
Python Conditional Statements Based on Success of Last Command

In shell scripting, I can create a conditional statement based on the success or failure (exit status)of a command such as:

Code:
pinger()
{
ping -c 2 $remote_host >/dev/null 2>&1
ping_stat=$?
}

pinger 
if [[ $ping_stat -eq 0 ]]; then

	echo "blahblahblah" 
  exit 0
fi

how is this done using Python using exit status?
# 2  
Old 10-24-2013
The return value of os.system("shell command"); is the return code of whatever was ran. You'd check for success by checking if it was zero.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-24-2013
Be aware!
Code:
Last login: Thu Oct 24 23:06:01 on ttys000
AMIGA:barrywalker~> python3.3
Python 3.3.2 (v3.3.2:d047928ae3f6, May 13 2013, 13:52:24) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> retcode=os.system("claer")
sh: claer: command not found
>>> type(retcode)
<class 'int'>
>>> print(retcode)
32512
>>> retcode=os.system("ls /tmp")
0001d5269d380		launch-Tz0C4e		launchd-1575.z2293S
launch-KKNp3U		launch-hwShRg		text
launch-MSWbKw		launchd-124.mdpfPn
>>> type(retcode)
<class 'int'>
>>> print(retcode)
0
>>> _

This User Gave Thanks to wisecracker For This Post:
# 4  
Old 10-24-2013
Hi, wisecracker.

From the python os.system documentation:
Code:
On Unix, the return value is the exit status of the process encoded in the format specified for wait().

You should read the man page for the wait(2) system call. The integer returned, (32512 above) encodes more information than just the value passed to the exit system call. There are macros that are used to extract the actual value passed to exit(). Python's os module provides access to those macros. The two most relevant to your post:
Code:
os.WIFEXITED(status)

    Return True if the process exited using the exit(2) system call, otherwise return False.

    Availability: Unix.

os.WEXITSTATUS(status)

    If WIFEXITED(status) is true, return the integer parameter to the exit(2) system call.
    Otherwise, the return value is meaningless.

    Availability: Unix.

Those excerpts are from Python Standard Library - 16.1 - os.

To summarize, you cannot directly read the integer returned by wait(2) (which is the return value of os.system on UNIX). Also, you should not use bit shifting and/or masking to extract the relevant status bits because the encoding is allowed to differ between system implementations.

Regards,
Alister

Last edited by alister; 10-24-2013 at 09:15 PM..
This User Gave Thanks to alister For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional deletion of files based on extension

Hello All, I have some files like file, file.chk, file.sem and file.temp in huge. I would like to delete some files based on following criteria. 1. Unconditionally delete .sem and .temp files 2. If we found the actual file, don't remove .chk file, otherwise remove .chk file as well for... (5 Replies)
Discussion started by: VasuKukkapalli
5 Replies

2. Shell Programming and Scripting

Executes scripts parallelly based on their success

Hi Team , I have one Master.sh file which call X,Y,Z scripts , but here X may call again some sub scripts X_sub1.sh , X_sub2.sh Y calls Y_sub1.sh,Y_sub2.sh and similarly Z script also . Now requirement is Both X and Y should execute parallel bcz X and Y are independent... (9 Replies)
Discussion started by: chandini
9 Replies

3. UNIX for Dummies Questions & Answers

Check Success Status Of sed command

How do i check success status of a sed command execution i have the below script not sure if it is right approach to check status of execution using a function. Also it looks like in the below sed command even if the search string doesn't exist in the file it is returning status as success as i... (6 Replies)
Discussion started by: Ariean
6 Replies

4. UNIX for Dummies Questions & Answers

Conditional execution of statements

Hi , I have a script which has multiple awk and sed commands like below. First one :- find /root/src/ -name "*csv" -size 0 -exec rm {} \; Second one: - ls *SRE*.txt > SRT_TRAN.dat rm *asr*.txt Third one :- find /root/src/ -name '*.csv' | while read FILENAME ; do awk... (2 Replies)
Discussion started by: shruthidwh
2 Replies

5. Shell Programming and Scripting

How to use awk or nawk by using Conditional Statements

Hi Guys! Anybody know how can I use a nawk or awk on a script and printing the NAME, SECTION (must be 410 or 411 or 414) and TOTAL COST of CLASS 1 and 3 combined must be greater than 50. See below desired output file. input.txt: NAME,CLASS,COST,SECTION JOHN,1,10,410 JOHN,2,20,410... (2 Replies)
Discussion started by: pinpe
2 Replies

6. Shell Programming and Scripting

Python script - control flow statements

Hi guys.I'm just beginner of python. I'm just trying to do some analysis on simple input file. it has 6 columns and i want to consider k,l and m,n if i and j are + after that checking which value is greater or lower in k,l and m,n I have included logic header just to explain what I was... (4 Replies)
Discussion started by: repinementer
4 Replies

7. Programming

Conditional Compilation based on Environmental Variable in Unix

I want to be able to access an environment variable to control how a program is compiled. So: export MY_VERSN=9 Then ideally, within my C++ code, I would have #if MY_VERSN = 9 iret = FRED9() #else iret = FRED() #endif The way I thought I could do it is that in the script that... (2 Replies)
Discussion started by: BrighterLater
2 Replies

8. Shell Programming and Scripting

for i loop with conditional statements?

New to scripting in general, so patience plz. If I ask a stupid question or don't get it, I thank you for your kindness in advance. That said, did a for i loops checks to see if a PB* file is there but I need to know two things before I copy the file. I need to know if the file's create date... (2 Replies)
Discussion started by: xgringo
2 Replies

9. UNIX for Dummies Questions & Answers

Success status of mailx command

Hi I want to know if the email address in the mailx exists or not Eg: Mailx -s "Subj" hello@ab.com How do I know if the email address is a valid one??? (4 Replies)
Discussion started by: superprogrammer
4 Replies

10. Shell Programming and Scripting

Conditional Statements

How can I compare two decimal values within a function using Bash? Function fun2 isn't comparing the decimal values. Is there a way to do this using Bash or Korn? #!/bin/bash set -x x=1 z=110 function fun1() { i=`bc << EOF 2>> /dev/null scale=3 ... (1 Reply)
Discussion started by: cstovall
1 Replies
Login or Register to Ask a Question