tilde-expansion

have to determine all users home directories - tilde scripting problem

雨燕双飞 提交于 2020-01-10 19:46:13
问题 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? 回答1: 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

Bash script, returns awk: can't open file ~/.ssh/config

谁说我不能喝 提交于 2020-01-07 02:49:15
问题 I'm able to awk this file directly from the command line but when I try to use it from my script it breaks. Not sure why. Heres my script #!/bin/bash ssh_config_path="~/.ssh/config" echo -n "Enter the username of the account you'd like to switch to > " read username awk ' !x{x=sub(/github-secondary/,"github.com")} !y{y=sub(/github\.com/,"github-secondary")} 1' $ssh_config_path 回答1: In quotes bash does not expand ~ . I suggest to use $HOME : ssh_config_path="$HOME/.ssh/config" 来源: https:/

No such file or directory (ls) in conjunction with tilde expansion

こ雲淡風輕ζ 提交于 2020-01-04 02:11:47
问题 I am writing a simple bash script and wanted to display all the items in a a particular directory. I tried doing the following: desktop="~/Desktop/testr/" echo $desktop echo `ls $desktop` However I keep getting the output: ~/Desktop/testr/ ls: ~/Desktop/testr/: No such file or directory But when I run ls from the terminal, I can see the items. I suspect that the problem is that the ~ is not getting expanded but I thought that the double quotes would have taken care of that. Thanks for your

How to manually expand a special variable (ex: ~ tilde) in bash

蓝咒 提交于 2019-12-27 08:49:50
问题 I have a variable in my bash script whose value is something like this: ~/a/b/c Note that it is unexpanded tilde. When I do ls -lt on this variable (call it $VAR), I get no such directory. I want to let bash interpret/expand this variable without executing it. In other words, I want bash to run eval but not run the evaluated command. Is this possible in bash? How did I manage to pass this into my script without expansion? I passed the argument in surrounding it with double quotes. Try this

Tilde not expanded when quoting on the right hand side of a Bash variable assignment [duplicate]

泪湿孤枕 提交于 2019-12-24 18:43:51
问题 This question already has answers here : Why isn't tilde (~) expanding inside double quotes? [duplicate] (1 answer) Tilde expansion in quotes (3 answers) Tilde in path doesn't expand to home directory (4 answers) Closed last year . I have made a directory ~/test_myDir I then run the following bash script: x="myDir" dirName="~/test_$x" cd $dirName echo "hey" > test.txt I get the following error: test.sh: line 5: cd: ~/test_myDir: No such file or directory I then remove the quotes from the

Tilde not expanded when quoting on the right hand side of a Bash variable assignment [duplicate]

一曲冷凌霜 提交于 2019-12-24 18:41:55
问题 This question already has answers here : Why isn't tilde (~) expanding inside double quotes? [duplicate] (1 answer) Tilde expansion in quotes (3 answers) Tilde in path doesn't expand to home directory (4 answers) Closed last year . I have made a directory ~/test_myDir I then run the following bash script: x="myDir" dirName="~/test_$x" cd $dirName echo "hey" > test.txt I get the following error: test.sh: line 5: cd: ~/test_myDir: No such file or directory I then remove the quotes from the

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

流过昼夜 提交于 2019-12-17 16:03:53
问题 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? 回答1: ~ is a

Tilde in path doesn't expand to home directory

匆匆过客 提交于 2019-12-16 21:32:33
问题 Say I have a folder called Foo located in /home/user/ (my /home/user also being represented by ~ ). I want to have a variable a="~/Foo" and then do cd $a I get -bash: cd: ~/Foo: No such file or directory However if I just do cd ~/Foo it works fine. Any clue on how to get this to work? 回答1: You can do (without quotes during variable assignment): a=~/Foo cd "$a" But in this case the variable $a will not store ~/Foo but the expanded form /home/user/Foo . Or you could use eval : a="~/Foo" eval cd

Swift: How to expand a tilde in a path String

独自空忆成欢 提交于 2019-12-14 03:40:41
问题 How can I expand a path String with a tilde in Swift? I have a string like "~/Desktop" and I'd like to use this path with the NSFileManager methods, which requires the tilde to be expanded to "/Users/<myuser>/Desktop" . (This question with a clear problem statement doesn't exist yet, this should be easily findable. Some similar but not satisfying questions are Can not make path to the file in Swift, Simple way to read local file using Swift?, Tilde-based Paths in Objective-C) 回答1: Tilde

Bash Tilde Expansion

帅比萌擦擦* 提交于 2019-12-12 09:53:31
问题 Is it possible to add to the rules that bash uses for tilde expansion? I'd like to have ~data expand to /data/users/me, ~scratch expand to /data/scratch/me etc.. Is this possible, or does bash have too tight a tight hold on the '~'? Thanks, Andrew 回答1: Tilde expansion is tied to users' home directories (or the contents of the directory stack or $PWD or $OLDPWD ). Use variable expansion, aliases or functions to accomplish what you're after. You can also use CDPATH to list a set of directories