Functions of Shell programming values and use of select

Source: Internet
Author: User
Tags function definition

Select

The Select loop is used primarily to create menus, the menu items in numerical order are displayed on standard errors, and a PS3 prompt is displayed, waiting for the user's input, the user enters a number in the menu list, executes the corresponding command, the user input is saved in the variable the REPLY.

Select is an infinite loop, so remember to exit the loop with the break command, or exit the script with exit , or you can use Ctrl + C to exit the loop

Select Statement structure:

Select variable in option 1 option 2;d o

Break

Done

#!/bin/sh

Ps3= "What are you favourite OS?:"

Select I in "Linux" "Windows" "Mac";d o

echo "You have select $REPLY"

Break

Done

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/86/48/wKioL1e66cXQjoFdAABSGOLfjX8172.jpg-wh_500x0-wm_3 -wmp_4-s_601026961.jpg "title=" 2.jpg "alt=" Wkiol1e66cxqjofdaabsgolfjx8172.jpg-wh_50 "/>

use of Select and if selection statements

#!/bin/bash

#

ps3= "Please input a color:"

Select color in red white black; Do

if[["$color" = = "Red"]];then

echo "is good"

elif[["$color" = = "white"];

echo "is OK"

elif[["$color" = = "Black"]];then

echo "Is God"

Fi

Break

Done

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M02/86/48/wKioL1e66dai61IjAAB1tpmudc8913.jpg-wh_500x0-wm_3 -wmp_4-s_488755383.jpg "title=" 3.jpg "alt=" Wkiol1e66dai61ijaab1tpmudc8913.jpg-wh_50 "/>

Select and case

#!/bin/bash

#

ps3= "Please input a color:"

Select Color in "Red" "White" "Black"; Do

Case$color in

Red

echo "is Goog"

;;

White

echo "is OK"

;;

Black

echo "Is God"

;;

*)

Exit

;;

Esac

Done

Break

Done

650) this.width=650; "Src=" Http://s5.51cto.com/wyfs02/M01/86/48/wKioL1e66faBvp9sAABPFPsHH2g459.jpg-wh_500x0-wm_3 -wmp_4-s_3932203987.jpg "title=" 4.jpg "alt=" Wkiol1e66fabvp9saabpfpshh2g459.jpg-wh_50 "/>


Function

function Functions are a block of statements consisting of several shell commands that enable code reuse and modular programming, maximize code reuse and minimize code redundancy, and are similar to shell programs. The difference is that he is not a separate process and cannot run independently, but is part of the shell program. function is

difference between a function and a shell program

The Shell program runs in the child shell, and the Shell function runs in the current shell, so in the current shell, the function can be shell Middle variable to modify

Functions consist of two parts: function name and function body

Syntax One:

function F_name {

function body

}

Syntax Two:

function F_name () {

function body

}

Syntax Three:

F_name (){

function body

}

The function has two return values:

The return value of the function's execution result:

(1) output with the echo or printf command

(2) output of the call command in the function body

Exit code status for the function:

(1) Depending on the exit status code of the last command executed in the function

(2) Custom exit status code, in the form of:

Return returned from the function, with the last state command to determine the return value

return 0 no error returned

Return 1-255 returned with error

The function must be defined before it is used, so the function definition should be placed at the beginning of the script until the shell first discovers it before it can be used

Use the function name directly at the time of invocation, as shown in the following example


Use the function to display the host name of the current system

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/86/49/wKiom1e67BbgiLkGAAATWCB98nY387.png-wh_500x0-wm_3 -wmp_4-s_3581957900.png "title=" 5.png "alt=" Wkiom1e67bbgilkgaaatwcb98ny387.png-wh_50 "/>


Use the UID and shell type of the function display

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/86/49/wKiom1e67CGAZbcrAAAeyDJNe00223.png-wh_500x0-wm_3 -wmp_4-s_2926984015.png "title=" 6.png "alt=" Wkiom1e67cgazbcraaaeydjne00223.png-wh_50 "/>

Sometimes we do a lot of things, each time we re-write it too time-consuming, with the function we can define the action to be performed in the function, so that when we want to perform certain operations, directly to call the function to complete the operation, if it is more than one function, then can be defined in a file, and then load the function into the shell, the file name can be arbitrarily taken, but it is better to have some kind of relationship with some tasks, so it is easy to find some.

Once the function file is loaded into the shell, you can call the function in a command or script. You can use the set command to view all defined functions whose output list includes all functions that have been loaded into the shell

to change a function, first remove the function from the shell with the Unst command , and then reload the file after the change is complete

Methods for loading functions

. FileName or source filename (The difference between the two is that the variable is not loaded into the environment variable, and the latter is added to the environment variable)

Ways to delete a function

use unset function_name as you would delete a variable

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M01/86/48/wKiom1e66nbApo4tAAAezqLTA0Y448.png-wh_500x0-wm_3 -wmp_4-s_3950261126.png "title=" 7.png "alt=" Wkiom1e66nbapo4taaaezqlta0y448.png-wh_50 "/>

function parameters

Pass parameters to function: When calling a function, separate the given argument list with whitespace after the function name, for example "testfunc arg1 arg2 ... "

in the function body, you can use $, $, ... call these parameters, and you can also use the [email protected], $*, $# and Other special variables, note: If the function has a local variable, if its name is the same as the local variable, use the local variable

1, Fibonacci series, also known as the Golden Section series, because the mathematician Leonardo's Fibonacci to the rabbit breeding as an example of the introduction, so called "Rabbit series", refers to such a series:0,1,1,2,3,5,8,13,21st,34、......, the Fibonacci sequence is defined in a recursive way as follows:F(0)=0,F(1)=1,F(N)=f (n-1) +f (n-2)(N≥2)

write a function to find the N- order Fibonacci sequence

#!/bin/bash

#

Fab () {

if[$1-eq 1]; then

Echo-n "1"

elif [$1-eq 2]; then

Echo-n "1"

Else

Echo-n "$[$ (Fab $[$1-1]) + $ (fab $[$1-2])"

Fi

}

For i in {1..10};d o

Fab$i

Echo

Done

650) this.width=650; "Src=" Http://s1.51cto.com/wyfs02/M02/86/48/wKiom1e66pmx2_AmAABBynNGjss176.png-wh_500x0-wm_3 -wmp_4-s_4197065163.png "title=" 8.png "alt=" Wkiom1e66pmx2_amaabbynngjss176.png-wh_50 "/>

2. Hanoi (also known as Hanoi) is an ancient Indian legend. When big Brahma created the world, he made three diamond pillars, and stacked the gold discs on a pillar from bottom to top in order of size . The great Brahma commanded the Brahman to rearrange the discs from below to the other pillars in order of size. It is also stipulated that the disc cannot be enlarged on the small disc, and only one disc can be moved between the three pillars at a time.

using functions to implement moving steps of the Hanoi of the N-disc

#!/bin/bash
#
Step=0
Move () {
Let step++
echo "$step: Move disk $-----> $"
}
Hanoi () {
If [$1-eq 1];then
Move $ $4
Else
Hanoi "$[$1-1]" $ $4 $
Move $ $4
Hanoi "$[$1-1]" $ $4
Fi
}
Read-p "Please input the number of plates:" Number
Hanoi $number A B C

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/86/48/wKioL1e66snwEMslAABBynNGjss388.png-wh_500x0-wm_3 -wmp_4-s_3457343623.png "title=" 8.png "alt=" Wkiol1e66snwemslaabbynngjss388.png-wh_50 "/>





This article is from the "Operation and maintenance Career" blog, please make sure to keep this source http://fszxxxks.blog.51cto.com/10122713/1841221

Functions of Shell programming values and use of select

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.