当前位置:网站首页>Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function
Shell function, system function, basename [string / pathname] [suffix] can be understood as taking the file name in the path, dirname file absolute path, and user-defined function
2022-07-27 10:05:00 【Redamancy06】
List of articles
1. function
1.1 System function
1.1.1basename
1.1.1.1 Basic grammar
basename [string / pathname] [suffix] ( Function description :basename The command will delete all prefixes including the last (‘/’) character , Then display the string .
basename It can be understood as taking the file name in the path
Options :
suffix For the suffix , If suffix Designated ,basename Will pathname or string Medium suffix Get rid of .
1.1.2dirname
dirname File absolute path ( Function description : Remove filename from given filename with absolute path ( Non catalog part ), Then return to the rest of the path ( Part of the catalog ))
dirname It can be understood as the absolute path name of the file path
1.1.3 Case study
1.1.3.1date+%s Timestamp exercise



1.1.3.2basename The practice of

Cut out the file name 
parameter File code 
The displayed file name includes the path , We don't need the output path , So add basename To solve


1.1.3.3dirname The practice of
dirname Intercept the path before the last slash , Do not include filename
and basename Intercept the file name after the last slash , Exclude path



If only pwd Words , Executing code in other folders will not print the directory of the script , Need to add dirname, as follows 

add dirname The script path after output remains unchanged
1.2 Custom function
1.2.1 Basic grammar
[ function ] funname[()]
{
Action;
[return int;]
}
1.2.2 Experience and skill
(1) Must be before calling function place , Declare function first ,shell The script is run line by line . It doesn't compile first like any other language .
(2) Function return value , Only through $? System variable acquisition , Can display plus :return return , If not , Results will be run with the last command , As return value .return Heel value n(0-255)
1.2.3 Case study
1.2.3.1 Not used return Version of



Can output and
1.2.3.2 use return If you come back


It can be seen that ,return It must be followed by a number , Revised as follows :

In this way, the results can be output normally , however return Can only return 0-255, If the number exceeds, the correct answer cannot be displayed , as follows 
156+245!=145 Of , So it can explain return Only return 0-255 Number of numbers , How can we solve this problem , Look at the code below 
In this way, the function add Inside echo The value is assigned to ans, And then output 
The result is also right , Explain that this is correct
边栏推荐
- oracle rac 19c pdb实例当掉
- Brush the title "sword finger offer" day04
- Talk about 10 scenarios of index failure. It's too stupid
- 刷题《剑指Offer》day03
- Summary of binary tree exercises
- c'mon! Please don't ask me about ribbon's architecture principle during the interview
- Annotation and reflection
- 并发之线程状态转换
- MOS drive in motor controller
- 加油程序君
猜你喜欢

直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly

二叉树习题总结

VS2019+CUDA11.1新建项目里没有CUDA选项

Visual slam lecture notes (I): Lecture 1 + Lecture 2

吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
![[SCM]源码管理 - perforce 分支的锁定](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[SCM]源码管理 - perforce 分支的锁定

Looking for a job for 4 months, interviewing 15 companies and getting 3 offers

Concurrent Park and unpark description

GBase 8a MPP集群扩容实战

Nacos configuration center dynamically refreshes the data source
随机推荐
食品安全 | 还在吃酵米面吗?当心这些食物有毒!
省应急管理厅:广州可争取推广幼儿应急安全宣教经验
7/26 思维+dp+后缀数组的学习
达梦 PARTGROUPDEF是自定义的对象吗?
LeetCode.565. 数组嵌套____暴力dfs->剪枝dfs->原地修改
Acl2021 best paper released, from ByteDance
【云原生 • DevOps】一文掌握容器管理工具 Rancher
2016 outlook
StyleGAN论文笔记+修改代码尝试3D点云生成
Leetcode.814. binary tree pruning____ DFS
Concurrent thread state transition
TFlite 的简单使用
oracle rac 19c pdb实例当掉
Review of in vivo detection
Anaconda安装(非常详细)
Dcgan paper improvements + simplified code
面试必备:虾皮服务端15连问
How does data analysis solve business problems? Here is a super detailed introduction
Shell运算符、$((运算式))” 或 “$[运算式]、expr方法、条件判断、test condition、[ condition ]、两个整数之间比较、按照文件权限进行判断、按照文件类型进行判断
并发之线程状态转换