• No results found

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

N/A
N/A
Protected

Academic year: 2021

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

Copied!
24
0
0

Loading.... (view fulltext now)

Full text

(1)

How to write a bash script like the python?

Lloyd Huang

KaLUG - Kaohsiung Linux User Group

COSCUP Aug 18 2012

(2)

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

(3)

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

(4)

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.

(5)

Bash source VS Python import

How Bash do it?

(6)

Bash source VS Python import

#!/bin/bash | #!/usr/bin/python funtest() { | 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 $> python

func for test >>> import samp00 func for fake

$> bash samp00.sh $> python samp00.py func for test func for test func for fake func for fake

(7)

Bash source VS Python import

#!/bin/bash | #!/usr/bin/python funtest() { | 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.py func for test func for test func for fake func for fake

(8)

Bash source VS Python import

#!/bin/bash

funtest() {

echo "func for test"

}

funfake() {

echo "func for fake"

}

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

funtest

funfake

fi

(9)
(10)

Python module and docstring

Python module and docstring

• import, from

• import module as someone

– Import module

• del

– Delete object

• docstring

(11)

Python module and docstring

#!/usr/bin/python

def 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()

(12)

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.

(13)

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

(14)

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

(15)

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 "func for test" }

funfake() {

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

} if [ "$0" == "$BASH_SOURCE" ]; then funtest funfake fi

15

(16)

Bash import and docstring ?

$> source import.sh $> import samp02.sh $> samp02 samp02.funfake samp02.funtest $> samp02.funtest

func for test

$> import samp02.sh as new_samp $> new_samp.funtest

func for test

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

(17)

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

(18)
(19)

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 "func for test"

test -n "$PostHook" && $PostHook

}

(20)

Hook

$> import hook00.sh

$> hook00.funtest

func for test

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

func for test

This is PostHook

(21)

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 "func for test" funtestPostHook }

(22)

Hook

$> import hook01.sh $> hook01.funtest func for test

$> 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 func for test

(23)

Demo

Demo

(24)

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

References

Related documents

In accordance with literature denoting pupil behaviour as anti-structure (see 2.6), individual pupils will have to position themselves amongst peers within the

– File Input – Python can read in the contents of files – File Output – Python can write text to

Low performance, binge drinking and regular cannabis use (for skipping and any cause only) also increased the number of missed days, higher parental control and support reduced it

For this report, CFA looked at premium data from the five largest auto insurers for a single good driver profile in most ZIP codes throughout the country and examined average

Understanding the effects of manipulations in the absence of pilot studies or previous research is important to the increasing number of studies using GC

Professor Muhanna has published numerous articles in scholarly journals, including Management Science, MIS Quarterly, Strategic Management Journal, Decision Sciences, the Journal

This video will help you setup Python Environment in your computer and write your first Python program.. Spotle.ai

• The Python script creates the contiguous sub- regions (sampling volume) freely oriented in the dataset volume.. Python Script