将百分比编码的文件 URL 转换为 bash 中的本地文件

sas*_*alm 4 url bash

我有一个百分比编码的文件 URL - 例如file:///home/sashoalm/Has%20Spaces.txt。我需要将其转换为本地文件 - /home/sashoalm/Has Spaces.txt.

我怎么能在 bash 中做到这一点?

anu*_*ava 5

在 BASH 中,您可以使用此实用程序功能:

decodeURL() {
   printf "$(sed 's#^file://##;s/+/ /g;s/%\(..\)/\\x\1/g;' <<< "$@")\n";
}
Run Code Online (Sandbox Code Playgroud)

然后将此函数调用为:

decodeURL 'file:///home/sashoalm/Has%20Spaces.txt'
/home/sashoalm/Has Spaces.txt
Run Code Online (Sandbox Code Playgroud)