Does the shell in linux have system functions?

WBOY
Release: 2022-06-23 10:27:23
Original
2868 people have browsed it

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".

Does the shell in linux have system functions?

#The operating environment of this tutorial: linux7.3 system, Dell G3 computer.

Does the shell in Linux have system functions?

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]
Copy after login

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
Copy after login

You can use basename to get the file name. If you put the suffix, only the file name will be returned, without the suffix

Does the shell in linux have system functions?

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
Copy after login

Does the shell in linux have system functions?

2. Custom function

Basic syntax

[ function ] funname[()]
{
Action;
[return int;]
}
Copy after login

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
Copy after login

Does the shell in linux have system functions? Recommended learning:

Linux video tutorial

The 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!