博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bash路径_如何删除Bash中的尾随路径?
阅读量:2515 次
发布时间:2019-05-11

本文共 1150 字,大约阅读时间需要 3 分钟。

bash路径

I am writing a Bash accepting input of a path from the user. I want to make the path have no trailing ‘/’, such as ‘/path/to/dir’ instead of ‘/path/to/dir/’.

我正在编写一个Bash 接受来自用户的路径输入。 我想使路径没有尾随“ /”,例如“ / path / to / dir”而不是“ / path / to / dir /”。

However, as the input is from the user, the input could be ‘/path/to/dir’. My script is intended to handle this.

但是,由于输入来自用户,因此输入可能是“ / path / to / dir”。 我的脚本旨在解决这个问题。

The question is: how to remove the trailing ‘/’ if the input indeed has it? When the input has not trailing ‘/’, it should be fine.

问题是:如果输入确实包含后缀“ /”,如何删除? 当输入的末尾没有尾号“ /”时,应该没问题。

You may do in a straight forward way like check whether the last character is ‘/’ and the path is a to a directory and remove that ‘/’ from the .

您可以采用简单的方式进行操作,例如检查最后一个字符是否为'/',并且路径是否为目录的a,然后从删除该'/'。

But here I provide another much easier method:

但是在这里,我提供了另一种更简单的方法:

`dirname $path`/`basename $path`

will consistently gives the same path without the trailing ‘/’ no matter whether the path is a directory or a file without or without the trailing ‘/’.

不管路径是目录还是没有尾随'/'的文件,都将始终给出相同的路径而没有尾随'/'。

Answered by Eric Z Ma.
埃里克·马(Eric Z Ma)回答。

翻译自:

bash路径

转载地址:http://yhowd.baihongyu.com/

你可能感兴趣的文章
vue base64
查看>>
【Django实战开发】案例一:创建自己的blog站点-1.安装及搭建开发环境
查看>>
Pie(二分)
查看>>
Mysql 索引优化
查看>>
09湖州二模(自选模块不等式)
查看>>
Mybatis Batch 批量操作
查看>>
Ubuntu server搭建Java web服务器
查看>>
WSGI学习系列WSME
查看>>
java读取xml配置文件和properties配置文件
查看>>
HDU 4300 Contest 1
查看>>
POJ 3311
查看>>
Button MouseEvent颜色变化
查看>>
Volist标签
查看>>
浅谈模块化
查看>>
14个免费访客行为分析工具
查看>>
beego orm关联查询之多对多(m2m)
查看>>
(转)arguments.callee移除AS3匿名函数的侦听
查看>>
onNewIntent调用时机
查看>>
MYSQL GTID使用运维介绍(转)
查看>>
Fail to start neutron-server
查看>>