shell basics 5- function

Defined functions

function  fname(){
    
}

or

function_name(){
    
}

For simple functions, or even do so fname () {statement;}

Function call

Direct write function name. As fname

Pass parameters to a function

Parameters are passed in the position parameter, can not be passed in the form of parameters and set the default parameters, the parameters passed in location is as follows:

arg1 arg2 fname
simple example function as follows:

fname(){
    echo $1, $2; #访问参数1和参数2
    echo "$@"; #以列表的方式一次性打印所有参数
    echo "$*"; #类似于$@,但是所有参数被视为单个实体
    return 0; #返回值
}
fname a b

Explain unusual parameters:

  1. "$ @" Is expanded to "$ 1" "$ 2" "$ 3".
  2. "$ *" Is expanded to "$ 1c $ 2c $ 3", where c is the first character of the IFS.

IFS explained:
IFS is an important concept in shell scripts when processing text data, it is quite useful. IFS can be a White Space (Spacebar), Tab (the table key), Enter one or several (Enter key) in.
IFS (Internal Field Seperator) Linux shell in a predetermined delimiter for the command line is decomposed into word (field).
IFS setup is simple, and common variable setting method is similar:

IFS=":"

Save the original value of IFS before the recommended settings IFS, timely recovery after use.

Location parameter alias

Set up a command by setting the alias method and pass parameters go in and see what effect

[root@dns-node2 tmp]# alias lsg='ls |grep '
[root@dns-node2 tmp]# lsg sh
DaoJiShi.sh
debugsh.sh
testFunc.sh
testSet.sh

We write a function that takes a position parameter, whereby the parameter can be compared with the position parameter and the position at the alias function

[root@dns-node2 tmp]# getFile(){ ls |grep $1 ;}
[root@dns-node2 tmp]# getFile txt
output.txt
[root@dns-node2 tmp]# getFile sh
DaoJiShi.sh
debugsh.sh
testFunc.sh
testSet.sh

The expansion of knowledge function

recursive function

In Bash, the function also supports recursive call (you can call itself a function). For example, F. () {Echo $. 1; F. Hello;
SLEEP. 1;}.

Fork bomb
recursive function calls itself is able to function. This function must have exit conditions, otherwise it will continue to
generate their own until the system runs out of resources, or collapse all.
: () {: |: & } ;:
This function will always generate a new process, and ultimately the formation of a denial of service attack.
& The child process into the background before the function call. This dangerous code can not stop derived process,
which is called Fork bomb.

You can limit the most that can be generated by modifying the configuration file /etc/security/limits.conf nproc in
the number of large process and thus prevent such attacks.
The following statement can generate the number of all users of the process is limited to 100:

hard nproc 100

Export function

By export -f fnamederivation function, this way, a function's scope can be extended to the child, that is, the child process can also use this function a.

[root@dns-node2 tmp]# cat testExportFunc.sh
getFile sh
[root@dns-node2 tmp]# sh testExportFunc.sh
testExportFunc.sh: line 1: getFile: command not found

[root@dns-node2 tmp]# export -f getFile
[root@dns-node2 tmp]# sh testExportFunc.sh
DaoJiShi.sh
debugsh.sh
testExportFunc.sh
testFunc.sh
testSet.sh

Read command returns the value

Read $? Obtain the return value, if the command is successful exit, then the exit status of 0, otherwise non 0.

Pass parameters to the command

In the script, command line parameters can be accessed according to their position in the command line. $ 1 is the first parameter, the second parameter
is a $ 2, and so on.
The following statement can show the first three command line parameters:

echo $1 $2 $3

The more common approach is to iterate over all the command-line parameters. shift command to turn left to move a bit parameter
is set, the script can use $ 1 to access to every parameter. The following code shows all command line parameters:

$ cat showArgs.sh
for i in `seq 1 $#`
do
echo $i is $1
shift
done
$ sh showArgs.sh a b c
1 is a
2 is b
3 is c

Guess you like

Origin www.cnblogs.com/liaojiafa/p/11530934.html