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

COSCUP2012: How to write a bash script like the python?

Embed Size (px)

Citation preview

Page 1: COSCUP2012: How to write a bash script like the python?

How to write a bash script like the python?

Lloyd HuangKaLUG - Kaohsiung Linux User Group

COSCUP Aug 18 2012

Page 2: COSCUP2012: How to write a bash script like the python?

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

Page 3: COSCUP2012: How to write a bash script like the python?

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

Page 4: COSCUP2012: How to write a bash script like the python?

Bash source VS Python import

Bash source VS Python import

Python

• A module allows you to logically organize your Pythoncode. Grouping related code into a module makes thecode easier to understand and use.

• Python modules are easy to create; they're just filesof Python program code.

• if __name__ == '__main__':

4

Page 5: COSCUP2012: How to write a bash script like the python?

Bash source VS Python import

How Bash do it?

5

Page 6: COSCUP2012: How to write a bash script like the python?

Bash source VS Python import

#!/bin/bash | #!/usr/bin/pythonfuntest() { | def funtest():

echo "func for test" | print "func for test"} |funfake() { | def funfake():

echo "func for fake" | print "func for fake"} |# main | if __name__ == '__main__':funtest | funtest()funfake | funfake()

$> source samp00.sh $> pythonfunc for test >>> import samp00func for fake

$> bash samp00.sh $> python samp00.pyfunc for test func for testfunc for fake func for fake

6

Page 7: COSCUP2012: How to write a bash script like the python?

Bash source VS Python import

#!/bin/bash | #!/usr/bin/pythonfuntest() { | def funtest():

echo "func for test" | print "func for test"} |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.pyfunc for test func for testfunc for fake func for fake

7

Page 8: COSCUP2012: How to write a bash script like the python?

Bash source VS Python import

#!/bin/bashfuntest() {

echo "func for test"}funfake() {

echo "func for fake"}if [ "$0" == "$BASH_SOURCE" ]; then

funtestfunfake

fi

8

Page 9: COSCUP2012: How to write a bash script like the python?

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

Page 10: COSCUP2012: How to write a bash script like the python?

Python module and docstring

Python module and docstring

• import, from

• import module as someone

– Import module

• del

– Delete object

• docstring

– Python Documentation Strings

10

Page 11: COSCUP2012: How to write a bash script like the python?

Python module and docstring

#!/usr/bin/pythondef funtest():

"""Here is python docstring for funtest.Here is python docstring for funtest."""print "func for test"

def funfake():"Here is python docstring for funcfake."print "func for fake"

if __name__ == '__main__':funtest()funfake()

11

Page 12: COSCUP2012: How to write a bash script like the python?

Python module and docstring

$> python>>> import samp02>>> samp02.samp02.__doc__( samp02.funtest( samp02.funfake(

>>> samp02.funtest()func for test

>>> help (samp02.funtest)>>>Help on function funtest in module samp02:

funtest()Here is python docstring for funtest.Here is python docstring for funtest.

12

Page 13: COSCUP2012: How to write a bash script like the python?

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()func for test

>>> 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

Page 14: COSCUP2012: How to write a bash script like the python?

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

Page 15: COSCUP2012: How to write a bash script like the python?

Bash import and docstring ?

#!/bin/bashfuntest() {

__doc__="Here is bash import.sh docstring for funtestHere is bash import.sh docstring for funtest"

echo "func for test"}funfake() {

__doc__="Here is bash import.sh docstring for funfake"echo "func for fake"

}if [ "$0" == "$BASH_SOURCE" ]; then

funtestfunfake

fi

15

Page 16: COSCUP2012: How to write a bash script like the python?

Bash import and docstring ?

$> source import.sh$> import samp02.sh$> samp02samp02.funfake samp02.funtest$> samp02.funtestfunc for test$> import samp02.sh as new_samp$> new_samp.funtestfunc for test$> help new_samp.funtestHere is bash import.sh docstring for funtestHere is bash import.sh docstring for funtest

$> del new_samp$> new_samp.funtestbash: new_samp.funtest� command not found

16

Page 17: COSCUP2012: How to write a bash script like the python?

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 conifs="$1"; shiftcon="$1"; shiftIFS="$ifs" read $@ < <(echo $con)

}

17

Page 18: COSCUP2012: How to write a bash script like the python?

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

Page 19: COSCUP2012: How to write a bash script like the python?

Hook

Hook

#!/bin/bashfuntest() {

__doc__="Hook test - Part IPreHook=\"echo This is PreHook\"PostHook=\"echo This is PostHook\""

test -n "$PreHook" && $PreHookecho "func for test"test -n "$PostHook" && $PostHook

}

19

Page 20: COSCUP2012: How to write a bash script like the python?

Hook

$> import hook00.sh$> hook00.funtestfunc for test

$> help hook00.funtestHook test - Part IPreHook="echo This is PreHook"PostHook="echo This is PostHook"

$> PreHook="echo This is PreHook"$> PostHook="echo This is PostHook"$> hook00.funtestThis is PreHookfunc for testThis is PostHook

20

Page 21: COSCUP2012: How to write a bash script like the python?

Hook

#!/bin/bashfuntestPreHook () {

return}funtestPostHook () {

return}funtest() {

__doc__="Hook test - Part IIfuntestPreHook () { echo \"This is funtestPreHook\"; }funtestPostHook () { echo \"This is funtestPostHook\"; } "

funtestPreHookecho "func for test"funtestPostHook

}

21

Page 22: COSCUP2012: How to write a bash script like the python?

Hook

$> import hook01.sh$> hook01.funtestfunc for test

$> help hook01.funtestHook test - Part IIhook01.funtestPreHook () { echo "This is funtestPreHook"; }hook01.funtestPostHook () { echo "This is funtestPostHook"; }

$> hook01.funtestPreHook () { echo "This is funtestPreHook"; }$> hook01.funtestPostHook () { echo "This is funtestPostHook"; }$> hook01.funtestThis is funtestPreHookfunc for testThis is funtestPostHook

22

Page 23: COSCUP2012: How to write a bash script like the python?

Demo

Demo

23

Page 24: COSCUP2012: How to write a bash script like the python?

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