Chapter 6 Advanced Shell script programming

4.2.12 select loop and menu

Format:

select NAME [in WORDS ... ;] do COMMANDS; done

select variable in list ;do
    Loop command
done

explain:

  • The select loop is mainly used to create menus. The menu items arranged in numerical order are displayed on the standard error, and the PS3 prompt is displayed for user input
  • The user enters a number in the menu list and executes the corresponding command
  • User input is saved in the built-in variable REPLY
  • select is an infinite loop, so use the break command to exit the loop or the exit command to terminate the script. You can also press ctrl+c to exit the loop
  • select is often used in conjunction with case
  • Similar to the for loop, you can omit the in list and use the position parameter

example:

[root@rocky8 ~]# type select
select is a shell keyword
[root@rocky8 ~]# help select
select: select NAME [in WORDS ... ;] do COMMANDS; done
    Select words from a list and execute commands.
    
    The WORDS are expanded, generating a list of words.  The
    set of expanded words is printed on the standard error, each
    preceded by a number.  If `in WORDS' is not present, `in "$@"'
    is assumed.  The PS3 prompt is then displayed and a line read
    from the standard input.  If the line consists of the number
    corresponding to one of the displayed words, then NAME is set
    to that word.  If the line is empty, WORDS and the prompt are
    redisplayed.  If EOF is read, the command completes.  Any other
    value read causes NAME to be set to null.  The line read is saved
    in the variable REPLY.  COMMANDS are executed after each selection
    until a break command is executed.
    
    Exit Status:
Returns the status of the last command executed.

[root@rocky8 ~]# select menu in backup upgrade downgrade clean ;do true ;done
1) backup
2) upgrade
3) downgrade
4) clean
#? 1
#? ^C

[root@rocky8 ~]# PS3 = "please select relevant serial number:"
[root@rocky8 ~]# select menu in backup upgrade downgrade clean ;do true ;done
1) backup
2) upgrade
3) downgrade
4) clean
 Please select relevant serial number: 1
 Please select relevant serial number: ^C

example:

[root@rocky8 ~]# vim select.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      select3.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
PS3="Please select relevant serial number: "
select menu in backup upgrade downgrade clean; do 
    case $menu in
    backup)
        echo backup
        ;;
    upgrade)
        echo upgrade
        ;;
    *)
        echo other
        ;;
    esac
done

[root@rocky8 ~]# bash select.sh 
1) backup
2) upgrade
3) downgrade
4) clean
 Please select relevant serial number: 1
backup
 Please select relevant serial number: 2
upgrade
 Please select relevant serial number: 3
other
 Please select relevant serial number: 4
other
 Please select relevant serial number: ^C


[root@rocky8 ~]# vim select.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      select.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
PS3="Please select relevant serial number"
select menu in backup upgrade downgrade clean exit; do 
    case $REPLY in
    1)
        echo backup
        ;;
    2)
        echo upgrade
        ;;
    3)
        echo downgrade
        ;;
    4)
        echo clean
        ;;
    5)
        exit
        ;;
    *)
        echo input flase
        ;;
    esac
done

[root@rocky8 ~]# bash select2.sh 
1) backup
2) upgrade
3) downgrade
4) clean
5) exit
 Please select relevant serial number 1
backup
 Please select the relevant serial number 5

example:

[root@rocky8 ~]# vim select3.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-20
#FileName:      select2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
#
sum=0
PS3="Take your order, please(1-6): "
select MENU in Beijing roast duck Buddha jumping wall crayfish sheep scorpion hot pot order ends;do
    case $REPLY in
    1)
        echo $MENU The price is 100
        let sum+=100
        ;;
    2)
        echo $MENU The price is 88
        let sum+=88
        ;;
    3)
        echo $MENU The price is 66
        let sum+=66
        ;;
    4)
        echo $MENU The price is 166
        let sum+=166
        ;;
    5)
        echo $MENU The price is 200
        let sum+=200
        ;;
    6)
        echo "End of order,sign out"
        break
        ;;
    *)
        echo "Order error, reselect"
        ;;
    esac
done
echo "The total price is: $sum"

[root@rocky8 ~]# bash select3.sh 
1) Beijing Roast Duck
2) Steamed Abalone with Shark's Fin and Fish Maw in Broth
3) Crayfish
4) Lamb Spine Hot Pot
5) Hot Pot
6) End of order
 Take your order, please(1-6): 1
 The price of Beijing roast duck is 100
 Take your order, please(1-6): 8
 Order error, reselect
 Take your order, please(1-6): 5
 The price of hot pot is 200
 Take your order, please(1-6): 6
 End of order,sign out
 The total price is: 300

5.1 management function

Function consists of two parts: function name and function body

For help, see help function

#Syntax 1:
func_name (){
    ...Function body...
}

#Grammar 2:
function func_name {
    ...Function body...
}

#Syntax 3:
function func_name () {
    ...Function body...
}

example:

[root@rocky8 ~]# type function
function is a shell keyword

[root@rocky8 ~]# help function
function: function name { COMMANDS ; } or name () { COMMANDS ; }
    Define shell function.
    
    Create a shell function named NAME.  When invoked as a simple command,
    NAME runs COMMANDs in the calling shell's context.  When NAME is invoked,
    the arguments are passed to the function as $1...$n, and the function's
    name is in $FUNCNAME.
    
    Exit Status:
    Returns success unless NAME is readonly.
[root@rocky8 ~]# cd /etc/init.d/
[root@rocky8 init.d]# ls
functions  README
[root@rocky8 init.d]# less functions 

5.1.2 viewing functions

#View the currently defined function name
declare -F
#View currently defined function definitions
declare -f
#View and specify the currently defined function name
declare -f func_name
#View the currently defined function name definition
declare -F func_name

example:

[root@rocky8 init.d]# hello () { echo "hello,function"; }
[root@rocky8 init.d]# declare -f hello #-f display function content
hello () 
{ 
    echo "hello,function"
}

[root@rocky8 init.d]# declare -F hello #-F displays the function name
hello

[root@rocky8 init.d]# declare -F
declare -f gawklibpath_append
declare -f gawklibpath_default
declare -f gawklibpath_prepend
declare -f gawkpath_append
declare -f gawkpath_default
declare -f gawkpath_prepend
declare -f hello
[root@rocky8 init.d]# hello
hello,function

5.1.3 delete function

Format:

unset func_name

example:

[root@rocky8 init.d]# unset hello
[root@rocky8 init.d]# hello
-bash: hello: command not found

5.2 function call

Function call mode

  • Functions can be defined in an interactive environment
  • You can put a function in a script file as part of it
  • Can be placed in a separate file that contains only functions

Call: the function will be executed only when it is called. Call the function through the given function name. Where the function name appears, it will be automatically replaced with the function code

Function life cycle: created when called and terminated when returned

5.2.1 interactive environment call function

Defining and using functions in an interactive environment

example:

[root@rocky8 ~]# dir() {
> ls -l
> }
[root@rocky8 ~]# dir
total 116
-rw-------. 1 root root 1318 Oct  6 19:20 anaconda-ks.cfg

Example: to judge the major version of CentOS

[root@rocky8 init.d]# centos_version() {
> sed -rn 's#^.* +([0-9]+)\..*#\1#p' /etc/redhat-release
> }
[root@rocky8 init.d]# centos_version
8

root@ubuntu1804:~# ubuntu_version(){
> sed -rn '2s/^.*="([0-9]+)\..*/\1/p' /etc/os-release
> }
root@ubuntu1804:~# ubuntu_version
18

5.2.2 defining and using functions in scripts

The function must be defined before use, so the function definition should be placed at the beginning of the script until the shell finds it for the first time. The calling function can only use its function name

example:

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
func1(){
    echo func1 is runing
}
func1

[root@rocky8 ~]# bash test1.sh 
func1 is runing

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
func1
func1(){
    echo func1 is runing
}

[root@rocky8 ~]# bash test1.sh 
test1.sh: line 12: func1: command not found
#Functions must be defined before execution

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
func1
func1(){
    echo func1 is runing
}
func1

[root@rocky8 ~]# bash test1.sh 
test1.sh: line 12: func1: command not found
func1 is runing

5.2.3 using function files

You can save frequently used functions into a separate function file, then load the function file into the shell, and then call the function

The file name can be selected arbitrarily, but it is better to have some connection with related tasks, such as functions

Once the function file loads shell, you can call the function in the command line or script. You can use the delcare -f or set command to view all defined functions, and the 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 unset command. After making changes, reload the file

Process of implementing function file:

  1. Create a function file to store only the definition of the function
  2. Call function files in shell script or interactive shell. The format is as follows
. filename or source filename

example:

[root@rocky8 ~]# vim test2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
func1(){
    echo func1 is runing
}
func1

[root@rocky8 ~]# bash test2.sh 
func1 is runing

[root@rocky8 ~]# vim functions
func1 (){
    echo func1 is running
}
func2 (){
    echo func2 is running                                                                                   
}

[root@rocky8 ~]# vim test1.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F                                                                                                  
func1

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
func1 is running

[root@rocky8 ~]# exit
logout

[root@rocky8 ~]# declare -F
declare -f gawklibpath_append
declare -f gawklibpath_default
declare -f gawklibpath_prepend
declare -f gawkpath_append
declare -f gawkpath_default
declare -f gawkpath_prepend

5.3 function variables

Variable scope:

  • Common variable: only valid in the current shell process. A special sub shell process will be started to execute the script; Therefore, the scope of local variables is the current shell script program file, including the functions in the script
  • Environment variables: current shell and subshell are valid
  • Local variable: the life cycle of the function; The variable is automatically destroyed at the end of the function

be careful:

  • If a normal variable is defined in the function and the name is the same as the local variable, the local variable is used
  • Since ordinary variables and local variables will conflict, it is recommended to use only local variables in functions

Method for defining local variables in a function

local NAME=VALUE

example:

[root@rocky8 ~]# vim functions
func1 (){
    name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
}

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
func1
echo $name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
func1 is running
func1:name=raymond
raymond

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
name=tom
func1
echo $name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
func1 is running
func1:name=raymond
raymond

[root@rocky8 ~]# bash -x test1.sh 
+ . functions
+ declare -F
declare -f func1
declare -f func2
+ name=tom
+ func1
+ name=raymond
+ echo func1 is running
func1 is running
+ echo func1:name=raymond
func1:name=raymond
+ echo raymond
raymond

[root@rocky8 ~]# vim test1.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
func1
name=tom                                                                                
echo $name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
func1 is running
func1:name=raymond
tom

[root@rocky8 ~]# vim functions 
func1 (){
    local name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
}
#Variables defined by local can only work in function bodies

[root@rocky8 ~]# vim test1.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
name=tom
func1
echo $name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
func1 is running
func1:name=raymond
tom

[root@rocky8 ~]# vim test1.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
name=tom
echo $name
func1
echo $name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
tom
func1 is running
func1:name=raymond
tom

[root@rocky8 ~]# vim test1.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test1.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
declare -F
name=raymond
echo before function:name=$name
func1
echo agter function:name=$name

[root@rocky8 ~]# bash test1.sh 
declare -f func1
declare -f func2
before function:name=raymond
func1 is running
func1:name=raymond
agter function:name=raymond

5.4 function return value

Return value of function execution result:

  • Output using commands such as echo
  • Output result of calling command in function body

Exit status code of function:

  • The default depends on the exit status code of the last command executed in the function
  • User defined exit status code in the following format:

Return returns from the function. Use the last status command to determine the return value

return 0: no error returned

return 1-255 error returned

example:

[root@rocky8 ~]# type return
return is a shell builtin
[root@rocky8 ~]# help return
return: return [n]
    Return from a shell function.
    
    Causes a function or sourced script to exit with the return value
    specified by N.  If N is omitted, the return status is that of the
    last command executed within the function or script.
    
    Exit Status:
    Returns N, or failure if the shell is not executing a function or script.

[root@rocky8 ~]# return 10
-bash: return: can only `return' from a function or sourced script

[root@rocky8 ~]# vim test2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
func2
echo $? 

[root@rocky8 ~]# bash test2.sh 
func2 is running
0

[root@rocky8 ~]# vim functions 
func1 (){
    local name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
    exit 123                                                                              
}

[root@rocky8 ~]# bash test2.sh 
func2 is running

[root@rocky8 ~]# bash -x test2.sh 
+ . functions
+ func2
+ echo func2 is running
func2 is running
+ exit 123

[root@rocky8 ~]# vim functions 
func1 (){
    local name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
    return 123                                                                              
}

[root@rocky8 ~]# bash test2.sh 
func2 is running
123

5.5 function parameters

Function can accept parameters:

Pass parameters to the function: the given parameter list can be separated by a blank after the function name, such as testfunc arg1 arg2

In the function body, $1 can be used, 2 , . . . transfer use this some ginseng number ; still can with send use 2,... Call these parameters; You can also use 2,... Call these parameters; You can also use special variables such as @, $*, $#

example:

[root@rocky8 ~]# vim test3.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test3.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
add 2 3

[root@rocky8 ~]# vim functions 
func1 (){
    local name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
    return 123                                                                              
}
add(){
    local i=$1
    local j=$2
    echo $[i+j]
}

[root@rocky8 ~]# bash test3.sh 
5

[root@rocky8 ~]# vim test3.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test3.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
add $1 $2

[root@rocky8 ~]# bash test3.sh 5 6
11

[root@rocky8 ~]# vim functions 
func1 (){
    local name=raymond
    echo func1 is running
    echo "func1:name=$name"
}
func2 (){
    echo func2 is running
    return 123                                                                              
}
add(){
    local i=$1
    local j=$2
    echo $[i+j]
}
sub(){
    local i=$1
    local j=$2
    echo $[i-j]
}

[root@rocky8 ~]# vim test3.sh 
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      test3.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
. functions
case $1 in
add)
    add $2 $3
    ;;
sub)
    sub $2 $3
    ;;
*)
    echo input false
    ;;
esac

[root@rocky8 ~]# bash test3.sh 5 6
input false
[root@rocky8 ~]# bash test3.sh add 5 6
11
[root@rocky8 ~]# bash test3.sh sub 7 6
1

Example: realize the progress bar function

[root@rocky8 ~]# vim progress_chart.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      progress_chart.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
function print_chars(){
    # The first parameter passed in specifies the string to print
    local char="$1"
    # The second parameter passed in specifies how many times the specified string will be printed
    local number="$2"
    local c
    for ((c = 0; c < number; ++c)); do
        printf "$char"
    done
}

COLOR=32
declare -i end=50
for ((i = 1; i <= end; ++i)); do
    printf "\e[1;${COLOR}m\e[80D["
    print_chars "#" $i
    print_chars " " $((end - i))
    printf "] %3d%%\e[0m" $((i * 2))
    sleep 0.1s
done
echo

[root@rocky8 ~]# bash progress_chart.sh 
[##################################################] 100%

5.6 function recursion

Function recursion: the function calls itself directly or indirectly. Pay attention to the number of recursion layers, which may fall into an endless loop

Recursive features:

  • Function internal self call
  • There must be an exit statement to end the function

Example: recursive function call without exit

[root@rocky8 ~]# func () { let i++;echo $i;echo "func is running..,"; func; }
[root@rocky8 ~]# func

Recursive example:

Factorial is an operation symbol invented by kiston Kaman in 1808. It is a mathematical term. The factorial of a positive integer is the product of all positive integers less than or equal to the number, and the factorial of 0 and 1 is 1. The factorial of natural number n is written as n!

n!=1×2×3×...×n

Factorials can also be defined recursively: 0= 1,n!=(n-1)! × n

n!=n(n-1)(n-2)...1

n(n-1)! = n(n-1)(n-2)!

Example: fact.sh

[root@rocky8 ~]# vim fact.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      fact.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
fact(){
    if [ $1 -eq 1 ];then
        echo 1
    else
        echo $[$1*$(fact $[$1-1])]
    fi    
}
fact $1

[root@rocky8 ~]# bash fact.sh 5
120

[root@rocky8 ~]# vim fact2.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      fact2.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
fact(){
    local num=$1
    if [[ $num -eq 0 ]];then
        fac=1
    else
        dec=$((num-1))
        fact $dec
        fac=$((num*fac))
    fi
}
fact $1
echo "$1 The factorial of is:$fac"

[root@rocky8 ~]# bash fact2.sh 5
5 The factorial of is:120

Fork bomb is a malicious program. Its interior is an infinite loop constantly in the fork process. In essence, it is a simple recursive program. Because the program is recursive, if there are no restrictions, this will cause this simple program to quickly exhaust all the resources in the system

reference resources: https://en.wikipedia.org/wiki/Fork_bomb

function

:(){ :|:& };:
bomb() { bomb | bomb & }; bomb

Script implementation

[root@rocky8 ~]# :(){ :|:& };:
[1] 8722

[root@rocky8 ~]# vim Bomb.sh
#!/bin/bash
#
#**********************************************************************************************
#Author:        Raymond
#QQ:            88563128
#Date:          2021-10-21
#FileName:      Bomb.sh
#URL:           raymond.blog.csdn.net
#Description:   The test script
#Copyright (C): 2021 All rights reserved
#*********************************************************************************************
./$0|./$0&

5.7 practice

  1. Write functions to realize OS version judgment
  2. Write a function to get the IP address of the current system eth0
  3. Write functions to print green OK and red FAILED
  4. Write a function to judge whether there is no position parameter. If there is no parameter, an error will be prompted
  5. Write a function that takes two numbers as parameters and returns the maximum value
  6. Write the service script / root/bin/testsrv.sh to complete the following requirements
    (1) The script can accept parameters: start, stop, restart, status
    (2) If the parameter is not one of the four, you will be prompted to use the format and exit with an error
    (3) If start: create / var/lock/subsys/SCRIPT_NAME and displays "start successfully"
    Consider: if it has been started once in advance, what should I do?
    (4) If stop: delete / var/lock/subsys/SCRIPT_NAME and displays stop complete
    Consider: if it has been stopped in advance, how to deal with it?
    (5) If it is restart, stop first and then start
    Consider: if there is no start, how to deal with it?
    (6) If it is status, / var / lock / subsys / script_ If the name file exists, "SCRIPT_NAME is running..." is displayed, if / var / lock / subsys / script_ If the name file does not exist, "SCRIPT_NAME is stopped..."
    (7) It is forbidden to start the service in all modes. It can be managed by chkconfig and service commands
    Description: SCRIPT_NAME is the current script name
  7. Script / root/bin/copycmd.sh
    (1) Prompt the user for an executable command name
    (2) Gets a list of all library files that this command depends on
    (3) Copy the command to the corresponding path under a target directory (for example, / mnt/sysroot)
    For example: / bin / bash = = > / MNT / sysRoot / bin / Bash
    /usr/bin/passwd ==> /mnt/sysroot/usr/bin/passwd
    (4) Copy all the library files that this command depends on to the corresponding path in the target directory: for example: / lib64 / LD linux-x86-64. So. 2 = = > / MNT / sysRoot / lib64 / LD linux-x86-64. So. 2
    (5) After each command is copied, do not exit, but prompt the user to type a new command to copy, and repeat the above functions; Until the user enters quit to exit
  8. Fibonacci sequence is also called golden section sequence. It is also called "rabbit sequence" because mathematician Leonardo Fibonacci took rabbit breeding as an example. It refers to such a sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,.... Fibonacci sequence is defined recursively as follows: F (0) = 0, f (1) = 1, f (n) = F(n-1)+F(n-2) (n ≥ 2) , use the function to find the n-order Fibonacci sequence
  9. The problem of Hanoi Tower (also known as river tower) originates from an ancient legend in India. When Brahma created the world, he made three diamond pillars. On one pillar, 64 gold discs were stacked in order of size from bottom to top. Brahma ordered Brahman to rearrange the disk on another column in order of size from below. It is also stipulated that the disk cannot be enlarged on the small disk, and only one disk can be moved between the three columns at a time. Using the function, the moving steps of the Hanoi tower with N disks are realized

Tags: Linux Operation & Maintenance architecture DevOps Cloud Native

Posted on Sun, 07 Nov 2021 17:24:37 -0500 by warren