Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
how to read in user input ~/Downloads and resolve the ~?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
guyuming
Apprentice
Apprentice


Joined: 19 Nov 2020
Posts: 253

PostPosted: Fri Jan 10, 2025 7:40 am    Post subject: how to read in user input ~/Downloads and resolve the ~? Reply with quote

I need to read user input file path such as ~/Download with readline history, and resolve it with realpath.

however, the file_path variable value seems to have single quotation mark around it, so realpath says file not found in Chinese.

but if i echo $file_path, no single quotation mark shown.

How can I resolve user input from ~/Downloads into /home/guyuming/Downloads then?

Code:
guyuming@localhost ~ $ read -e -r file_path
~/Downloads
guyuming@localhost ~ $ realpath $file_path
realpath: '~/Downloads': 没有那个文件或目录
guyuming@localhost ~ $ echo $file_path
~/Downloads
guyuming@localhost ~ $ realpath '~/Downloads'
realpath: '~/Downloads': 没有那个文件或目录
guyuming@localhost ~ $ realpath ~/Downloads
/home/guyuming/Downloads
guyuming@localhost ~ $
Back to top
View user's profile Send private message
grknight
Retired Dev
Retired Dev


Joined: 20 Feb 2015
Posts: 1978

PostPosted: Fri Jan 10, 2025 2:03 pm    Post subject: Reply with quote

The reason why the last manual attempt works is because the shell expands the ~ before realpath sees it. The variable never is single quoted.

A way around this is to expand any leading ~ to $HOME after the read:
Code:
file_path="${file_path/#~/${HOME}}"

This may not work in all shells, but it definitely works in bash.
Back to top
View user's profile Send private message
guyuming
Apprentice
Apprentice


Joined: 19 Nov 2020
Posts: 253

PostPosted: Fri Jan 10, 2025 2:21 pm    Post subject: Reply with quote

grknight wrote:
The reason why the last manual attempt works is because the shell expands the ~ before realpath sees it. The variable never is single quoted.

A way around this is to expand any leading ~ to $HOME after the read:
Code:
file_path="${file_path/#~/${HOME}}"

This may not work in all shells, but it definitely works in bash.



Thank you very much! I had scratched my head on this for one day.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 479
Location: Naarm/Melbourne, Australia

PostPosted: Sat Jan 11, 2025 12:16 am    Post subject: Reply with quote

grknight wrote:
This may not work in all shells

Yeah, unfortunately this very handy syntax isn't POSIX, so although it works in Bash, Zsh and OpenBSD Ksh, it won't work in Dash. So a more generic solution is to use prefix pattern removal:

Code:
$ file_path='~/Downloads'
$ echo "${HOME}${file_path#\~}"

_________________
https://wiki.gentoo.org/wiki/User:Flexibeast
Back to top
View user's profile Send private message
guyuming
Apprentice
Apprentice


Joined: 19 Nov 2020
Posts: 253

PostPosted: Sat Jan 11, 2025 1:05 am    Post subject: Reply with quote

i think both "${HOME}${file_path#\~}" and "${file_path/#~/${HOME}}" are like those four-words-Chinese-idioms: concise, but nightmare for many kids :)

I had long taken for granted that realpath command will translate ~ for me. Literally, you see, realpath, means it, doesn't it?

so, i would suggest to enhance the realpath command with a option, such as --home for this.
Back to top
View user's profile Send private message
flexibeast
Guru
Guru


Joined: 04 Apr 2022
Posts: 479
Location: Naarm/Melbourne, Australia

PostPosted: Sat Jan 11, 2025 2:00 am    Post subject: Reply with quote

The `realpath` utility is specified by POSIX.

Tilde expansion is also specified by POSIX.

There isn't one single implementation of the `realpath` utility. By default, on Gentoo, it's provided by GNU coreutils (sys-apps/coreutils). But there's also the `realpath` provided by Busybox (sys-apps/busybox). And more generally, there's the `realpath` provided by BSDs (e.g. here's OpenBSD's).

Since Gentoo isn't the maintainer of any of this software, but merely the packager, you'd need to direct your suggestion to one or more of the relevant maintainers (e.g. here's the coreutils home page). You could also open a bug on austingroupbugs.net to ask that such an option be specified by POSIX (although that wouldn't, of course, inherently result in it being supported by all `realpath` implementations).
_________________
https://wiki.gentoo.org/wiki/User:Flexibeast
Back to top
View user's profile Send private message
Goverp
Advocate
Advocate


Joined: 07 Mar 2007
Posts: 2196

PostPosted: Sat Jan 11, 2025 10:49 am    Post subject: Reply with quote

There's an interesting discussion of handling this issue in Stackoverflow
_________________
Greybeard
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22973

PostPosted: Sat Jan 11, 2025 4:00 pm    Post subject: Reply with quote

Assuming use of bash with readline support enabled, it looks from my limited testing like compgen -W does what is needed:
Code:
$ v='~root'
$ compgen -W "$v"
/root
$ v='~dhcpcd'
$ compgen -W "$v"
/var/chroot/dhcpcd
Why is use of realpath a requirement?
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum