function
A function is a collection of codes that perform a specific function. When we call a function, the incoming parameter may get a certain value, which is equivalent to modularizing the code and calling it repeatedly, which is the meaning of the function.
system function
The lightweight scripts already provided by the system can also be called system commands.
basename
basic grammar
basename [string/pathname][suffix]
Semantics: The basename command deletes all prefixes, including the last / character, and then displays the remaining string after deletion. It can be understood as taking the file name in the path
options:
- suffix: specify the file suffix name, if suffix is specified, basename will delete suffix
#!/bin/bash
if([ $# -ne 1 ])
then
echo "请输入路径"
exit
fi
basename $1
dirname
basic grammar
dirname 文件绝对路径
Semantics: dirname removes the file name in the given file name containing the absolute path. It can be understood as the absolute path name of the file path.
#!/bin/bash
if([ $# -ne 1 ])
then
echo "请输入路径"
exit
fi
dirname $1
custom function
basic grammar
// 声明函数
function functionname(){
// 执行命令
return int;
}
// 调用函数
add 参数1 参数2
Precautions:
- The function needs to be declared before calling the function
- The return value of the function can only be
$?
obtained through return, if return is not used, the result of the last command will be returned - return followed by a value, the value range is 0-255
the case
Encapsulate some simple code into a custom function
#!/bin/bash
function test(){
sum=0;
sum=$[$sum+1]
echo $sum
}
sum=$(test)
echo $sum