Shell Function Basics

Source: Internet
Author: User

Important notes about the function:

When executing the Shell function, the function name and the parentheses after the function name are not required;

The definition of a function must be defined or loaded before the executing program

The execution order of the various programs in the Shell Execution system is: System Alias------system command----executable file

When the function executes, the variables are shared with the script that invokes it, and local variables and special positional parameters can be set for the function

Inside the shell function, the return command functions like exit, and return is the function of exit, and exit is the exit script file

The return statement returns an exit value to the current program that called the function, and exit returns an exit value to the executing program's current shell

If the function is stored in a separate file and is used by the script, the source or "." will be used. To load

Local variables are typically defined within a function, and these variables disappear after they leave the function



function execution method with parameters:

Function Name parameter 1 parameter 2

Description of the function followed by the parameter:

The shell's positional parameters ($1,$2...$#,$?,$*,[email protected]) can be used as arguments to the function

At this point the parameters of the parent script are temporarily obscured or hidden by the function arguments

$ A is special, it is still the name of the parent script.

When the function executes, the parameters of the original command-line script can be restored.

The parameter variables of a function are defined within the body of the function.

#!/bin/basholdboy () {echo "I am Oldboy"}function xpg {echo "I am XPG"}oldboyxpg~

The script file separating the function body and executing the function (more canonical method)

Create a function library script first (no function is performed by default)

Append multiple lines of text with the Cat command to append the function code to the system's function file, which is/etc/init.d/function.

[[email protected] ~]# cat >> /etc/init.d/functions <<-eof   oldboy ()  {   echo  "I am oldboy"}eof  # #这里的EOF可以使用Tab健, not shelf, However, you cannot use the space # #不知道在这个文件里面为什么不能调用, so use the test file to write out the shell function with parameters [[Email protected] ~]# cat test old ()  {echo  "i am oldgirl,you are $1"}[[email protected] ~]# cat  7.sh #!/bin/bash[ -f /root/test ] && source /root/test | |  echo 5old xiaoting[[email protected] ~]# sh 7.sh I am  oldgirl,you are xiaoting[[email protected] ~]#  example 8-4  to convert a function parameter to a script file command line parameter [[email]  protected] ~]# cat test old ()  {echo  "I am oldgirl,you are  $1 "}[[EMAIL&NBSP;PROTECTED]&NBSP;~]#&NBSP;CAT&NBSP;7.SH&NBSP;#!/BIN/BASH[&NBSP;-F&NBSP;/ROOT/TEST&NBSP;]  && source /root/test | |  echo 5old $1[[email protected] ~]# sh 7.sh xiaoting i am  oldgirl,you are xiaoting[[email protected] ~]#

8.5 Developing enterprise-level URL detection scripts with Shell functions

Example 8-5: script file command-line parameter to determine if an exception exists for any specified URL.

#!/bin/bashif [$#-ne 1]; then echo "Plz usage:$0 URL" exit 3fiwget-t--tries=2 $ >/dev/nulltty=$?if [$tty-eq 0]; Then echo the "is yes" else echo "$ is no" fi~

The above detection function is written into a function and the function parameter is converted into a script command-line parameter to determine whether any specified URL has an exception

Self-written: #!/bin/bashaa () {if [$#-ne 1]; then echo "Plz usage:$0 URL" Exit 3fi}bb () {wget-t--tries=2 $ >/dev/null tty=$?if [$tty-eq 0]; Then echo "was yes" else echo "$ is no" FI}AA $*bb $* official (more direct and authoritative): #!/bin/bashaa () {echo "plz usage:$0 URL"}bb () {wget -T ten--tries=2 $ >/dev/nulltty=$?if [$tty-eq 0]; Then echo "was yes" else echo "$ is no" fi}main () {if [$#-ne 1] then aa fi bb $1}main $*

After learning the function, try to modularize the scripting function, each module implements a function, and allows the script to be generic.


Example 8-6: Converting the function's arguments to a script file command-line parameter, judging whether any specified URL is an exception, and displaying it in a more professional output;

#!/bin/bash.  /etc/init.d/functions #引入系统函数库aa () {echo "plz usage:$0 URL"}bb () {wget-t--tries=2 $1-o/dev/nulltty=$?if [$tty -eq 0]; Then action "was yes"/bin/true #这里的action就是在脚本开头引入系统函数库后调用的else action "is No"/bin/falsefi}main () {if [$# -ne 1] then aa fi bb $1}main $*~

Shell Function Basics

Related Article

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.