The shell in Linux has system functions; shell programming has system functions like other programming languages, and you can also customize functions. For example, you can use the basename system function to get the file name. The syntax is "basename [pathname] [suffix]", using the dirname system function to return the path part of the file, the syntax is "dirname file location".
#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.
The shell in Linux has system functions
Function introduction
Shell programming is the same as other programming languages. System functions can also be customized functions. Among the system functions, we will introduce two here.
1. System function
basename basic syntax
Function: Return the last / part of the complete path, often used to obtain File name
basename [pathname] [suffix]
basename [string] [suffix] (Function description: The basename command will delete all prefixes including the last ('/') character, and then display the string.
Option:
suffix is the suffix. If suffix is specified, basename will remove the suffix from pathname or string.
Application Example
Case 1: Please return/ The "aaa.txt" part of root/test-linux/aaa/aaa.txt
basename /root/test-linux/aaa/aaa.txt
You can use basename to get the file name. If you put the suffix, only the file name will be returned, without the suffix
dirname Basic syntax
Function: Return the part before the last / of the complete path, often used to return the path part
dirname Absolute file path (Function description: Remove the file name (non-directory part) from the given file name containing the absolute path, and then return the remaining path (directory part))
Application Example
Case 1: Please return /root/test-linux/aaa/aaa.txt of /root/test-linux/aaa
dirname /root/test-linux/aaa/aaa.txt
Basic syntax
[ function ] funname[()] { Action; [return int;] }
Write the function name directly when calling: funname [value]
Application example
Case 1: Calculate the sum of two input parameters (dynamic acquisition), getSum
#!/bin/bash #定义一个函数 getSum function getSum(){ SUM=$[$n1+$n2] echo "和是=$SUM" } #输入两个值 read -p "请输入一个数n1=" n1 read -p "请输入一个数n2=" n2 #调用自定义函数 getSum $n1 $n2
Recommended learning:
Linux video tutorialThe above is the detailed content of Does the shell in linux have system functions?. For more information, please follow other related articles on the PHP Chinese website!