The function and library of Shell learning (I.)

Source: Internet
Author: User

Functions are the essence of the shell, and with functions and libraries, the shell handles complex problems as well as the compiler language, gravitated the importance of shell functions and libraries.

1 Definition of function

Recommended Use

How to define Func_name () {}

Some people might like to add a function tag to the front, which is bash's unique

function Func_name () {}

A little simple, easy to identify is a function

2 function return value

Can generally be used

① in the function, return returns

② call function, by command reference such as a= ' func_name haha '

3 A small example of the book, calculated 1-6 of two times, three-time Square

[email protected] shell]# cat square-cube.sh

#!/bin/bash

#

txt= "_mktmp"

Square_cube ()

{

echo "$ * $" |BC > $

echo "$ * $ * $ * $" |BC >> $

}

For i in ' SEQ 1 5 '

Do

Square_cube $txt $i

Square= ' Head-1 $txt '

Cube= ' Tail-1 $txt '

echo "Square of $i is $square"

echo "Cube of $i is $cube"

Done

Rm-f $txt

Execution Result:

[Email protected] shell]#./square-cube.sh

Square of 1 is 1

Cube of 1 is 1

Square of 2 is 4

Cube of 2 is 8

Square of 3 is 9

Cube of 3 is 27

Square of 4 is 16

Cube of 4 is 64

Square of 5 is 25

Cube of 5 is 125

Summary: Here is a very simple function call, function is to process the calculation results are saved in a specific location, the main script is responsible for controlling the flow

Tips:1 and L are very similar, we should pay attention to the harm I wasted time

4 Debugger Script

[email protected] shell]# cat debugger.sh

#!/bin/bash

#

#this a debugger by function

Logfile=/tmp/log.log

#指定脚本名

Appname=$0

#指定DEBUG级别, the higher the more redundant information displayed

verbose=10

#logmsg负责显示传递给它的消息

Logmsg ()

{

echo "$APPNAME: ' Date ' [email protected] ' >> $LOGFILE

}

#debug负责显示错误/debug Information

Debug ()

{

Verbosity=$1

#shift的目的在于把级别挪走 so that only debug information is displayed

Shift

If ["$verbosity"-le "$VERBOSE"]; then

echo "$APPNAME: ' Date '---level:${verbosity}---: [email protected]" >> $LOGFILE

Fi

}

#die函数你懂得

Die () {

echo "$APPNAME: ' Date '----fatal [email protected]"

Exit 1

}

#主脚本测试

#显示脚本开始, it seems like a little extra.

Logmsg now the log-checking system start-------

#测试uname-a command is present

uname-a| | Die uname-a command not find.

#uname存在的话, continue to execute

Logmsg-----------system-info ' uname-a '

#判断是redhat或者debian系统

cat/etc/redhat-release| | Debug 8 This was not a redhat system!

cat/etc/debian-release| | Debug 8 This was not a Debian system!

#书上更多点, I omitted some of it here, for brevity

Run results

[Email protected] shell]#./debugger.sh

Linux www.centos.vbird 2.6.32-358.el6.x86_64 #1 SMP Fri Feb 00:31:26 UTC x86_64 x86_64 x86_64 gnu/linux

CentOS Release 6.4 (Final)

Cat:/etc/debian-release:no such file or directory

In summary, here are three functions used in coordination with each other, there is revocation, there are errors, note [email protected] refers to all parameters, the name of the file, "$" refers to the first parameter, and so on, if you do not know the words of the evil complement

5 Examples of recursive functions

Typical a: calculating factorial

[email protected] shell]# cat factorial.sh

#!/bin/bash

#

Factorial () {

If ["$"-gt "1"];then

previous= ' Expr $1-1 '

parent= ' factorial $previous '

result= ' expr $ \* $parent '

Echo $result

Else

Echo 1

Fi

}

Factorial $

Execution Result:

[Email protected] shell]# sh-x factorial.sh 3

+ factorial 3

+ ' [' 3-GT 1 '];; Judging if it's 1.

+ + expr 3-1

+ previous=2

+ + factorial 2;; Since parent= ' factorial $previous ' called the function in the function, it is recursive

+ + ' [' 2-gt 1 '] '

+ + expr 2-1

+ + Previous=1

+ + + factorial 1

+ + + ' [' 1-gt 1 '] '

+ + + Echo 1

+ + parent=1;; Finally know that the factorial of the initial condition is 1 is 1

+ + expr 2 ' * ' 1

+ + result=2

+ + Echo 2

+ parent=2

+ + expr 3 ' * ' 2

+ result=6

+ echo 6

6

Can be counted as a large number, is slow ...

The function and library of Shell learning (I.)

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.