Difference between revisions of "Converting Bash scripts to Python"

From GRASS-Wiki
Jump to navigation Jump to search
Line 40: Line 40:

In Bash:
In Bash:
<source lang=bash>
g.remove rast='map' 2>/dev/null

In Python:
In Python:

Revision as of 18:06, 11 March 2011

This page contains some notes for the users who are converting their Bash script to Python.

User break

In Bash:

# what to do in case of user break:
 g.remove rast=$TMP1 > /dev/null
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 15

In Python:

import sys
import atexit

import grass.script as grass

def cleanup():
                      rast = tmp1)

def main():
    return 0

if __name__ == "__main__":
    options, flags = grass.parser()

Discard warning/error messages

In Bash:

g.remove rast='map' 2>/dev/null

In Python:

import os
import grass.script as grass

nuldev = file(os.devnull, 'w+')
grass.run_command('g.remove', rast = 'map', stderr = nuldev)