Linux Basic Learning Day 12th (shell functions, arrays and Yum usage and compilation installation)

Source: Internet
Author: User
Tags ftp site yum repolist

2016-08-18

Content of the lesson:

Shell Programming Basics (select loops and menus, functions)

Yum Management, custom Yum Warehouse


Select loops and Menus

Select variable in list

Do

Circular Body Command

Done

1. The Select loop is used primarily to create menus, the menu items in numerical order will be displayed on standard errors, and the PS3 prompt (#?) can be redefined to redefine PS3, waiting for user input

2. The user enters a number in the menu list, executes the corresponding command, the user input is saved in the built-in variable reply

[19:45 [Email protected]~]# sh 11.sh 1] add2) delete3) check4) quit#? 2delete#? 3check#? [19:47 [Email protected]~]#!shsh 11.sh 1) add2) delete3 check4) quitplease choose2deleteplease choose[19:47 [email prote cted]~]# cat! $cat 11.sh#!/bin/bashps3= "Please choose" Select var in add delete check Quitdoecho $vardone

3. Select is an infinite loop, so remember to exit the loop with the break command, or terminate the script with the Exit command. You can also press CTRL + C to exit the loop.

4. Select is often used in conjunction with case

[19:52 [email protected]~]# cat!] $cat 11.sh#!/bin/bashps3= "Please choose" Select var in add delete check quitdocase $var in Add) echo "$var";; Delete) echo "$var";; Check) echo "$var";; Quit) echo "quit" break;;  esacdone[19:52 [Email protected]~]#!shsh 11.sh 1) add2) delete3) check4) quitplease choose1addplease choose2deleteplease Choose3checkplease Choose4quit


Function:

"Please understand: a function is equivalent to a separate script"

1, the purpose: code reuse, improve efficiency

2. Introduction:

A. Function functions is a block of statements consisting of several shell commands for code reuse and modular programming.

B. It is similar to the shell program, but it is not a separate process, it cannot run independently, but is part of the shell program.

C. Functions are similar to shell programs, except that:

Shell program runs in child shell

The Shell function runs in the current shell. So in the current shell, the function can modify the variables in the shell

3. Functions currently created by Shell View:set

4. Defining functions (creating functions)

A. Functions consist of two parts: the function name and the function body. "Because the Linux system is used in the second way, we recommend using the second format to define the number of rows"

B. The function can be placed in a script file as part of its "to be placed at the beginning of the script", which can be placed in a separate file containing only functions.


Syntax One:

function f_name{

... function Body ...

}

Syntax Two:

F_name () {

... function Body ...

}

Example:

[19:58 [email protected]~]# cat funtions #!/bin/bashfiletype () {read-p "Please input the path:" Path[[! -e $path]] && {echo "The file is not exit"; exit;} | | Type= ' ls-ld $path |cut-c 1 ' case $type INL) echo "$path: Link file";; D) echo "$path:d irectory";;-) echo "$path: Common file";; *) echo "$path: Other file";; Esac

5. Function use

A. Creating a function directly at the beginning of the script

B.. /path/function

C.source/path/function

[20:06 [email protected]~]# cat!] $cat Filetype.sh#!/bin/bash. /root/funtionsfiletype

6. Delete function

As with the release variable, theunset FUNCTION

7. function return value

A. Execution result return value

In the normal shell, the execution result of the reference command is used in the anti-quote "', and for the function, the execution result of the reference function is also the" ""

B. Execution status return value

Same as usual shell script: $?, at this time $? The returned value is the defined return value, note that $? Returns the default depending on the exit status code of the last command executed in the function or a custom return value!

The exit status code for the function is "like exit of the script, stop execution when the return is executed, exit the function directly, and use the same as exit":

(1) The default depends 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 returns without error.

Return 1-255 has error returned

[20:31 [Email protected]~]# cat Sum.sh#!/bin/bashadd () {A=7b=8echo "$ (($a + $b))"}addecho "$?" #函数执行状态返回值c = ' ADD ' # Reference function execution result return value echo "$c" d=$ (($c +10)) echo "$d" [20:31 [email protected]~]# sh sum.sh #函数执行状态返回值0 #函数执行状态返回值15 # Reference function execution result return value 25


Yum Management:

Yum:yellowdogupdate modifier,rpm Front-end program to resolve package dependencies and to locate packages between multiple libraries

Yum is a class C/s architecture, client, server

Yum Repository:yum Warehouse, yum source (equivalent to server), yum repo, stores many RPM packages and related metadata files for packages (placed in a specific directory repodata)

File server:

ftp://

/HTTP

file:///, last/indicated root

The first two are network type, the latter is the model

Since all RPM packages installed on this machine (client) are dependent on the Yum Repository, the Yum repository must have the corresponding RPM packages and dependency packages

Yum's key is to have a reliable repository, as the name implies, this is the repository of software, it can be an HTTP or FTP site, can also be a local software pool, but must contain the RPM header, header includes the RPM package of various information, including description, function, Provide files, dependencies, etc. it is by collecting these headers and analyzing them that the remaining tasks can be completed automatically.

Yum Client configuration (with two):

1./etc/yum.conf: Provide public configuration for all warehouses

2,/etc/yum.repos.d/*.repo: For the point of the warehouse to provide configuration

The. Repo warehouse points to the definition:

[Repositoryid] #必填

Name=some name for this repository

baseurl=url://path/to/repository/#必填

Enabled={1|0} #是否启用该yum仓库,

GPGCHECK={1|0} #必填

Gpgkey=url

Enablegroups={1|0}

Failovermethod={roundrobin|priority}

The default is: Roundrobin, meaning random selection;

cost= defaults to 1000

3. Yum Source metadata (list of packages and related data), when the client accesses it, it caches the metadata repodata to the local machine's cache, which includes the list directory of the package and the dependencies between the packages. Therefore, the package is generally installed after the installation is automatically deleted, but the metadata file will not be deleted, but each access to the Yum source will access the Yum source metadata check code, if the checksum is changed in the local, the metadata will be re-downloaded, or the local Yum source data continue to be valid.


4. Metadata creation command: Creatrepo, generated metadata is stored under Repodata directory

A local yum can point to multiple yum warehouses (mirrors), which can be set at priority level

/etc/repos.d/*.repo: Each repo file is the point of a Yum repository.


[Email protected] repodata]# cat/etc/yum.conf

[Main] #名称: public

cachedir=/var/cache/yum/$basearch/$releasever

Keepcache=0 #: For cache package handling, 0 means delete after installation

debuglevel=2

Logfile=/var/log/yum.log

Exactarch=1 #: Precise platform Package handling

Obsoletes=1

Gpgcheck=1 #: Check the source and legality of packages

Plugins=1

Installonly_limit=5

Bugtracker_url=http://bugs.centos.org/set_project.php?project_id=19&ref=http://bugs.centos.org/bug_report_ Page.php?category=yum

Distroverpkg=centos-release

5, man yum.conf, can view the format and description of the Yum client configuration, BaseURL and mirrorlist are generally not defined at the same time, only define one of the

6, the installation package is the latest version of the default installation, Yum Repolist: The first time you run, download the source file list

7, the URL of repodata directory is the url!! of Yum source Two warehouse names under/etc/repos.d/*.repo cannot be the same!


RPM Package Customization (compile and install):

1, why to customize: RPM package version will lag behind the source package, or even lag behind a lot, such as; source package version: BIND-9.8.7,RPM package version: bind-9.7.2, this will make RPM package does not have a new version of the functionality or the old version of the vulnerability is not fixed, there is a hidden danger.


2, Custom: Manual compilation and Installation


Manually compiled packages are best installed in a specified file path, which facilitates later deletion management.


Three steps to compile the installation:

Prerequisite: Preparing the development Environment (compilation environment)

Install "Development Tools" and "Development Libraries"


1. Tar #解压源码包

2. CD #进入解压的源码包目录

3,./configure

--help

--prefix=/path/to/somewhere

--sysconfdir=/path/to/conffile_path

Function: 1, let the user select the compilation feature; 2, check the compilation environment;

4. Make

5. Make install

Tar XF tengine-1.4.2.tar.gzcd tegnine-1.4.2./configure--prefix=/usr/local/tengine--conf-path=/etc/tengine/ Tengine.confmakemake installexport path= $PATH:/usr/local/tengine/sbin/

After compiling the installation, complete the steps:

1, modify the PATH environment variable, to be able to identify the binary file path of this program;

Modify the/etc/profile file

In the/etc/profile.d/directory, create a file with a name suffix of. SH, which defines the export path= $PATH:/path/to/somewhere

2, by default, the system search library file path/lib,/usr/lib; To add additional search paths:

Create a file with a suffix of. conf in/etc/ld.so.conf.d/, and then write the path you want to add directly to this file (the next time the login takes effect);

# Ldconfig: Notifies the system to re-search the library file

-V: Shows the process of re-searching the library

3. header file: Output to System

Default:/usr/include

Add header file search path, using links to:

/usr/local/tengine/include//usr/include/

Two different ways:

Ln-s/usr/local/tengine/include/*/usr/include/or

Ln-s/usr/local/tengine/include/usr/include/tengine

4. Man file path: The man directory installed in the directory specified by--prefix;/usr/share/man

1, man-m/path/to/man_dir COMMAND (quit shell failure)

2, add a manpath in the/etc/man.config

/configure A more important parameter is--prefix, with the--prefix parameter, we can specify the software installation directory, when we do not need this software, directly delete the software directory is OK

"Library is function"

Make, do install can be written as: Makes && do install



Shell array, inserting element values is not supported

The array of Bash supports sparse format (index discontinuity)

Definition: aa= (1 2 3 4 5): Elements separated by spaces

Length: ${#AA [@]}

add element: Aa[2]=value

Delete element: unset aa[2]

Delete array: unset aa[@]

Interception of arrays: array{aa[@]:2:3},2 represents skipped elements, 3 means the number of elements intercepted

Substitution of elements: Array{aa[@]/5/cs}: Replace elements with element value 5 with CS "The method is not really changing the array, just showing the change"

Example: generating 10 random numbers, using a bubbling algorithm to sort ascending or descending (using a shell array)

#!/bin/bash#created by hill#contact:[email protected]# Version:1.0#create time:2016-08-20 18:37:37#description: Generates 10 random numbers and uses the bubbling algorithm to sort ascending or descending declare -a  rangfor i in  ' seq 0 9 ' do    rang[$i]= $RANDOMdoneecho   " before:${rang[@]} "for j in  ' seq 10 ' do    for k in  ' seq  $ ((10-$j)) '     do        if [ ${ rang[$[$k -1]]} -ge ${rang[$k]} ];then             tmp=${rang[$k]}            rang[ $k]=${rang[$[$k -1]]}            rang[$[$k -1]]= $tmp         fi    donedoneecho  "after:${rang[@]}" 


Linux Basic Learning Day 12th (shell functions, arrays and Yum usage and compilation installation)

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.