How to convert $NAUTILUS_SCRIPT_CURRENT_URI into PATH
I need to convert $NAUTILU开发者_运维技巧S_SCRIPT_CURRENT_URI with non-latin symbols to PATH
eg/home/shara/%D0%A0%D0%B0%D0%B1%D0%BE%D1%87%D0%B8%D0%B9<br>%20%D1%81%D1%82%D0%BE%D0%BB/testdir
to
/home/shara/Рабочий стол/testdir
How to do this?Assuming the <br>
in the middle is just an accident, something like this:
in="/home/shara/%D0%A0%D0%B0%D0%B1%D0%BE%D1%87%D0%B8%D0%B9%20%D1%81%D1%82%D0%BE%D0%BB/testdir"
eval out=\$\'${in//%/\\x}\'
Note, this does no correction for your locale, it only replaces the %XX escapes with the matching raw byte. You can use iconv
if necessary. Also, if a raw % is escaped as %%
instead of %25
(this is not mentioned in your question) or if the input may contain backslashes, you will need to account for those too.
I did it with this
selected_files=($NAUTILUS_SCRIPT_SELECTED_FILE_PATHS)
for dir in "${selected_files[@]}" ; do
base=$(readlink -f "$(dirname "$dir")")
done
The following, adapted from unix stackexchange (in this case we don't need to replace '+' with ' '), will work with any POSIX shell, not just bash, (e.g. dash):
current_nautilus_path=$(echo "$NAUTILUS_SCRIPT_CURRENT_URI" | sed -e 's/%/\\x/g' -e 's_^file://__' | xargs -0 printf "%b")
Alternatively, if bashisms are OK, but you don't want to call any external programs, a slight modification of Jester's/Dennis Williamson's solution will give you the path without the "file:" prefix:
percent_decoded_uri=$(printf "${NAUTILUS_SCRIPT_CURRENT_URI//%/\\x}")
current_nautilus_path=${percent_decoded_uri#file://}
Finally, if calling python from within the script does not bother you, I strongly recommend the solution from askubuntu, suggested by Glutanimate, as it provides support for arbitrary gvfs protocols (e.g. sftp) for free:
current_nautilus_path=$(python -c 'import gio,sys; print(gio.File(sys.argv[1]).get_path())' "$NAUTILUS_SCRIPT_CURRENT_URI")
精彩评论