Object-oriented Shell scripts

Source: Internet
Author: User
Do you still remember the regular expression that used to calculate prime numbers? The world of programming is so interesting that you will always be able to see something special. Have you ever thought that you can put your variables and functions into a class when writing Shell scripts? Don't think this is impossible. no, I saw another online... information &

Do you still remember the regular expression that used to calculate prime numbers? The world of programming is so interesting that you will always be able to see something special. Have you ever thought that you can put your variables and functions into a class when writing Shell scripts? Don't think this is impossible. no, I saw another object-oriented Shell script on the Internet. Shell is not supported. you need to do something yourself. the hacker who can make this is really a hacker.

Of course, this is not really object-oriented, because it is just encapsulation and does not support inheritance and polymorphism. The most abnormal thing is that he still supports typeid!

Let's see how he did it. The following script may be confusing. I wanted to explain it. later I thought about it. I 'd suggest you study it on your own. In fact, it's not hard to understand it. I 'd like to give you a few tips.

As we can see, the following script defines functions such as class, func, var, and new, which are actually so-called keywords.
Class is a function, mainly a record class name.
Func and var are actually various variables with the same prefix of member function names and member variables.
The new method records instances. Let's take a look at the for loop in the new function, where the core is.
The script is as follows:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 #! /Bin/bash # ------------------------------------------------------------------- # OO support functions # Kludged by Pim van Riezen # ------------------------------------------------------------------- DEFCLASS = "" CLASS = "" THIS = 0 class () {DEFCLASS = "$1" eval CLASS _ $ {DEFCLASS} _ VARS = "" eval CLASS _ $ {DEFCLASS} _ FUNCTIONS = ""} static () {return 0} func () {local varname = "CLASS _ $ {DEFCLASS} _ FUNCTIONS" eval "$ varname = \" \$ {$ varname} $1 \ ""} var () {local varname = "CLASS _ $ {DEFCLASS} _ VARS" eval $ varname = "\" \$ {$ varname} $1 \ ""} loadvar () {eval "Varlist = \" \ $ CLASS _ $ {CLASS} _ VARS \ "" for var in $ varlist; do eval "$ var = \" \ $ INSTANCE _ $ {THIS} _ $ var \ "" done} loadfunc () {eval "funclist = \" \ $ CLASS _ $ {CLASS} _ FUNCTIONS \ "" for func in $ funclist; do eval "$ {func }() {$ {CLASS }::$ {func} \ "\ $ * \"; return \ $ ?;} "Done} savevar () {eval" varlist = \ "\ $ CLASS _ $ {CLASS} _ VARS \" "for var in $ varlist; do eval "INSTANCE _ $ {THIS} _ $ var = \" \ $ var \ "" done} typeof () {eval echo \ $ TYPEOF _ $1} new () {local cvar = "$2" shift local id = $ (uuidgen | tr A-F a-f | sed-e "s // g ") eval TYPEOF _ $ {id} = $ class eval $ cvar = $ id local funclist eval "funclist = \" \ $ CLASS _ $ {class} _ FUNCTIONS \ "for func in $ funclist; do eval "$ {cvar }. $ {Func} () {local t =\$ THIS; THIS = $ id; local c =\$ CLASS; CLASS = $ class; loadvar; loadfunc; $ {class }:: $ {func} \ "\ $ * \"; rt =\$ ?; Savevar; CLASS =\$ c; THIS =\$ t; return $ rt;} "done eval" $ {cvar }. $ {class} \ "\ $ * \" | true "}

Next, let's take a look at the routine.

123456789101112131415161718192021222324252627282930313233343536373839404142 # coding # Example code # coding # class definition class Storpel func setName func setQuality func print var name var quality # class implementation Storpel: Storpel () {setName "$1" setQuality "$2" if [-z "$ name"]; then setName "Generic"; fi if [-z "$ quality"]; then setQuality "Normal"; fi} Storpel: setName () {name = "$1";} Storpel: setQuality () {quality = "$1";} Storpel:: print () {echo "$ name ($ quality)" ;}# usage new Storpel one "Storpilator 1000" Medium new Storpel two new Storpel three two. setName & quot; Storpilator 2000 & quot; two. setQuality "Strong" one. print two. print three. print echo "" echo "one: $ one ($ (typeof $ one)" echo "two: $ two ($ (typeof $ two)" echo "three: $ three ($ (typeof $ two ))"

 

(Full text)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.