tilde-expansion

have to determine all users home directories - tilde scripting problem

天大地大妈咪最大 提交于 2019-11-30 15:19:38
Assume someuser has a home directory /home/someuser NAME=someuser In bash - what expression to I use combining tilde (~) and $NAME to return the users home directory? HOMEDIRECTORY=~someuser echo $HOMEDIRECTORY /home/someuser NAME=someuser echo ~$NAME ~someuser any suggestions? Safer : eval HOMEDIRECTORY="$(printf "~%q" "$NAME")" Here the %q option to printf quotes and escapes dangerous characters. If $NAME is joe, you'd get something like /home/joe . For root, you might get /root . For "abc;rm something" you'd get "~abc;rm something" instead of having something removed. If you have access to

How do I find a user's home directory in Perl?

▼魔方 西西 提交于 2019-11-27 20:58:45
I need to check whether a file in a user's home directory exists so use file check: if ( -e "~/foo.txt" ) { print "yes, it exists!" ; } Even though there is a file called foo.txt under the user's home directory, Perl always complains that there is no such file or directory. When I replace "~" with /home/jimmy (let's say the user is jimmy) then Perl give the right verdict. Could you explain why "~" dosen't work in Perl and tell me what is Perl's way to find a user's home directory? ~ is a bash -ism rather than a perl -ism, which is why it's not working. Given that you seem to be on a UNIX-type

Why isn't tilde (~) expanding inside double quotes? [duplicate]

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-26 04:28:58
问题 This question already has answers here : “~/Desktop/test.txt: No such file or directory” (2 answers) Closed last year . I want to check whether or not the hidden .git folder exists. First thought was to use: if [ -d \"~/.git\" ]; then echo \"Do stuff\" fi But the -d apparently does not look for hidden folders. 回答1: The problem has to do with the tilde being within double quotes. To get it expanded, you need to put the tilde outside the quotes: if [ -d ~/".git" ]; then # note tilde outside