Функции shell в linux

Функции shell в linux

Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a «regular» command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed. Shell functions are executed in the current shell context; no new process is created to interpret them.

Functions are declared using this syntax:

fname () compound-command [ redirections ]
function fname [()] compound-command [ redirections ]

This defines a shell function named fname . The reserved word function is optional. If the function reserved word is supplied, the parentheses are optional. The body of the function is the compound command compound-command (see Compound Commands). That command is usually a list enclosed between < and >, but may be any compound command listed above. If the function reserved word is used, but the parentheses are not supplied, the braces are recommended. compound-command is executed whenever fname is specified as the name of a simple command. When the shell is in POSIX mode (see Bash POSIX Mode), fname must be a valid shell name and may not be the same as one of the special builtins (see Special Builtins). In default mode, a function name can be any unquoted shell word that does not contain ‘ $ ’. Any redirections (see Redirections) associated with the shell function are performed when the function is executed. A function definition may be deleted using the -f option to the unset builtin (see Bourne Shell Builtins).

The exit status of a function definition is zero unless a syntax error occurs or a readonly function with the same name already exists. When executed, the exit status of a function is the exit status of the last command executed in the body.

Note that for historical reasons, in the most common usage the curly braces that surround the body of the function must be separated from the body by blank s or newlines. This is because the braces are reserved words and are only recognized as such when they are separated from the command list by whitespace or another shell metacharacter. Also, when using the braces, the list must be terminated by a semicolon, a ‘ & ’, or a newline.

When a function is executed, the arguments to the function become the positional parameters during its execution (see Positional Parameters). The special parameter ‘ # ’ that expands to the number of positional parameters is updated to reflect the change. Special parameter 0 is unchanged. The first element of the FUNCNAME variable is set to the name of the function while the function is executing.

All other aspects of the shell execution environment are identical between a function and its caller with these exceptions: the DEBUG and RETURN traps are not inherited unless the function has been given the trace attribute using the declare builtin or the -o functrace option has been enabled with the set builtin, (in which case all functions inherit the DEBUG and RETURN traps), and the ERR trap is not inherited unless the -o errtrace shell option has been enabled. See Bourne Shell Builtins, for the description of the trap builtin.

Читайте также:  Openvpn client autostart linux

The FUNCNEST variable, if set to a numeric value greater than 0, defines a maximum function nesting level. Function invocations that exceed the limit cause the entire command to abort.

If the builtin command return is executed in a function, the function completes and execution resumes with the next command after the function call. Any command associated with the RETURN trap is executed before execution resumes. When a function completes, the values of the positional parameters and the special parameter ‘ # ’ are restored to the values they had prior to the function’s execution. If a numeric argument is given to return , that is the function’s return status; otherwise the function’s return status is the exit status of the last command executed before the return .

Variables local to the function may be declared with the local builtin (local variables). Ordinarily, variables and their values are shared between a function and its caller. These variables are visible only to the function and the commands it invokes. This is particularly important when a shell function calls other functions.

In the following description, the current scope is a currently- executing function. Previous scopes consist of that function’s caller and so on, back to the «global» scope, where the shell is not executing any shell function. Consequently, a local variable at the current local scope is a variable declared using the local or declare builtins in the function that is currently executing.

Local variables «shadow» variables with the same name declared at previous scopes. For instance, a local variable declared in a function hides a global variable of the same name: references and assignments refer to the local variable, leaving the global variable unmodified. When the function returns, the global variable is once again visible.

The shell uses dynamic scoping to control a variable’s visibility within functions. With dynamic scoping, visible variables and their values are a result of the sequence of function calls that caused execution to reach the current function. The value of a variable that a function sees depends on its value within its caller, if any, whether that caller is the «global» scope or another shell function. This is also the value that a local variable declaration «shadows», and the value that is restored when the function returns.

For example, if a variable var is declared as local in function func1 , and func1 calls another function func2 , references to var made from within func2 will resolve to the local variable var from func1 , shadowing any global variable named var .

Читайте также:  Wifi модуль на линукс

The following script demonstrates this behavior. When executed, the script displays

In func2, var = func1 local
func1() < local var='func1 local' func2 >func2() < echo "In func2, var = $var" >var=global func1

The unset builtin also acts using the same dynamic scope: if a variable is local to the current scope, unset will unset it; otherwise the unset will refer to the variable found in any calling scope as described above. If a variable at the current local scope is unset, it will remain so (appearing as unset) until it is reset in that scope or until the function returns. Once the function returns, any instance of the variable at a previous scope will become visible. If the unset acts on a variable at a previous scope, any instance of a variable with that name that had been shadowed will become visible (see below how localvar_unset shell option changes this behavior).

Function names and definitions may be listed with the -f option to the declare ( typeset ) builtin command (see Bash Builtin Commands). The -F option to declare or typeset will list the function names only (and optionally the source file and line number, if the extdebug shell option is enabled). Functions may be exported so that child shell processes (those created when executing a separate shell invocation) automatically have them defined with the -f option to the export builtin (see Bourne Shell Builtins).

Functions may be recursive. The FUNCNEST variable may be used to limit the depth of the function call stack and restrict the number of function invocations. By default, no limit is placed on the number of recursive calls.

Источник

Unix / Linux — Shell Functions

In this chapter, we will discuss in detail about the shell functions. Functions enable you to break down the overall functionality of a script into smaller, logical subsections, which can then be called upon to perform their individual tasks when needed.

Using functions to perform repetitive tasks is an excellent way to create code reuse. This is an important part of modern object-oriented programming principles.

Shell functions are similar to subroutines, procedures, and functions in other programming languages.

Creating Functions

To declare a function, simply use the following syntax −

The name of your function is function_name, and that’s what you will use to call it from elsewhere in your scripts. The function name must be followed by parentheses, followed by a list of commands enclosed within braces.

Example

Following example shows the use of function −

#!/bin/sh # Define your function here Hello () < echo "Hello World" ># Invoke your function Hello

Upon execution, you will receive the following output −

Pass Parameters to a Function

You can define a function that will accept parameters while calling the function. These parameters would be represented by $1, $2 and so on.

Читайте также:  Расшифровка прав доступа linux

Following is an example where we pass two parameters Zara and Ali and then we capture and print these parameters in the function.

#!/bin/sh # Define your function here Hello () < echo "Hello World $1 $2" ># Invoke your function Hello Zara Ali

Upon execution, you will receive the following result −

$./test.sh Hello World Zara Ali

Returning Values from Functions

If you execute an exit command from inside a function, its effect is not only to terminate execution of the function but also of the shell program that called the function.

If you instead want to just terminate execution of the function, then there is way to come out of a defined function.

Based on the situation you can return any value from your function using the return command whose syntax is as follows −

Here code can be anything you choose here, but obviously you should choose something that is meaningful or useful in the context of your script as a whole.

Example

Following function returns a value 10 −

#!/bin/sh # Define your function here Hello () < echo "Hello World $1 $2" return 10 ># Invoke your function Hello Zara Ali # Capture value returnd by last command ret=$? echo "Return value is $ret"

Upon execution, you will receive the following result −

$./test.sh Hello World Zara Ali Return value is 10

Nested Functions

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

Following example demonstrates nesting of two functions −

#!/bin/sh # Calling one function from another number_one () < echo "This is the first function speaking. " number_two >number_two () < echo "This is now the second function speaking. " ># Calling function one. number_one

Upon execution, you will receive the following result −

This is the first function speaking. This is now the second function speaking.

Function Call from Prompt

You can put definitions for commonly used functions inside your .profile. These definitions will be available whenever you log in and you can use them at the command prompt.

Alternatively, you can group the definitions in a file, say test.sh, and then execute the file in the current shell by typing −

This has the effect of causing functions defined inside test.sh to be read and defined to the current shell as follows −

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

To remove the definition of a function from the shell, use the unset command with the .f option. This command is also used to remove the definition of a variable to the shell.

Источник

Оцените статью
Adblock
detector