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

Parameters and script flow improvement of shell

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

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

Today, I made up my mind that we must use the platform to do the work of affairs. The time is short and the task is tight. The previous operation and maintenance development has paved a surface, and each side has done some related things, so there has been progress in the work as a whole, but in the end, there is no dedicated function.

So I wonder what's going on, can I focus on a little bit, even if I just achieve a simple function, do it well, and whether other functions can copy a lot of ideas as well. So, after work, I began to sort out my thoughts. I asked myself and answered, each answer corresponds to two columns, one is the need to improve (action), and the other is the estimated time of the action. As a result, after a few minutes, I found that ideally, it would only take me two hours to get it done, which was acceptable, so I arranged an order of things that needed to be done and started doing it right away.

My design is to use the operation and maintenance platform to connect to the central control server to cascade trigger remote operations, so that the operation and maintenance platform has the lowest coupling to all environments.

Even the most rudimentary operation has to be done with clenched teeth, so it is generally like this process: there are two deployment requirements in the morning and two in the afternoon, the first one is completed in a semi-debug state, and the second starts to do the modular arrangement of the script, which is a semi-automatic process, and then finds some problems and is very tangled to fix the historical deployment problems. By the time of the third requirement, it is obviously much better. To the fourth, can be completed in one breath, the process is open, the details can be polished.

So I modified the logic of some scripts, there is a very general requirement, assuming that the script is initdb.sh to call this script needs to enter a series of parameters, such as 5 parameters.

There are 10 steps in this script, each of which is implemented using function. If you want to deal with these five parameters, you can control the execution of 10 steps at the same time, such as step 2 is not executed, step 3 is executed. In fact, the script is relatively difficult to manage and implement.

I have imagined several ways to achieve it:

1) split the 10 steps into 10 scripts, and then each script has its own calling method, which can be controlled independently.

2) write a configuration script, such as main.sh, and then control the execution of initdb.sh in this script

The second looks better, but the problem is still unsolved, because how to manage the parameters and how to deal with the 10 steps still have to be refined.

My preliminary idea is five parameters, with one parameter, the first five parameters and one step parameter.

The expected implementation is generally as follows:

# sh a.sh x x x x x 'init1 init3 init2'

Init1

Init3

Init2

That is, the execution is carried out in the way of init1 init3 init2.

The key code is as follows:

Function init1 () {

Echo 'init1'

}

Function init2 () {

Echo 'init2'

}

Function init3 () {

Echo 'init3'

}

Function initdb () {

Arr=$1

For fun in ${arr [*]}; do

Echo $I

Case $fun in

Init1)

Echo "test init1"

Init1

Init2)

Echo "test init2"

Init2

Init3)

Echo "test init3"

Init3

*)

Echo "out of scope"

Esac

Done

}

Init_par=$1

Actions= ($init_par)

Initdb "${actions [*]}"

The implementation results are as follows:

[root@dev01 test] # sh a.sh 'init1 init3 init2'

Test init1

Init1

Test init3

Init3

Test init2

Init2

There is a problem here, that is, the steps we enter may be unordered, but we want to execute them sequentially, for example, step 2 depends on step 1, which we are clear about. If you want to implement such a requirement, you need some additional additions, that is, the sorting function, or better control when the front end is passed in.

It's all right. Those who can do more work, so we'll make it happen first. Make sure it works correctly, so the code starts to change again.

Function init1 () {

Echo 'init1'

}

Function init2 () {

Echo 'init2'

}

Function init3 () {

Echo 'init3'

}

Function initdb () {

Arr=$1

Complete_arr= (init3 init1 init2)

Order_arr= ()

For act_tmp in ${complete_arr [*]}; do

If [["${arr [@]}" = ~ "$act_tmp"]]; then

Echo $act_tmp

Order_arr [${# order_arr [@]}] = $act_tmp

Echo order_arr [${# order_arr [@]}]

Fi

Done

For fun in ${order_arr [*]}; do

Echo $I

Case $fun in

Init1)

Echo "test init1"

Init1

Init2)

Echo "test init2"

Init2

Init3)

Echo "test init3"

Init3

*)

Echo "out of scope"

Esac

Done

}

Init_par=$1

Actions= ($init_par)

Initdb "${actions [*]}"

Assuming that the correct step is changed to init3 init1 init2, and the input parameter is init1 init3 init2, if you can convert it, your happiness will be greatly improved.

The execution result of the script is as follows:

[root@dev01 test] # sh aa.sh 'init1 init3 init2'

Init3

Order_arr [1]

Init1

Order_arr [2]

Init2

Order_arr [3]

Test init3

Init3

Test init1

Init1

Test init2

Init2

It can be seen that the expected goal has been achieved, so this idea can be used for reference and can be used in other places.

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: 251

*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