Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Shell Programming points to note about the function     - CentOS6.0 successful installation and configuration OpenCV (Linux)

- Linux compiler of GCC (Linux)

- Modify grub solve computer startup error: ERROR 17 (Linux)

- High-performance JavaScript DOM programming (Programming)

- Linux ldconfig command (Linux)

- Easily create RPM packages using the FPM (Linux)

- sqlplus connect to Oracle (Database)

- Windows Server 2012 R2 Datacenter install SQL Server 2016 CTP (Database)

- Internet proxy workaround can not be used under Linux YUM (Linux)

- Teach you the Ubuntu Server enabled SSH multifactor authentication (Server)

- How do you temporarily emptied Bash environment variable before running the command on Linux (Linux)

- Based LNMP + uWSGI deploy Django applications (Server)

- Ubuntu in Vim editor display processing method Chinese garbled (Linux)

- Grub2 Boots the openSUSE installation image (Linux)

- The correct way of logical backup mysqldump (Database)

- ActiveMQ5.10.2 version configuration JMX (Linux)

- Spring3 + SpringMVC + Hibernate4 full annotation environment configuration (Server)

- Varnish configuration language VCL and its built-in variables (Server)

- Novice Advanced Ubuntu7.10 configured IP address (Linux)

- XP virtual machine under VirtualBox solve occupy 100% CPU problem (Linux)

 
         
  Shell Programming points to note about the function
     
  Add Date : 2018-11-21      
         
       
         
  This morning a friend suddenly asked to web site, shell programming function Why return only integer, how can the implementation of the results returned by the function and saved to a variable. Actually, this is a good solution --- just need a good understanding of the shell can function. The following is based chats finishing a few points on the function:

Several shell programming functions need to know about

 First, whether it is inside a function or outside the function, the value of $ 0 is the script itself.

    [Root @ target ~] # cat test.sh
    #! / Bin / bash
    
    echo $ 0
    
    function testFunc () {
        echo "In function:"
        echo "\ $ 0 = $ 0"
        echo "\ $ 1 = $ 1"
    }
    
    testFunc 'test test'
    [Root @ target ~] # bash test.sh
    test.sh
    In function:
    $ 0 = test.sh
    $ 1 = test test

 Second, the variables in the function definition (if it is not necessary to modify the global variables used) to make use of local keyword defined as a local variable, so as not to override the global value of the variable

    
[Root @ target ~] # cat local.sh
    #! / Bin / bash
    
    TESTDATA = "Hello world."
    TESTDATA1 = "Hello shell."
    
    function localFunc () {
        TESTDATA = "Hello python."
        local TESTDATA1 = "Hello PHP."
        echo "In function:"
        echo "\ $ TESTDATA = $ TESTDATA"
        echo "\ $ TESTDATA1 = $ TESTDATA1"
    }
    
    #call function
    localFunc
    
    echo "Out of function:"
    echo "\ $ TESTDATA = $ TESTDATA"
    echo "\ $ TESTDATA1 = $ TESTDATA1"
    [Root @ target ~] # bash local.sh
    In function:
    $ TESTDATA = Hello python.
    $ TESTDATA1 = Hello PHP.
    Out of function:
    $ TESTDATA = Hello python.
    $ TESTDATA1 = Hello shell.

 Third, the use of the function return keyword returns a value of 0-255, indicates that the function exit status code (that is, whether the function is executed successfully), 0 on success, nonzero means failure. In their daily work, you can make the function returns whether they were successful according to the conditions for making a judgment about the use of other programs. If desired function returns a result value, and stores it in a variable, use echo statements.

    [Root @ target ~] # cat return.sh
    #! / Bin / bash
    
    function returnFunc () {
        echo "value"
        return 247
    }
    
    DATA = $ (returnFunc)
    echo "\ $? = $?"
    echo "\ $ DATA = $ DATA"
    [Root @ target ~] # bash return.sh
    $? = 247
    $ DATA = value

 

Fourth, if you have more than one shell script, a script needs to call another script function, you need to execute the following command to load the specified script file:

. ./func.sh

or

source ./func.sh

The operation is similar to the top include the operation of other languages.

 

  [Root @ target ~] # cat func.sh
    #! / Bin / bash
    
    function func1 () {
        echo "This is func1"
    }
    
    function func2 () {
        echo "This is func2"
    }
    
    function func3 () {
        echo "This is func3"
    }
    [Root @ target ~] # cat call.sh
    #! / Bin / bash
    
    #source ./func.sh
    . ./func.sh
    
    func1
    func2
    func3
    [Root @ target ~] # bash call.sh
    This is func1
    This is func2
    This is func3
     
         
       
         
  More:      
 
- Varnish achieve page jump (Server)
- How to upgrade to Ubuntu 14.04 Ubuntu 14.10 (Linux)
- Ubuntu 12.04 kernel configuration automatically restart and crash dump (Linux)
- SaltStack installation and testing (Server)
- Ubuntu 14.10 / 14.04 how to install Quick Start tool Mutate 2.2 (Linux)
- Fedora 22 Server how to upgrade to Fedora 23 Beta Server (Linux)
- Ubuntu install video playback software SMPlayer 14.9.0.7042 (Linux)
- Java implementation of stacks and queues (Programming)
- ORA-00824: can not set sga_target due to existing problem-solving (Database)
- Ubuntu 14.04 installed Nvidia CUDA 7.5 and build Python Theano deep learning development environment (Linux)
- Java String type time compare the size (Programming)
- RedHat Performance Tuning (Server)
- Ubuntu 15.04 Linux install anti-virus software Antiviral 0.2 (Linux)
- CentOS 6 Install Xen4.2 Virtualization Practice (Linux)
- Linux 4.0+ kernel support for hardware switching module (HW Switch Offload) (Linux)
- Red Hat Enterprise Linux Server 6.5 installation GCC 4.9.2 (Linux)
- BackTrack (BT3, BT4) Linux installation tutorial (Linux)
- Linux kernel netpoll framework netconsole (Linux)
- Acting on JavaScript events (Programming)
- Android Touch message passing mechanism analysis (Programming)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.