bash路径_如何删除Bash中的尾随路径?

bash路径

I am writing a Bash script 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 string.

您可以采用简单的方式进行操作,例如检查最后一个字符是否为'/',并且路径是否为目录的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)回答。

翻译自: https://www.systutorials.com/how-to-remove-trailing-in-path-in-bash/

bash路径

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值