Introduction and use of Unix shell udfs

Source: Internet
Author: User
Tags chop create directory

Introduction to Unix shell user-defined functions and use 1. functions without parameters and without parameters can be directly called to implement some functions. The function is written in scripts and stored together with other commands, but the function must be defined at the beginning of the script. That is, in the script containing the function, all functions must be defined at the beginning of the script, and then called after the function is defined or referenced in other scripts. Www.2cto.com instance 1. The following is a simple custom function. Calculate the sum of 1 to 10: [plain] pg no_param_test #! /Bin/ksh # test the custom function www.2cto.com # author: _ yeeXun # date: 8:37:29 no_param_test () {SUM = 0 # for I in {1 .. 10} for I in 1 2 3 4 5 6 7 8 9 10 do echo $ I SUM = 'expr $ SUM + $ I 'I = 'expr $ I + 1' if [$ I-eq 11]; then echo "Sum: $ SUM" fi done} no_param_test # EOF execution Script: sh no_param_test12345678910Sum: 55 II. Custom functions with parameters in advanced languages (such as C, in JAVA, C #, etc.), parameters are in brackets behind the function; in shell, the number of parameters is checked in the function body. When calling a function, follow the parameter directly. No matter how many parameters there are, they all follow the function name. 1. Single Parameter instance 2. Script: [plain] pg direc_check #! /Bin/ksh is_directory () {_ DIR_NAME = $1 # check params if [$ #-lt 1]; then echo "is_directory: I need a directory name to check "return 1 fi # check if [! -D $ _ DIR_NAME]; then return 1 else return 0 fi} error_msg () {echo "***********" echo $ @ echo "***********" return 0} echo-n "Enter destination directory: "read DIR if is_directory $ DIR then echo" $ DIR is a directory and exists in $ PWD "else error_msg" $ DIR does not exist... creating it now "mkdir $ DIR>/dev/null 2> & 1 if [$? -Ne 0]; then error_msg "cocould not create directory: chech it out! "Exit 1 else: fi # author: _ yeeXun # date: 15:27:57 # EOF here the # check params section of the function is for parameter check, $ # indicates the number of input parameters. Run the following Script: sh direc_checkEnter destination directory: test *********** test does not exist... when you run creating it now ********** again, enter the same name, and the system will prompt that the directory already exists. Sh direc_checkEnter destination directory: testtest is a directory and exists in/usr/b4nx/user/ytcclb. We are adding a directory and viewing the directory created by this script in the current directory using the awk language: [plain] ls-l | awk '{if ($0 ~ /^ D /) print $0} 'drwxr-xr-x 2 b4nx group 512 Mar 2 call_fun_test drwxr-xr-x 2 b4nx group 512 Mar 2 test 2. Multiple parameters, whether single parameter or not, check and determine whether multiple parameters are used. $ # indicates the number of input parameters. $1 indicates the first parameter, $2 indicates the second parameter, and $3 indicates the fourth parameter, and so on. The following is a string truncation function: instance 3, pg chop [plain] #! /Bin/ksh # chop # to call: chop string how_many_chars_to_chop chop () {_ STR = $1 _ CHOP = $2 # awk's substr starts at 0, wo need to increment by one CHOP = 'expr $ _ CHOP + 1' # check we have two params if [$ #-ne 2]; then echo "check_length: I need a string and how many characters to chop "return 1 fi # check the length of the string first _ LENGTH = 'echo $ _ STR | awk '{print length ($0)} ''if ["$ _ LENGTH"-lt "$ _ CHOP"]; then echo "The length of the string is short" return 1 fi echo $ _ STR | awk '{print substr ($1, '$ _ CHOP')} '} # call the function echo-n "Enter the string:" read STR echo-n "Enter the start position: "read LEN chop $ STR $ LEN # author: _ yeeXun # date: 14:17:34 # EOF this script, a function named" chop "intercepts a string with two parameters, $1 indicates the string to be intercepted, and $2 indicates the starting position of the string to be intercepted. Run the following command: sh chopEnter the string: string123Enter the start position: 5ng123 sh chopEnter the string: _ yeexunInCSDNEnter the start position: 10CSDN 3. the return value of the function returned value indicates the status of the function, 1. return 0: normal return. 2. return 1: return Error. 3. return: The final command execution status determines the return value. You can use $? Detection. Iv. function call 1. In the same script, the function must be defined at the beginning of the script and can be called after definition. In the same script, define the function at the top and call it after the definition, such as instance 1, instance 2, and instance 3. 2. You can define these functions in the same script as a common function script, and then reference the script in the called script. Reference Method: <dot> <space> <directory> <script> instance 4. function call-Cross-script Function script: [plain] pg direc_check.sh #! /Bin/ksh is_directory () {_ DIR_NAME = $1 # check params if [$ #-lt 1]; then echo "is_directory: I need a directory name to check "return 1 fi # check if [! -D $ _ DIR_NAME]; then return 1 else return 0 fi} error_msg () {echo "***********" echo $ @ echo "***********" return 0} # multiline comment method :: <'... ': <'echo-n "Enter destination directory: "read DIR if is_directory $ DIR then echo" $ DIR is a directory and exists in $ PWD "else error_msg" $ DIR does not exist... creating it now "mkdir $ DIR>/dev/null 2> & 1 if [$? -Ne 0]; then error_msg "cocould not create directory: chech it out! "Exit 1 else: fi '# author: _ yeeXun # date: 15:27:57 # EOF the script that calls the function in this script: [plain] pg fun_test #! /Bin/ksh # call the function outside the script is_drectory # <dot> <space> <directory with functions> <script with functions>. $ HOME/user/ytcclb/direc_check.sh echo-n "Enter destination directory: "read DIR if is_directory $ DIR then echo" $ DIR is a directory and exists in $ PWD "else error_msg" $ DIR does not exist... creating now "mkdir $ DIR>/dev/null 2> & 1 if [$? -Ne 0]; then error_msg "cocould not create directory: check it out" exit 1 else: fi # author: _ yeeXun # date: 15:49:12 # EOF when entering an existing directory: sh fun_testEnter destination directory: testtest is a directory and exists in/usr/b4nx/user/ytcclb enter a directory name that does not exist: sh fun_testEnter destination directory: dir_test ********** dir_test does not exist... creating now *********** view all directory names in the current directory: [plain] ls-l | awk '{ If ($0 ~ /^ D /) print $0} 'drwxr-xr-x 2 b4nx group 512 Mar 2 call_fun_test drwxr-xr-x 2 b4nx group 512 Mar 4 dir_test drwxr-xr-x 2 b4nx group 512 mar 2 :04 test -- the end --

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.