Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #94804

Re: How to re-write this bash script in Python?

References <b07086dc-0946-4398-b621-23d564d54507@googlegroups.com>
Date 2015-07-31 17:47 +1000
Subject Re: How to re-write this bash script in Python?
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.1110.1438328836.3674.python-list@python.org> (permalink)

Show all headers | View raw


On Fri, Jul 31, 2015 at 4:31 AM,  <sutanu.das@gmail.com> wrote:
> #!/bin/bash
>
> _maillist='pager@email.com'
> _hname=`hostname`
> _logdir=/hadoop/logs
> _dirlog=${_logdir}/directory_check.log
>
> _year=$(date -d "-5 hour" +%Y)
> _month=$(date -d "-5 hour" +%m)
> _day=$(date -d "-5 hour" +%d)
> _hour=$(date -d "-5 hour" +%H)
>
> _hdfsdir=`hdfs dfs -ls -d /hadoop/flume_ingest_*/$_year/$_month | awk '{print $8}'`
>
> echo "Checking for HDFS directories:" > ${_dirlog}
> echo >> ${_dirlog}
>
> for _currdir in $_hdfsdir
> do
> hdfs dfs -ls -d $_currdir/$_day/$_hour &>> ${_dirlog}
> done
>
> if [[ `grep -i "No such file or directory" ${_dirlog}` ]];
> then
> echo "Verify Flume is working for all  servers" | mailx -s "HDFS Hadoop Failure on Flume: ${_hname}" -a ${_dirlog} ${_maillist}
> fi
> --
> https://mail.python.org/mailman/listinfo/python-list

There are two basic approaches to this kind of job.

1) Go through every line of bash code and translate it into equivalent
Python code. You should then have a Python script which blindly and
naively accomplishes the same goal by the same method.

2) Start by describing what you want to accomplish, and then implement
that in Python, using algorithmic notes from the bash code.

The second option seems like a lot more work, but long-term it often
isn't, because you end up with better code. For example, bash lacks
decent timezone support, so I can well believe random832's guess that
your five-hour offset is a simulation of that; but Python can do much
better work with timezones, so you can get that actually correct.
Also, file handling, searching, and text manipulation and so on can
usually be done more efficiently and readably in Python directly than
by piping things through grep and awk.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

How to re-write this bash script in Python? sutanu.das@gmail.com - 2015-07-30 11:31 -0700
  Re: How to re-write this bash script in Python? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-07-30 20:36 +0100
  Re: How to re-write this bash script in Python? random832@fastmail.us - 2015-07-30 16:17 -0400
  Re: How to re-write this bash script in Python? Chris Angelico <rosuav@gmail.com> - 2015-07-31 17:47 +1000
    Re: How to re-write this bash script in Python? Grant Edwards <invalid@invalid.invalid> - 2015-07-31 14:26 +0000
      Re: How to re-write this bash script in Python? Chris Angelico <rosuav@gmail.com> - 2015-08-01 00:53 +1000

csiph-web