1 system function
1. basename basic grammar
the basename [String / pathname] [suffix] (Function Description: the basename command to delete all including the last prefix ( '/' ) character, then the character string is displayed.
Options:
suffix is a suffix, if suffix is specified, the basename will pathname or string the suffix removed.
2. Case practical operation
( 1 ) intercepts the /home/atguigu/banzhang.txt path name of the file
[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt
banzhang.txt
[atguigu@hadoop101 datas]$ basename /home/atguigu/banzhang.txt .txt
banzhang
3. dirname basic grammar
dirname absolute file path (Function Description: remove the file name (some non-directory) from the given file name contains the absolute path, and then returns the remaining path (part of the directory))
4. Case practical operation
( 1 ) obtain banzhang.txt file path
[atguigu@hadoop101 ~]$ dirname /home/atguigu/banzhang.txt
/home/atguigu
2 custom function
1. The basic syntax
[ function ] funname[()]
{
Action;
[return int;]
}
funname
2. Experience Skills
( 1 ) must be in place before calling the function, declare the function, shell script is run line by line. Not like other languages to compile.
( 2 ) function returns a value, only through the $? The system variable, you can display plus: return to return, if not, the result will be the last command run, as the return value. return followed by the value n (0-255)
3. Case practical operation
( 1 ) calculates two input parameters and
[atguigu@hadoop101 datas]$ touch fun.sh
[Atguigu @ hadoop101 dates] $ came fun.sh
#!/bin/bash
function sum()
{
s=0
s=$[ $1 + $2 ]
echo "$s"
}
read -p "Please input the number1: " n1;
read -p "Please input the number2: " n2;
sum $n1 $n2;
[atguigu@hadoop101 datas]$ chmod 777 fun.sh
[atguigu@hadoop101 datas]$ ./fun.sh
Please input the number1: 2
Please input the number2: 5
7
3 Return Values
return returns 0 to 255
return the string "return value"
Get a return value:
echo $?