How to write a bash script like the python? Lloyd Huang. KaLUG - Kaohsiung Linux User Group COSCUP Aug 18 2012



Similar documents
How to extend Puppet using Ruby

Computational Mathematics with Python

DevKey Documentation. Release 0.1. Colm O Connor

Computational Mathematics with Python

Pen Test Tips 2. Shell vs. Terminal

Intro to scientific programming (with Python) Pietro Berkes, Brandeis University

Computational Mathematics with Python

Linux Shell Script To Monitor Ftp Server Connection

Network Security In Linux: Scanning and Hacking

Shellshock. Oz Elisyan & Maxim Zavodchik

Advanced Bash Scripting. Joshua Malone

Hadoop Hands-On Exercises

Bash shell programming Part II Control statements

Automatic Script Generation Based on User-System Interactions

Advanced Topics: Biopython

Profiling, debugging and testing with Python. Jonathan Bollback, Georg Rieckh and Jose Guzman

SGE Roll: Users Guide. Version Edition

Epics_On_RPi Documentation

RHEL Clients to AD Integrating RHEL clients to Active Directory

24 ottobre 2013.openerp.it XML-RPC vs Psycopg2 Dr. Piero Cecchi. OpenERP Analisi Prestazionale Python script XML-RPC vs Psycopg2

PostgreSQL administration using Puppet. Miguel Di Ciurcio Filho

Introduction. dnotify

TOPICS IN COMPUTER SECURITY

Objects and classes. Objects and classes. Jarkko Toivonen (CS Department) Programming in Python 1

Scientific Programming, Analysis, and Visualization with Python. Mteor 227 Fall 2015

CIS 551 / TCOM 401 Computer and Network Security

Programmation Systèmes Cours 4 Runtime user management

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Extreme computing lab exercises Session one

Rake Task Management Essentials

An Introduction to High Performance Computing in the Department

Job Scheduler Daemon Configuration Guide

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

INASP: Effective Network Management Workshops

mitmproxy.org How MITMproxy has been slaying SSL Dragons OWASP The OWASP Foundation

Intel Do-It-Yourself Challenge Lab 2: Intel Galileo s Linux side Nicolas Vailliet

Troubleshooting Mac OS X Server Tips and tricks

About This Document 3. Integration and Automation Capabilities 4. Command-Line Interface (CLI) 8. API RPC Protocol 9.

SELinux Policy Editor RBAC(Role Based Access Control) guide (for Ver 2.0))

Windmill. Automated Testing for Web Applications

VDCF - Virtual Datacenter Control Framework for the Solaris TM Operating System

Command Line Crash Course For Unix

Use Cases for Docker in Enterprise Linux Environment CloudOpen North America, 2014 Linda Wang Sr. Software Engineering Manager Red Hat, Inc.

Penetration Testing Ninjitsu 2: Crouching Netcat, Hidden Vulnerabilities. By Ed Skoudis

B&K Precision 1785B, 1786B, 1787B, 1788 Power supply Python Library

Submitting batch jobs Slurm on ecgate. Xavi Abellan User Support Section

Systems Programming & Scripting

Writing MySQL Scripts With Python's DB-API Interface

How to Set Up pgagent for Postgres Plus. A Postgres Evaluation Quick Tutorial From EnterpriseDB

Automating Linux Malware Analysis Using Limon Sandbox Monnappa K A monnappa22@gmail.com

General Overview. Slurm Training15. Alfred Gil & Jordi Blasco (HPCNow!)

netflow-indexer Documentation

Penetration Testing Report Client: Business Solutions June 15 th 2015

Tor Exit Node Block Scripts

Integrating Lustre with User Security Administration. LAD 15 // Chris Gouge // 2015 Sep

Windows Security and Directory Services for UNIX using Centrify DirectControl

Introduction to Python

Introduction to Scientific Computing

Subversion Integration

Using the Radmind Command Line Tools to. Maintain Multiple Mac OS X Machines

Web Hosting: Pipeline Program Technical Self Study Guide

Creating an LDAP Directory

Outline. Unix shells Bourne-again Shell (bash) Interacting with bash Basic scripting References

Grid Engine Users Guide p1 Edition

Chapter 7: Unix Security. Chapter 7: 1

Module Google Rich Snippets + Product Ratings and Reviews

Wind River. Intelligent Device Platform XT EMS Profile EMS DEVICE MANAGEMENT USER'S GUIDE WIND RIVER 1.0

How to build secure Apache Tomcat deployments with RPM.

Extending Remote Desktop for Large Installations. Distributed Package Installs

mruby extension module for monitoring system Takanori Suzuki

Using Single Sign-on with Samba. Appendices. Glossary. Using Single Sign-on with Samba. SonicOS Enhanced

GR A universal framework for visualization applications

Extreme computing lab exercises Session one

Simulation Tools. Python for MATLAB Users I. Claus Führer. Automn Claus Führer Simulation Tools Automn / 65

Open source framework for interactive data exploration in server based architecture

CS 2112 Lab: Version Control

Advanced Tornado TWENTYONE Advanced Tornado Accessing MySQL from Python LAB

A Dude probing SNMP! Building custom probes and configuring equipment using SNMP with The Dude. Andrea Coppini AIR Wireless - Malta andrea@air.com.

CS 1133, LAB 2: FUNCTIONS AND TESTING

Continuous Integration In challenging environments w/ Ansible. PyCon5 Italy, Cesare Placanica

Hadoop Hands-On Exercises

Module Google Rich Snippets + Product Ratings and Reviews

Module Title: Advanced Systems Administration 2

Cluster synchronization with Csync 2

Module Google Rich Snippets + Product Ratings and Reviews

Introduction to Linux (Authentication Systems, User Accounts, LDAP and NIS) Süha TUNA Res. Assist.

SLURM Workload Manager

Lecture 4: Writing shell scripts

Beginners Shell Scripting for Batch Jobs

Monitoring a Linux Mail Server

AUTOMATIC EVALUATION OF COMPUTER PROGRAMS USING MOODLE S VIRTUAL PROGRAMMING LAB (VPL) PLUG- IN

The Puppet Show Managing Servers with Puppet

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

Transcription:

How to write a bash script like the python? Lloyd Huang KaLUG - Kaohsiung Linux User Group COSCUP Aug 18 2012

Before the start Before the start About Bash Python and me. The ipython and lpython.py. A trick, but it touches my heart. Examples of lpython.py : cat /etc/passwd lpython.py \ 'for i in stdin:\n s=i.split(":"); print s[0],s[5],s[6],' uname -a lpython.py 'print stdin.read().split()[0:3]' 2

Bash: Questions and answers Bash: Questions and answers Q: How to be more logic and reuse? A: Function. Q: How to do unit testing? A: Function. Q: How different is between Bash function and Python module? A:... 3

Bash source VS Python import Bash source VS Python import Python A module allows you to logically organize your Python code. Grouping related code into a module makes the code easier to understand and use. Python modules are easy to create; they're just files of Python program code. if name == ' main ': 4

Bash source VS Python import How Bash do it? 5

Bash source VS Python import #!/bin/bash #!/usr/bin/python funtest() { def funtest(): echo "" print "" } funfake() { def funfake(): echo "func for fake" print "func for fake" } # main if name == ' main ': funtest funtest() funfake funfake() $> source samp00.sh $> python >>> import samp00 func for fake $> bash samp00.sh $> python samp00.py func for fake func for fake 6

Bash source VS Python import #!/bin/bash #!/usr/bin/python funtest() { def funtest(): echo "" print "" } funfake() { def funfake(): echo "func for fake" print "func for fake" } return 2> /dev/null if name == ' main ': funtest funtest() funfake funfake() $> source samp00.sh $> python >>> import samp00 $> bash samp00.sh $> python samp00.py func for fake func for fake 7

Bash source VS Python import #!/bin/bash funtest() { echo "" } funfake() { echo "func for fake" } if [ "$0" == "$BASH_SOURCE" ]; then funtest funfake fi 8

From xkcd http://xkcd.com/353/

Python module and docstring Python module and docstring import, from import module as someone Import module del Delete object docstring Python Documentation Strings 10

Python module and docstring #!/usr/bin/python def funtest(): """ Here is python docstring for funtest. Here is python docstring for funtest. """ print "" def funfake(): "Here is python docstring for funcfake." print "func for fake" if name == ' main ': funtest() funfake() 11

Python module and docstring $> python >>> import samp02 >>> samp02. samp02. doc ( samp02.funtest( samp02.funfake( >>> samp02.funtest() >>> help (samp02.funtest) >>> Help on function funtest in module samp02: funtest() Here is python docstring for funtest. Here is python docstring for funtest. 12

Python module and docstring >>> samp02.funfake. doc 'Here is python docstring for funcfake.' >>> print samp02.funfake. doc Here is python docstring for funcfake. >>> import samp02 as new_samp02 >>> new_samp02.funtest() >>> del new_samp02 >>> new_samp02.funtest() Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'new_samp' is not defined 13

Bash import and docstring? Bash import and docstring? Usage: source import.sh import, from import module as someone Import module del module Delete module help module.function Documentation Strings 14

Bash import and docstring? #!/bin/bash funtest() { doc =" Here is bash import.sh docstring for funtest Here is bash import.sh docstring for funtest " echo "" } funfake() { doc ="Here is bash import.sh docstring for funfake" echo "func for fake" } if [ "$0" == "$BASH_SOURCE" ]; then funtest funfake fi 15

Bash import and docstring? $> source import.sh $> import samp02.sh $> samp02 samp02.funfake samp02.funtest $> samp02.funtest $> import samp02.sh as new_samp $> new_samp.funtest $> help new_samp.funtest Here is bash import.sh docstring for funtest Here is bash import.sh docstring for funtest $> del new_samp $> new_samp.funtest bash: new_samp.funtest command not found 16

uid, gid = split(``1000:100'',``:'')? uid, gid = split(``1000:100'',``:'')? Python : split name, passwd, uid, gid, note, homedir, shell = string.split("www-data:x:33:33:www-data:/var/www:/bin/sh",":") Bash : read LDread () { doc ="LDread '.' 'V01.01.05.28.KH0.10.28' c1 c2 c3 c4 c5 c6 c7" } local ifs con ifs="$1"; shift con="$1"; shift IFS="$ifs" read $@ < <(echo $con) 17

阿 宅 圖 片 來 源 : 公 立 怪 咖 國 民 小 學 http://blog.xuite.net/protion/school/9142122

Hook Hook #!/bin/bash funtest() { doc ="Hook test - Part I PreHook=\"echo This is PreHook\" PostHook=\"echo This is PostHook\"" } test -n "$PreHook" && $PreHook echo "" test -n "$PostHook" && $PostHook 19

Hook $> import hook00.sh $> hook00.funtest $> help hook00.funtest Hook test - Part I PreHook="echo This is PreHook" PostHook="echo This is PostHook" $> PreHook="echo This is PreHook" $> PostHook="echo This is PostHook" $> hook00.funtest This is PreHook This is PostHook 20

Hook #!/bin/bash funtestprehook () { return } funtestposthook () { return } funtest() { doc ="Hook test - Part II funtestprehook () { echo \"This is funtestprehook\"; } funtestposthook () { echo \"This is funtestposthook\"; } " } funtestprehook echo "" funtestposthook 21

Hook $> import hook01.sh $> hook01.funtest $> help hook01.funtest Hook test - Part II hook01.funtestprehook () { echo "This is funtestprehook"; } hook01.funtestposthook () { echo "This is funtestposthook"; } $> hook01.funtestprehook () { echo "This is funtestprehook"; } $> hook01.funtestposthook () { echo "This is funtestposthook"; } $> hook01.funtest This is funtestprehook This is funtestposthook 22

Demo Demo 23

Advantages VS Problems Advantages VS Problems Advantages Function reuse Unit testing Name space Documentation strings Grouping up commands Problems To Conflict with ImageMagick Can't work with busybox ash 24