Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Programming fundamentals of shell Learning

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Students who have studied programming languages can quickly start with shell because of the basic character types, circular statements and C language. Let's learn about the following beginner basics:

1. Learn some basic character information before learning loop statements:

$0, $1, $2, $?, $*, $

$0 represents the sh program itself, for example: sh auto_system_backup.sh, so the $0 here is auto_system_backup.sh

Usage: {$0 / boot / etc} this is a hint in programming: the display result is Usage: {auto_system_backup.sh / boot / etc}

$1 represents the first parameter after the execution of the script, for example: sh auto_system_backup.sh / ect/ / tmp/, then $1 here is / ect/, and naturally / tmp/ is $2

$1 will come in handy when we need to selectively perform certain tasks, for example, $1 is used in the choice of backup database mentioned in the above blog posts.

-

If [- z "$1"]; then

Echo-e "\ 033 [32mUsage:\ nPlease Enter DataBase that you will backup\ NMUI -\ n\ nUsage: {$0 mysql}\ 033 [0m"

Exit

Fi

-

$? Indicates that there are no errors in the execution of the above commands. If not, return 0, which is mainly used to check for errors in the code. If there are any errors, prompt or no longer execute the following code.

-

If [$?-eq 0]; then

Echo-e "\ 033 [32mThe Mysql Backup $MYSQLDB Successfully!\ 033 [0m"

Else

Echo-e "\ 033 [32mThe Mysql Backup $MYSQLDB Failed,Please check.\ 033 [0m"

Fi

-

$* represents all parameters following the execution of the script

If [- z "$*"]; then

Echo-e "\ 033 [32mUsage:\ nPlease Enter Your Backup Files or Directories\ n Murray -\ n\ nUsage: {$0 / boot / etc}\ 033 [0m"

Exit

Fi

$# indicates the number of all parameters following the execution of the script

The following script, var02.sh, can display the above parameter effects:

-

#! / bin/bash

# define path variables

# by authors robin 2017

Echo "#"

Echo "The\ $1 is $1"

Echo "The\ $2 is $2"

Echo "The\ $? is $?"

Echo "The\ $* is $*"

Echo "The\ $# is $#"

-

Execute script: sh var02.sh abc edf

The results are as follows:

#

The $1 is abc

The $2 is edf

The $? Is 0

The $* is abc edf

The $# is 2

-

two。 Let's take a look at some basic logical operators:

-f determine whether the file exists eg: if [- f filename]

-d determine whether eg exists in the directory: if [- d dir]

Eq equals to apply to: integer comparison

-ne is not equal to apply to: integer comparison

-lt is less than applied to: integer comparison

-gt is greater than applied to: integer comparison

-le less than or equal to apply to: integer comparison

-ge greater than or equal to apply to: integer comparison

-a both sides and logical expression-a logical expression

-o unilateral establishment (or) logical expression-o logical expression

-z empty string:-z "$*" where $* and $1 appear as characters

-

You can also see them in some of the above scripts, and then take a closer look at what they have done. The rest will be understood by everyone in the later script parsing.

3. Loop statement: for if while, etc.

The three judgment statements are the foundation of programming, and they are dealt with by nesting each other in logical operations. Here are a few simple examples:

If statement:

Judge the statement once, and judge the statement after execution if it is 0 in [].

-if_files_exist00.sh-

#! / bin/bash

# juge dir exist

# fome net 2017

If [!-d / home/test1]; then

Mkdir-p / home/test1

Else

Echo "This DIR is exist, Please exist."

Fi

The above code explains: if the / home/test1 directory doesn't exist, we'll create it and prompt it if it does, and modify it slightly with the $1 above.

-if_files_exist01.sh

#! / bin/bash

# juge dir exist

# from net 2017

DIR=$1

If [!-d $1]; then

Mkdir-p $1

Else

Echo "The dir $1 is exist, Please exit."

Fi

Sh if_files_exist01.sh / home/test02-this is where the script is executed to manually add parameters

In the above script, we can also add a prompt message to prompt the user to enter parameters. That is, to determine whether $1 is empty, I will not write more here, I will combine the previous code modification!

Elif statement:

-enter the score to judge the pros and cons.

#! / bin/bash

# juge scores

# from net 2017

Scores=$1

If [- z $scores]; then

Echo "Usage: {$060 | 80.}"

Exit

Fi

If [$scores-gt 85]; then

Echo "very good!"

Elif [$scores-gt 75]; then

Echo "good!"

Elif [$scores-ge 60]; then

Echo "pass!"

Else

Echo "no pass!"

Fi

For statement:

Judge many times and deal with it in a cycle as long as the condition is established.

-the following code calculates the sum of 1-15

#! / bin/bash

Jroom0

For i in `seq 1 15`

Do

J = `expr $I + $j`-- this is equivalent to j=i+j in C language.

Done

Echo $j

-

While statement:

It is also judged many times until the condition is not established.

-

#! / bin/bash

ITunes 0

While ($I

< 10)) do echo "The number is $i" ((i++)) done ---------------------------- select语句: 这语句主要用于选取,并获得选取的值,以下程序运行选取的2那么就会获得$i的值为数组中的第二个 语法结构: select i in "1" "2" "3"; do 代码。。。。。。。 done ---------------------------------- #!/bin/bash select i in "Centos" "Redhat" "ubuntu"; do echo "Select you use system:"$i; done --------------------------------------

Most of the time this select statement is combined with the case statement to deal with the problem

Case statement: plays an important role in writing large automation scripts

The following code is mainly used to selectively execute program code

#! / bin/bash

# auto install LAMP

# from net 2017

Case $1 in

Apache)

Echo "Wait install httpd server...."

Tar-jxvf httpd-2.2.27.tar.bz2;cd httpd-2.2.27

Mysql)

Echo "Wait install mysql server...."

PHP)

Echo "Wait install mysql server...."

*)

Echo-e "\ 033 [32mUsage: {$0 Apache | Mysql | PHP | help}\ 033 [0m"

Esac

Let's take a look at the implementation effect:

Obviously, after receiving the parameters, we begin to execute the code under the module, and under each module we can write any shell code to execute. You can rub all the code into it in a later large-scale service deployment. For example, automatic installation service, configuration service, database backup, database recovery, directory data synchronization, version upgrade and so on. Both are fine, but in order not to make it too complicated, it is recommended not to write too many functions in it. The following combines case and select to write a simple little script that does not perform a specific task:

-- auto_lamp_case01.sh--

#! / bin/bash

# auto install LAMP

# fom net 2017

PS3= "Select your will exec Menu:"

Select i in "Apache"Mysql"PHP"

Do

Case $I in

Apache)

Echo "Wait install httpd server...."

Tar-jxvf httpd-2.2.27.tar.bz2;cd httpd-2.2.27

Mysql)

Echo "Wait install mysql server...."

PHP)

Echo "Wait install mysql server...."

*)

Echo-e "\ 033 [32mUsage: {$0 Apache | Mysql | PHP | help}\ 033 [0m"

Esac

Done

Since there is only a simple display code under each module, we can perfect the code according to our own needs at a later stage, and the execution effect is as follows

Obviously, this code is smarter, prompting more choices after executing the program, and there are a lot of things that can be extended on this script to achieve more automated processing.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report