本文共 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 ‘/’.
不管路径是目录还是没有尾随'/'的文件,都将始终给出相同的路径而没有尾随'/'。
翻译自:
bash路径
转载地址:http://yhowd.baihongyu.com/