2.shell Programming - Advanced Usage function

2.1 Definitions and use functions

Use basic functions

[root@VM_0_9_centos ~]# test()
> {}
-bash: syntax error near unexpected token `{}'
[root@VM_0_9_centos ~]# test() {}
-bash: syntax error near unexpected token `{}'
[root@VM_0_9_centos ~]# test() 
> {
>     echo "test function"
> }
[root@VM_0_9_centos ~]# test
test function
[root@VM_0_9_centos ~]# function greeting
> {
>     echo "hello world"
> }
[root@VM_0_9_centos ~]# greeting 
hello world
[root@VM_0_9_centos ~]# 

Example one: write a daemon, nginx if you turn off the automatic open

vim nginx_daemon.sh

#!/bin/bash
#

# Process id running the script, if the script name has nginx words, this also needs to filter out
this_pid=$$

while true
do

ps -ef | grep nginx | grep -v grep | grep -v $ this_pid &> / dev / null

if [ $? -eq 0 ];then
    echo "Nginx is running well!"
    sleep 3
else
    systemctl start nginx
    echo "Nginx is down,start it....."
be
done

Put this script in the background

nohup sh nginx_daemon.sh &

After closing the View

tail -f nohup.out

2.2. Passing arguments to functions

the shell parameter passing

function name
{
    echo "hello $1"
    echo "hello $2"
}

Function call

name derek alice

For example

[root@VM_0_9_centos shell_learn]# function greeting
> {
>     echo "Hello $1"
> }
[root@VM_0_9_centos shell_learn]# 
[root@VM_0_9_centos shell_learn]# greeting derek
Hello derek
[root@VM_0_9_centos shell_learn]# greeting alice
Hello alice
[root@VM_0_9_centos shell_learn]# 

2.3. The return value of the function

Return by value

Method 1: return

Method Two: echo

Use return to return value

  • Use return to return a value, can only return an integer 1-255
  • Using the return value of the function return is usually only used for the call to get the rest state, typically only returns 0 or 1; 0 on success, 1 indicates failure

Use echo return value

  • Use string echo can return any results
  • Commonly used to return data, such as a string value or list of values

Example one

#!/bin/bash
#

this_pid=$$

function is_nginx_running
{
    ps -ef | grep nginx | grep -v grep | grep -v $ this_pid &> / dev / null

    if [ $? -eq 0 ];then
            return
    else
        return 1
    be
}

is_nginx_running && echo "nginx is runnig...." || echo "nginx is stop!"

 Example Two: Get a list of users

#!/bin/bash
#

function get_users
{
    users=`cat /etc/passwd | cut -d: -f1`
    echo $users
}

user_list=`get_users`

index=1

for user in $user_list
do
    echo "The $index user is: $user"
    index=$(($index+1))
done

2.4 The local and global variables

Global Variables

  • No special statement, shell variables are global variables
  • Large script function in a global variable with caution

Local variables

  • When you define a variable, use local keywords
  • The presence of the same inner and outer functions function variables, external variables inside a function of coverage function

2.5. Library

Library

  • Repeating frequently used code as a function of the package file
  • Generally not performed directly, but called by other scripts
  • Suffix library file name is arbitrary, but generally use .lib
  • Library files are typically not executable options
  • Without having to specify the library file and script, just references in the script in the same directory

 

Guess you like

Origin www.cnblogs.com/derek1184405959/p/11099961.html