I want to get the home directory of a user of a unix system. Why doesn't the following work:
# sudo su $offender -c "bash -s < <(echo echo \$HOME)"
sh: Syntax error: redirection unexpected
It works for me in Ubuntu and Fedora using sudo su $offender -c "echo \$HOME"
You could also gouge it from your /etc/passwd file like so:
grep "^$offender" /etc/passwd | cut -d':' -f6
Don't really know why it doesn't work, but here's a way to get the home directory that does not involve spawning a shell as that user:
getent passwd "${offender:?No Offending Account Given}" | awk -F':' 'NR==1{print $6}'
The direct problem is that sh
does not recognize process substitution (the <(echo $HOME)
) notation, even when it is a link to bash
.
It seems like a rather brute force way to get the information. Doesn't:
eval echo ~$offender
work for you?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.