Shell Tutorial (vi): Functions, online Help _shell

Source: Internet
Author: User


Functions allow you to break down into smaller, logical segments, and then be required to perform tasks that require the overall functionality of a script.



Using functions to perform repetitive tasks is a good way to create reuse of code. Code reuse is an important part of the principles of modern object-oriented programming.



Shell functions are subroutines, procedures, and functions similar to those in other programming languages. To create a function:



To declare a function, simply use the following syntax:



List of commands
}


Function name Function_name, which is what you will use to call it from elsewhere in your script. Function names must follow the list of commands within parentheses, in parentheses. For example:



Here's a simple example of using a function:


#!/bin/sh


# Define your function here

Hello () {

echo "Hello World"

}


# Invoke your function

Hello


When you want to execute the above script, it produces the following results:


$./test.sh
Hello World
$
Parameter passed to the function:


You can define a function that takes parameters and calls those functions. These parameters are represented $1,$2, and so on.



Here is an example where we pass two parameters to Zara and Ali, and then we capture and print these parameter functions.


#!/bin/sh

# Define your function here
Hello () {
echo "Hello World $"
}

# Invoke your function
Hello Zara Ali


This will produce the following results:


$./test.sh
Hello World Zara Ali
$
From the return value of the function:


If you execute an exit command from within a function, the effect is not only to terminate the execution of the function, but also to call the function in the shell program.



If you don't want to, just terminate the execution of the function, and then there is a defined function that exits.



Depending on the actual situation, you can return any value from your function, using the returned command with the following syntax:


Return code


The code here can be anything you choose here, but obviously, you should choose your script as a whole context that is meaningful or useful. Example:



The following function returns a value of 1:


#!/bin/sh

# Define your function here
Hello () {
echo "Hello World $"
Return 10
}

# Invoke your function
Hello Zara Ali

# Capture value returnd by last command
Ret=$?

echo "Return value is $ret"


This will produce the following results:


$./test.sh
Hello World Zara Ali
return value is 10
$
Nested functions:


One of the more interesting features of a function is that they can call itself and call other functions. A function called a recursive function that calls itself.



A simple example illustrates a nested two function:


#!/bin/sh

# Calling one function from another
Number_one () {
echo "This is the" "The" the "" "," "", "speaking.
Number_two
}

Number_two () {
echo "This are now the second function speaking ..."
}

# calling function one.
Number_one


This will produce the following results:


This is the speaking ...
This is the second function speaking ...
Call from the prompt function:


You can use the definitions of common functions. Profile so that they will log in every time, at the command prompt, you can work with them.



Alternatively, you can group the definition in a file into test.sh, and then execute the file by typing in the current shell:


$. test.sh


The effect of doing so results in any function defined within the test.sh, which can be read in the following definition as the current shell:


$ number_one
This is the speaking ...
This is the second function speaking ...
$


To remove the definition from a shell function, you can use the unset command. F option. This is the same command to remove the definition shell of a variable.


$unset. F function_name








All UNIX commands come with some optional and mandatory options. It is very common to forget the full syntax of these commands.



Since no one remembers every Unix command and option, it has been providing online help since early in UNIX.



The UNIX version of the Help file, called the man page. If you know the name of any command, but you do not know how to use it, then the manual page will help you. Grammar



Here is a simple command to get the system working, and the details of any UNIX command:


$man command
Example:


Now, you can imagine any command that you want to get help. Suppose you want to know about PWD, then you just need to use the following command:


$man pwd


The above command will open a complete message that will help you to give you the PWD command. Try it yourself at your command prompt to get more details



You can get complete details on the man command itself using the following command:


$man Mans
Manual Pages section:


The manual page is generally divided into sections, and the General man page author prefers change. Here are some of the more common sections:



Part



Describe



NAME



Name of the command



Synopsis



General usage parameters of the command.



DESCRIPTION



Generally describes of the command and what it does



OPTIONS



Describes all of the arguments or options to the command



ALSO



Lists other commands that are directly related to the "command in the" man page or closely resembling its functionality.



BUGS



Explains any known issues or bugs this exist with the command or its output



Examples



Common usage examples that give reader a idea of how the command can be used.



AUTHORS



The author of the man Page/command.



Finally, I would like to say that the manual page is an important research resource and the first way when you need information on UNIX system commands or files. Useful Shell commands:



Now that you know how to proceed, the link will give you a list of the most important and frequently used Unix shell commands.



If you don't know how to use any of the commands, then use the man page to get complete details about the command.



Here's the list shell-useful commands






from:http://www.yiibai.com/shell/what_is_shell.html#


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.