Shell function "parameter passing and input output" & built-in functions

Source: Internet
Author: User
Tags echo command function definition numeric value

Linux--shell Script Foundation 3:shell function "parameter passing and input output" & built-in functions

function definition 1

Exit Status 1

Parameter Passing 2

Standard IO 2

Script Debugging 2

And&or 3

Built-in command Supplement 3

function definition

function definition:

In the shell, a function is a set of command sets or statements that form an available block

function enhances the ability of the shell to be programmable

Composition

Function_name () {

Statements

}

Function name (must be unique in a script)

function body (command set)

Attention

Forward declaration does not exist in the shell (first defined and used)

It must be defined before a function is called

Functions can define their own variables used internally: Local

Call a function as you would with a command

eg

function CopyFile ()

{

Local srcfile=$1

Local dstfile=$2

CP $srcfile $dstfile

Return 0 # Always return success

}

Copyfile/tmp/myconf/etc/sysconf

Call

Function_name params

The function can return a numeric value with the return command, or echo a string to return

Parameter passing

Parameter passing:

Parameters use the same as positional parameters

$, $, ...

[email protected], $*

$#

Func $var 1 $var 2

Note: The middle of the variable is separated by a space, tab

return value

Return: Used primarily to return exit status, i.e. $?

0 <= return value (integer) <= 255;

0 for error-free;

not 0 for error;

Note: If you return additional data, you can use echo or global variables

Return_var= ' func $var 1 $var 2 '

Exit status

Exit status (Exit State):

The function returns a value that is called the exit state. Exit status can be specified statement by return, otherwise the exit status of the function is the exit state of the last execution command of the function (0 indicates success, not 0 indicates an error code).

Can be in the script by $? Reference

An exit value of 0 indicates success

Exit Value not 0 indicates failure

Bash uses 128+n as its exit status when a command is exited because of a severe signal N

There's a signal.

128 and above

Severe signal N exit

Command not found:

127

File found but not executable:

126

Error in expansion or redirection: error code that can be used by the script

1-125

All built-in commands are returned when usage errors

2

Success

0

Built-in command replenishment

Built-in command additions:

: command

A colon (:) is an empty command that is occasionally used to simplify conditional logic, equivalent to an alias of true.

For example while:

Also used in the condition settings of a variable

For example: ${var:=value}

If ....; then

:

Fi

. command

Used to execute commands in the current shell

. ./shell_script

Equivalent to the source command

When executing the commands listed in the script, you are using the same shell that called the script program

Continuation character \[enter]

Must be the last character of the line for unified command across multiple lines

echo command

String with line break at the end of the output

Echo–n "string to output" bash style go to line break

eval command

Allows parameters to be evaluated, which is the shell's built-in command

That allows code to be generated and run at any time

EXEC command

Two kinds of usage

Typical: Replace the current shell with a different program

EXEC Wall "Thanks for all the fish"

The second usage modifies the current file descriptor

EXEC 3 < Afile

Exit n Command

Causes the script to end running with exit code n

If you do not specify an exit state when the script exits, the last command state of the script is executed as the return value

Export command

Feature Description: Sets or displays environment variables.

Syntax: Export [-fnp][variable name]=[variable setting value]

Parameters

-F represents the function name in [variable name].

-n Deletes the specified variable. The variables are not actually deleted, but are not exported to the execution environment of the subsequent directives.

-p lists all the environment variables that the shell assigns to the program

Attention:

1, the script is executed in a child shell environment run, the script after the execution of the child shell automatically exit;

2. The system environment variables in a shell are copied to the child shell (variables defined with export);

3. A system environment variable in a shell is valid only for the shell or its child shell, and the shell ends with the variable disappearing (and cannot be returned to the parent shell).

4. Variables defined without export are valid only for the shell, and the child shell is not valid.

Additional notes:

1. When executing a program in the shell, the shell provides a set of environment variables. Export can add, modify, or delete environment variables for use by subsequent executing programs. The effect of export only extends to the operation of this landing.

2.

Export creates its own parameters as an environment variable that can be seen by other scripts and programs called by the current program

The exported variable forms the environment variable of any child process derived from the shell

Expr command

Format Expr Argu operator Argu [must take space]

Use its parameters as an expression to evaluate the most common use of simple mathematical operations

x= ' expr $x + 1 ' Note that spaces on both sides of the operator must be

x=$ (expr $x + 1)

Example:

1. Direct calculation

$EXPR 10 + 10

$expr 30/3

$EXPR 30 \* 3 "must be escaped"

2. Incremental calculation

$LOOP =10

$LOOP = ' expr $LOOP + 1 '//counter-quotes

3. Numerical test

Use expr to test for numbers

$VALUE =12

$expr $VALUE + >/dev/null 2>&1

$echo $?

0

4. The exit status of expr itself

Note: The return value is exactly the opposite of the system Exit command

$VALUE = "Hello"

$expr $VALUE = "Hello"

1

$echo $?

0

5. Pattern matching and extraction

$VALUE = "Hello"

$expr $VALUE: '. * '

5

Extraction

$VALUE = "Account.doc"

$expr $VALUE: '.?. Doc

Account

Let command

The system default "+" is handled as a string, so it is not treated as an operator, but is only recognized as a normal string

Use let to force a number

$ d=111

$ echo $d +1

111+1

$ let d= $d +1; Echo $d

112

printf command

Formatted output

D Decimal

A character of C

S a string

% One% character

printf "%s\n" Hello

printf "%s%d" "Hi" 6

return command

Returns a function that can have a numeric parameter, which, as the return value of the function, returns the exit code of the last command by default if the return command is not with a parameter

Set command

Setting parameter variables for the shell

Set $ (date)

echo the month is $

That is, the value of the parameter as the current script argument list.

Shift command

Move all parameter variables to the left one position, is $2->$1 $3->$2 original $ $ is discarded, the $ remains unchanged

can take parameter to move left one numeric parameter

Trap command

Used to specify the action to take when the signal is received

Common use: Complete cleanup when a script is interrupted

Trap command Signal

Note: Scripts are usually interpreted from top to bottom, and you must specify the trap command before you want to protect that part of the code

HUP (1) hangs, interrupts dropped or user exits raised

INT (2) interrupt Ctrl + C key combination occurs

Quit (3) exit, ctrl+\ key combination occurs

ABRT (6) Abort, dictation serious execution error caused

ALRM (14) Alarm to handle timeouts

Term (15) termination, issued when system shuts down

unset command

Remove a variable or function from an environment variable

You cannot delete a read-only variable defined by the shell itself

from:http://my.oschina.net/hanzhankang/blog/202724

Shell function "parameter passing and input output" & built-in functions

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.